// Build download request for file with its corresponding metadata private DownloadFileWithMetaDataRequest buildDownloadFileRequest(FileMetaData metadata) { if (metadata == null || metadata.id == null) { throw new KinveyException(EnumErrorCategory.ERROR_FILE, EnumErrorCode.ERROR_FILE_DOWNLOAD_MISSING_METADATA_INFORMATION, ""); } var urlParameters = new Dictionary <string, string>(); urlParameters.Add("appKey", ((KinveyClientRequestInitializer)client.RequestInitializer).AppKey); urlParameters.Add("fileID", metadata.id); DownloadFileWithMetaDataRequest downloadRequest = new DownloadFileWithMetaDataRequest(urlParameters, this.client); client.InitializeRequest(downloadRequest); //download.clientAppVersion = this.GetClientAppVersion (); downloadRequest.customRequestHeaders = this.GetCustomRequestProperties(); //TODO need more elegant approach for mime type // RestSharp.HttpHeader mime = new RestSharp.HttpHeader (); // mime.Name = "x-kinvey-content-type"; // mime.Value = "application/octet-stream"; // download.RequestHeaders.Add (mime); return(downloadRequest); }
/// <summary> /// Download the File associated with the id of the provided metadata. The file is streamed into the stream, with delegates returning either errors or the FileMetaData from Kinvey. /// </summary> /// <param name="metadata">The FileMetaData representing the file to download. This must contain an id.</param> /// <param name="content">Where the contents of the file will be streamed.</param> /// <param name="ct">[optional] The cancellation token. If cancellation is requested, an OperationCancelledException will be thrown.</param> public async Task <FileMetaData> DownloadAsync(FileMetaData metadata, Stream content, CancellationToken ct = default(CancellationToken)) { DownloadFileWithMetaDataRequest downloadRequest = buildDownloadFileRequest(metadata); ct.ThrowIfCancellationRequested(); FileMetaData fmd = await downloadRequest.ExecuteAsync(); ct.ThrowIfCancellationRequested(); await downloadRequest.downloadFileAsync(fmd, content); return(fmd); }
/// <summary> /// Download the File associated with the id of the provided metadata. The file is copied into the byte[], with delegates returning either errors or the FileMetaData from Kinvey. /// </summary> /// <param name="metadata">The FileMetaData representing the file to download. This must contain an id.</param> /// <param name="content">Content.</param> /// <param name="ct">[optional] The cancellation token. If cancellation is requested, an OperationCancelledException will be thrown.</param> public async Task <FileMetaData> DownloadAsync(FileMetaData metadata, byte[] content, CancellationToken ct = default(CancellationToken)) { DownloadFileWithMetaDataRequest downloadRequest = buildDownloadFileRequest(metadata); ct.ThrowIfCancellationRequested(); FileMetaData fmd = await downloadRequest.ExecuteAsync().ConfigureAwait(false); ct.ThrowIfCancellationRequested(); content = await downloadRequest.downloadFileBytesAsync(fmd).ConfigureAwait(false); return(fmd); }