/// <inheritdoc/> public async Task <IList <UploadResult> > UploadAsync(string workspace, IList <string> paths, UploadFileOverrides overrides = null) { // Resolve the workspace var workspaceItem = await _proKnow.Workspaces.ResolveAsync(workspace); // Upload the files return(await UploadAsync(workspaceItem, paths, overrides)); }
/// <inheritdoc/> public async Task <IList <UploadResult> > UploadAsync(WorkspaceItem workspaceItem, IList <string> paths, UploadFileOverrides overrides = null) { // Get a list of all of the files to be uploaded var batchPaths = new List <string>(); AddFiles(batchPaths, paths); // Upload the files return(await UploadFilesAsync(workspaceItem.Id, batchPaths, overrides)); }
/// <summary> /// Uploads a file asynchronously /// </summary> /// <param name="workspaceId">The ProKnow ID for the workspace</param> /// <param name="path">The full path to the file to be uploaded</param> /// <param name="overrides">Optional overrides to be applied after the file is uploaded</param> /// <returns>The upload result</returns> private async Task <UploadResult> UploadFileAsync(string workspaceId, string path, UploadFileOverrides overrides = null) { // Gather information for the upload var initiateFileUploadInfo = BuildInitiateFileUploadInfo(workspaceId, path, overrides); // Initiate the file upload _logger.LogDebug($"Initiating upload of file {path} with size {initiateFileUploadInfo.FileSize / 1024.0:0.##} KB."); var initiateFileUploadResponse = await InitiateFileUploadAsync(initiateFileUploadInfo); // If the file has not already been uploaded if (initiateFileUploadResponse.Status == "uploading") { // Upload the file contents in chunks var uploadChunkInfos = ChunkFile(initiateFileUploadInfo, initiateFileUploadResponse); _logger.LogDebug($"Uploading file {path} in {uploadChunkInfos.Count} chunk(s)."); var tasks = new List <Task>(); foreach (var uploadChunkInfo in uploadChunkInfos) { tasks.Add(Task.Run(async() => { await UploadChunkAsync(uploadChunkInfo); } )); } await Task.WhenAll(tasks); } else { // Overwrite the filename in case of duplicate content (ProKnow returns the original filename rather than the one just uploaded) initiateFileUploadResponse.Path = path; _logger.LogDebug($"Skipped uploading of file {path} (duplicate content)."); } return(new UploadResult(initiateFileUploadResponse.Id, initiateFileUploadResponse.Path, initiateFileUploadResponse.Status)); }
/// <summary> /// Collects the information needed to build an initiate file upload request /// </summary> /// <param name="workspaceId">The ProKnow ID of the workspace to which the file will be uploaded</param> /// <param name="path"></param> /// <param name="overrides"></param> /// <returns>The information needed to build an initiate file upload request</returns> private InitiateFileUploadInfo BuildInitiateFileUploadInfo(string workspaceId, string path, UploadFileOverrides overrides = null) { using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(path)) { return(new InitiateFileUploadInfo { WorkspaceId = workspaceId, Path = path, FileSize = stream.Length, Checksum = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(), ChunkSize = UPLOAD_CHUNK_SIZE_IN_BYTES, NumberOfChunks = Math.Max(1, Convert.ToInt32(Math.Floor(Convert.ToDouble(stream.Length) / UPLOAD_CHUNK_SIZE_IN_BYTES))), Overrides = overrides }); } } }
/// <summary> /// Uploads files asynchronously /// </summary> /// <param name="workspaceId">The ProKnow ID for the workspace</param> /// <param name="paths">The full paths to the files to be uploaded</param> /// <param name="overrides">Optional overrides to be applied after the file is uploaded</param> /// <returns>The upload results</returns> private async Task <IList <UploadResult> > UploadFilesAsync(string workspaceId, IList <string> paths, UploadFileOverrides overrides) { var tasks = new List <Task>(); var uploadResults = new ConcurrentBag <UploadResult>(); foreach (var path in paths) { tasks.Add(Task.Run(async() => { var uploadResult = await UploadFileAsync(workspaceId, path, overrides); uploadResults.Add(uploadResult); })); } await Task.WhenAll(tasks); return(uploadResults.ToList()); }