A modded EditSaber for making beat saber maps.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "FileDownloader.h"
  3. #include "BeatSaberEditor.h"
  4. #include "PlatformFilemanager.h"
  5. #include "GenericPlatform/GenericPlatformFile.h"
  6. #include "Paths.h"
  7. UFileDownloader::UFileDownloader():
  8. FileUrl(TEXT(""))
  9. , FileSavePath(TEXT(""))
  10. {
  11. }
  12. UFileDownloader::~UFileDownloader()
  13. {
  14. }
  15. UFileDownloader* UFileDownloader::MakeDownloader()
  16. {
  17. UFileDownloader* Downloader = NewObject<UFileDownloader>();
  18. return Downloader;
  19. }
  20. UFileDownloader* UFileDownloader::DownloadFile(const FString& Url, FString SavePath)
  21. {
  22. FileUrl = Url;
  23. FileSavePath = SavePath;
  24. TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest();
  25. HttpRequest->SetVerb("GET");
  26. HttpRequest->SetURL(Url);
  27. HttpRequest->OnProcessRequestComplete().BindUObject(this, &UFileDownloader::OnReady);
  28. HttpRequest->OnRequestProgress().BindUObject(this, &UFileDownloader::OnProgress_Internal);
  29. // Execute the request
  30. HttpRequest->ProcessRequest();
  31. AddToRoot();
  32. return this;
  33. }
  34. void UFileDownloader::OnReady(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
  35. {
  36. RemoveFromRoot();
  37. Request->OnProcessRequestComplete().Unbind();
  38. if (Response.IsValid() && EHttpResponseCodes::IsOk(Response->GetResponseCode()))
  39. {
  40. // SAVE FILE
  41. IPlatformFile & PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
  42. // create save directory if not existent
  43. FString Path, Filename, Extension;
  44. FPaths::Split(FileSavePath, Path, Filename, Extension);
  45. if (!PlatformFile.DirectoryExists(*Path))
  46. {
  47. if(!PlatformFile.CreateDirectoryTree(*Path))
  48. {
  49. OnResult.Broadcast(EDownloadResult::DirectoryCreationFailed);
  50. return;
  51. }
  52. }
  53. // open/create the file
  54. IFileHandle* FileHandle = PlatformFile.OpenWrite(*FileSavePath);
  55. if (FileHandle)
  56. {
  57. // write the file
  58. FileHandle->Write(Response->GetContent().GetData(), Response->GetContentLength());
  59. // Close the file
  60. delete FileHandle;
  61. OnResult.Broadcast(EDownloadResult::Success);
  62. }
  63. else
  64. {
  65. OnResult.Broadcast(EDownloadResult::SaveFailed);
  66. }
  67. }
  68. else
  69. {
  70. OnResult.Broadcast(EDownloadResult::DownloadFailed);
  71. }
  72. }
  73. void UFileDownloader::OnProgress_Internal(FHttpRequestPtr Request, int32 BytesSent, int32 BytesReceived)
  74. {
  75. int32 FullSize = Request->GetContentLength();
  76. OnProgress.Broadcast(BytesSent, BytesReceived, FullSize);
  77. }