|
| 1 | +using Morph.Server.Sdk.Model; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Morph.Server.Sdk.Client |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Transfer file from/to server to/from local file |
| 11 | + /// </summary> |
| 12 | + public class DataTransferUtility : IDataTransferUtility |
| 13 | + { |
| 14 | + private int BufferSize { get; set; } = 81920; |
| 15 | + private readonly IMorphServerApiClient _morphServerApiClient; |
| 16 | + private readonly ApiSession _apiSession; |
| 17 | + |
| 18 | + public DataTransferUtility(IMorphServerApiClient morphServerApiClient, ApiSession apiSession) |
| 19 | + { |
| 20 | + this._morphServerApiClient = morphServerApiClient ?? throw new ArgumentNullException(nameof(morphServerApiClient)); |
| 21 | + this._apiSession = apiSession ?? throw new ArgumentNullException(nameof(apiSession)); |
| 22 | + } |
| 23 | + |
| 24 | + public async Task SpaceUploadFileAsync(string localFilePath, string serverFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false) |
| 25 | + { |
| 26 | + if (!File.Exists(localFilePath)) |
| 27 | + { |
| 28 | + throw new FileNotFoundException(string.Format("File '{0}' not found", localFilePath)); |
| 29 | + } |
| 30 | + var fileSize = new FileInfo(localFilePath).Length; |
| 31 | + var fileName = Path.GetFileName(localFilePath); |
| 32 | + using (var fsSource = new FileStream(localFilePath, FileMode.Open, FileAccess.Read)) |
| 33 | + { |
| 34 | + var request = new SpaceUploadDataStreamRequest |
| 35 | + { |
| 36 | + DataStream = fsSource, |
| 37 | + FileName = fileName, |
| 38 | + FileSize = fileSize, |
| 39 | + OverwriteExistingFile = overwriteExistingFile, |
| 40 | + ServerFolder = serverFolder |
| 41 | + }; |
| 42 | + await _morphServerApiClient.SpaceUploadDataStreamAsync(_apiSession, request, cancellationToken); |
| 43 | + return; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + public async Task SpaceDownloadFileIntoFileAsync(string remoteFilePath, string targetLocalFilePath, CancellationToken cancellationToken, bool overwriteExistingFile = false) |
| 51 | + { |
| 52 | + if (remoteFilePath == null) |
| 53 | + { |
| 54 | + throw new ArgumentNullException(nameof(remoteFilePath)); |
| 55 | + } |
| 56 | + |
| 57 | + if (targetLocalFilePath == null) |
| 58 | + { |
| 59 | + throw new ArgumentNullException(nameof(targetLocalFilePath)); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + var localFolder = Path.GetDirectoryName(targetLocalFilePath); |
| 64 | + var tempFile = Path.Combine(localFolder, Guid.NewGuid().ToString("D") + ".emtmp"); |
| 65 | + |
| 66 | + if (!overwriteExistingFile && File.Exists(targetLocalFilePath)) |
| 67 | + { |
| 68 | + throw new Exception($"Destination file '{targetLocalFilePath}' already exists."); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + using (Stream tempFileStream = File.Open(tempFile, FileMode.Create)) |
| 75 | + { |
| 76 | + using (var serverStreamingData = await _morphServerApiClient.SpaceOpenStreamingDataAsync(_apiSession, remoteFilePath, cancellationToken)) |
| 77 | + { |
| 78 | + await serverStreamingData.Stream.CopyToAsync(tempFileStream, BufferSize, cancellationToken); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (File.Exists(targetLocalFilePath)) |
| 83 | + { |
| 84 | + File.Delete(targetLocalFilePath); |
| 85 | + } |
| 86 | + File.Move(tempFile, targetLocalFilePath); |
| 87 | + |
| 88 | + } |
| 89 | + finally |
| 90 | + { |
| 91 | + //drop file |
| 92 | + if (tempFile != null && File.Exists(tempFile)) |
| 93 | + { |
| 94 | + File.Delete(tempFile); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public async Task SpaceDownloadFileIntoFolderAsync(string remoteFilePath, string targetLocalFolder, CancellationToken cancellationToken, bool overwriteExistingFile = false) |
| 101 | + { |
| 102 | + if (remoteFilePath == null) |
| 103 | + { |
| 104 | + throw new ArgumentNullException(nameof(remoteFilePath)); |
| 105 | + } |
| 106 | + |
| 107 | + if (targetLocalFolder == null) |
| 108 | + { |
| 109 | + throw new ArgumentNullException(nameof(targetLocalFolder)); |
| 110 | + } |
| 111 | + |
| 112 | + string destFileName = null; |
| 113 | + var tempFile = Path.Combine(targetLocalFolder, Guid.NewGuid().ToString("D") + ".emtmp"); |
| 114 | + try |
| 115 | + { |
| 116 | + using (Stream tempFileStream = File.Open(tempFile, FileMode.Create)) |
| 117 | + { |
| 118 | + |
| 119 | + using (var serverStreamingData = await _morphServerApiClient.SpaceOpenStreamingDataAsync(_apiSession, remoteFilePath, cancellationToken)) |
| 120 | + { |
| 121 | + destFileName = Path.Combine(targetLocalFolder, serverStreamingData.FileName); |
| 122 | + |
| 123 | + if (!overwriteExistingFile && File.Exists(destFileName)) |
| 124 | + { |
| 125 | + throw new Exception($"Destination file '{destFileName}' already exists."); |
| 126 | + } |
| 127 | + |
| 128 | + await serverStreamingData.Stream.CopyToAsync(tempFileStream, BufferSize, cancellationToken); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (File.Exists(destFileName)) |
| 133 | + { |
| 134 | + File.Delete(destFileName); |
| 135 | + } |
| 136 | + File.Move(tempFile, destFileName); |
| 137 | + |
| 138 | + } |
| 139 | + finally |
| 140 | + { |
| 141 | + //drop file |
| 142 | + if (tempFile != null && File.Exists(tempFile)) |
| 143 | + { |
| 144 | + File.Delete(tempFile); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + public async Task SpaceUploadFileAsync(string localFilePath, string serverFolder, string destFileName, CancellationToken cancellationToken, bool overwriteExistingFile = false) |
| 152 | + { |
| 153 | + if (!File.Exists(localFilePath)) |
| 154 | + { |
| 155 | + throw new FileNotFoundException(string.Format("File '{0}' not found", localFilePath)); |
| 156 | + } |
| 157 | + var fileSize = new FileInfo(localFilePath).Length; |
| 158 | + |
| 159 | + using (var fsSource = new FileStream(localFilePath, FileMode.Open, FileAccess.Read)) |
| 160 | + { |
| 161 | + var request = new SpaceUploadDataStreamRequest |
| 162 | + { |
| 163 | + DataStream = fsSource, |
| 164 | + FileName = destFileName, |
| 165 | + FileSize = fileSize, |
| 166 | + OverwriteExistingFile = overwriteExistingFile, |
| 167 | + ServerFolder = serverFolder |
| 168 | + }; |
| 169 | + await _morphServerApiClient.SpaceUploadDataStreamAsync(_apiSession, request, cancellationToken); |
| 170 | + return; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | + |
0 commit comments