private async Task <bool> CreatePrepressPackageAsync(string url, ArtPackageResource package) { using (var client = GetClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeJson)); var response = await client.PostAsJsonAsync(url, package); return(response.IsSuccessStatusCode); } }
/// <summary> /// This creates an artwork package for an order and uploads the artwork files /// </summary> /// <param name="orderId">The Order for which the artwork is being uploaded for</param> /// <param name="hubId">The Hub for which the order is registered to</param> /// <param name="files">A list of files to be uploaded</param> /// <returns>An asynchronous task with a void type</returns> public async Task UploadArtworkAsync(string orderId, string hubId, IEnumerable <string> files) { //connect to the prepress api and upload the artwork files var uploadedFileUrls = new List <FileUploadResource>(); var discovery = await DiscoverPrepressAsync(); var uploadUrl = discovery.UploadUrl; var packageUrl = discovery.PackageUrl; foreach (var file in files) { //foreach file // create an upload resource var mimeType = MimeMapping.GetMimeMapping(file); using (var stream = File.OpenRead(file)) { var resource = await CreateFileResourceAsync(uploadUrl, orderId, Path.GetFileName(file), stream.Length, mimeType); uploadedFileUrls.Add(resource); var md5 = MD5.Create(); var cs = new CryptoStream(new MemoryStream(), md5, CryptoStreamMode.Write); var fileLink = resource.Links.FindLinkByRelation(PrepressApiDiscovery.Self); var chunkLink = resource.Links.FindLinkByRelation(PrepressApiDiscovery.ChunksRelation); var contentLink = resource.Links.FindLinkByRelation(PrepressApiDiscovery.UploadContentRelation); if (fileLink == null || chunkLink == null || contentLink == null) { throw new ApplicationException("Links not found"); } byte[] buffer = new byte[FileChunkSize]; int bytesRead; while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) { var chunk = new byte[bytesRead]; Buffer.BlockCopy(buffer, 0, chunk, 0, bytesRead); await CreateChunkResourceAsync(chunkLink.Uri, chunk); cs.Write(buffer, 0, bytesRead); } cs.FlushFinalBlock(); //patch MD5 await PatchMD5(fileLink.Uri, BitConverter.ToString(md5.Hash).Replace("-", string.Empty).ToLower()); } } //create prepress order resource(attaching files uploaded) var pkg = new ArtPackageResource { HubId = hubId, OrderId = orderId, Files = uploadedFileUrls.Select(x => new { ContentType = x.ContentType,// "",//files.SingleOrDefault(y => Path.GetFileName(y) == x.Filename)?.MimeType, Name = x.Filename, Url = x.Links.FindLinkByRelation(PrepressApiDiscovery.UploadContentRelation)?.Uri }).ToArray() }; await CreatePrepressPackageAsync(packageUrl, pkg); }