/// <inheritdoc/> public async Task <UploadChunkResultModel> UploadAsync(int totalChunks, int chunkNumber, string uid, Stream input, CancellationToken ct) { if (string.IsNullOrWhiteSpace(uid)) { throw new ArgumentNullException(nameof(uid)); } if (input == null) { throw new ArgumentNullException(nameof(input)); } string localPath = CreateLocalUploadPath(uid); string chunkFilename = GetChunkedFilename(chunkNumber, uid); string chunkFilePath = fileSystem.Path.Combine(localPath, chunkFilename); // TODO: define max chunk count int currentChunkCount = fileSystem.Directory.GetFiles(localPath).Length; using (Stream output = fileSystem.File.OpenWrite(chunkFilePath)) { input.CopyTo(output); } var result = new UploadChunkResultModel() { IsCompleted = currentChunkCount + 1 == totalChunks }; return(await Task.FromResult(result)); }
/// <inheritdoc/> public async Task <UploadChunkResultModel> UploadAsync(int totalChunks, int chunkNumber, string uid, Stream input, CancellationToken ct) { Ensure.ArgumentNotNullOrWhiteSpace(uid, nameof(uid)); Ensure.ArgumentNotNull(input, nameof(input)); if (input.Length < constants.MinUploadChunkSize || input.Length > constants.MaxUploadChunkSize) { throw new ArgumentException($"The size of a single chunk must be between {constants.MinUploadChunkSize} and {constants.MaxUploadChunkSize} bytes."); } string localPath = CreateLocalUploadPath(uid); string chunkFilename = GetChunkedFilename(chunkNumber, uid); string chunkFilePath = fileSystem.Path.Combine(localPath, chunkFilename); int currentChunkCount = fileSystem.Directory.GetFiles(localPath).Length; if (configuration.Options.MaxSizeKilobytes > 0 && currentChunkCount * input.Length > configuration.Options.MaxSizeKilobytes * 1000) { fileSystem.Directory.Delete(localPath, true); throw new ArgumentException($"The file size exceeds the limit of {configuration.Options.MaxSizeKilobytes} kilobytes."); } using (Stream output = fileSystem.File.OpenWrite(chunkFilePath)) { input.CopyTo(output); } var result = new UploadChunkResultModel() { IsCompleted = currentChunkCount + 1 == totalChunks }; return(await Task.FromResult(result)); }