/// <summary> /// Initiates the download of a specific file. /// </summary> /// <param name="objVer">The object that contains the file to download.</param> /// <param name="fileVer">The file to download.</param> /// <param name="token">A cancellation token for the request.</param> /// <returns>The raw response from the HTTP request.</returns> public async Task <byte[]> DownloadFileAsync(ObjVer objVer, FileVer fileVer, CancellationToken token = default(CancellationToken)) { // Sanity. if (null == objVer) { throw new ArgumentNullException(nameof(objVer)); } if (null == fileVer) { throw new ArgumentNullException(nameof(fileVer)); } // Extract the URI elements. int objectTypeId; string objectId, objectVersionId; objVer.GetUriParameters(out objectTypeId, out objectId, out objectVersionId); string fileId, fileVersionId; fileVer.GetUriParameters(out fileId, out fileVersionId); // Build up the request. var request = new RestRequest($"/REST/objects/{objectTypeId}/{objectId}/{objectVersionId}/files/{fileId}/content"); // Execute the request. var response = await this.MFWSClient.Get(request, token) .ConfigureAwait(false); // Return the content. return(response?.RawBytes); }
/// <summary> /// Initiates the download of a specific file. /// </summary> /// <param name="objVer">The object that contains the file to download.</param> /// <param name="fileVer">The file to download.</param> /// <param name="outputStream">The output stream for the response to be written to.</param> /// <param name="token">A cancellation token for the request.</param> /// <returns>The raw response from the HTTP request.</returns> public async Task DownloadFileAsync(ObjVer objVer, FileVer fileVer, System.IO.Stream outputStream, CancellationToken token = default(CancellationToken)) { // Sanity. if (null == objVer) { throw new ArgumentNullException(nameof(objVer)); } if (null == fileVer) { throw new ArgumentNullException(nameof(fileVer)); } // Extract the URI elements. int objectTypeId; string objectId, objectVersionId; objVer.GetUriParameters(out objectTypeId, out objectId, out objectVersionId); string fileId, fileVersionId; fileVer.GetUriParameters(out fileId, out fileVersionId); // Build up the request. var request = new RestRequest($"/REST/objects/{objectTypeId}/{objectId}/{objectVersionId}/files/{fileId}/content"); // Output the response to the given stream. request.ResponseWriter = (responseStream) => responseStream.CopyTo(outputStream); // Execute the request. await this.MFWSClient.Get(request, token) .ConfigureAwait(false); }
/// <summary> /// Replaces the content of a(n existing) single file. /// </summary> /// <param name="objVer">The object that the file belongs to.</param> /// <param name="fileVer">The file to replace.</param> /// <param name="filePath">The path to the file to upload in its replacement.</param> /// <param name="token">A cancellation token for the request.</param> /// <returns>The updated object version.</returns> public async Task <ExtendedObjectVersion> UploadFileAsync(ObjVer objVer, FileVer fileVer, string filePath, CancellationToken token = default(CancellationToken)) { // Sanity. if (null == objVer) { throw new ArgumentNullException(nameof(objVer)); } if (null == fileVer) { throw new ArgumentNullException(nameof(fileVer)); } try { if (false == System.IO.File.Exists(filePath)) { throw new ArgumentException("The file path must exist.", nameof(filePath)); } } catch (Exception e) { throw new ArgumentException("Could not confirm file path location.", nameof(filePath), e); } // Extract the URI elements. int objectTypeId; string objectId, objectVersionId; objVer.GetUriParameters(out objectTypeId, out objectId, out objectVersionId); string fileId, fileVersionId; fileVer.GetUriParameters(out fileId, out fileVersionId); // Build up the request. var request = new RestRequest($"/REST/objects/{objectTypeId}/{objectId}/{objectVersionId}/files/{fileId}/content.aspx"); // Add the file as a body. var fileInfo = new System.IO.FileInfo(filePath); request.AddFile(fileInfo.Name, fileInfo.FullName); // Execute the request. var response = await this.MFWSClient.Put <ExtendedObjectVersion>(request, token) .ConfigureAwait(false); // Return the data. return(response.Data); }