Exemplo n.º 1
0
 private HttpResponseMessage DecodedFileResponse(ImageDecoder image, File file)
 {
     return(new HttpResponseMessage(HttpStatusCode.OK)
     {
         Content = new PushStreamContent(async(outputStream, content, context) =>
         {
             using (outputStream)
                 using (image.ImageStream)
                 {
                     await
                     image.DecodeAsync(new Directory[0], new[] { file }, new FileDataOutputHandler(async(directory, _file, stream) => await stream.CopyToAsync(outputStream)));
                 }
         })
         {
             Headers =
             {
                 ContentType        = new MediaTypeHeaderValue("application/octet-stream"),
                 ContentDisposition = new ContentDispositionHeaderValue("attachment")
                 {
                     FileName       = file.Name,
                     Size           = file.Length
                 }
             }
         }
     });
 }
Exemplo n.º 2
0
 private static HttpResponseMessage DecodedDataAsZipFileResponse(string fileName, ImageDecoder image, IList <Directory> directories, IList <File> files)
 {
     return(new HttpResponseMessage(HttpStatusCode.OK)
     {
         Content = new PushStreamContent(async(outputStream, content, context) =>
         {
             ZipOutputStream zipStream = new ZipOutputStream(outputStream);
             try
             {
                 await image.DecodeAsync(directories, files,
                                         new FileDataOutputHandler(
                                             (directory) =>
                 {
                     if (!directory.IsRootDirectory)
                     {
                         zipStream.PutNextEntry(directory.Path);
                     }
                 },
                                             async(directory, file, stream) =>
                 {
                     zipStream.PutNextEntry(Path.Combine(directory.Path, file.Name));
                     await stream.CopyToAsync(zipStream);
                 }));
             }
             finally
             {
                 image.ImageStream.Close();
                 zipStream.Close();
             }
         })
         {
             Headers =
             {
                 ContentType        = new MediaTypeHeaderValue("application/octet-stream"),
                 ContentDisposition = new ContentDispositionHeaderValue("attachment")
                 {
                     FileName       = fileName
                 }
             }
         }
     });
 }
Exemplo n.º 3
0
        private async Task <ImageDescription> AddImageAsync(Stream imageData, string fileNameBase, bool isCover, bool checkCoverAR)
        {
            var memStream = default(MemoryStream);
            var srcStream = imageData;

            if (!imageData.CanSeek)
            {
                memStream = new MemoryStream();
                await imageData.CopyToAsync(memStream).ConfigureAwait(false);

                memStream.Position = 0;
                srcStream          = memStream;
            }

            var imageInfo = await ImageDecoder.DecodeAsync(srcStream).ConfigureAwait(false);

            srcStream.Position = 0;
            if (imageInfo == null)
            {
                throw new FormatException("Image data is of invalid or not recognized format");
            }

            const float coverARLimit = 3.0f / 4.0f;

            if (isCover && checkCoverAR)
            {
                if (((float)imageInfo.Width / (float)imageInfo.Height) > coverARLimit)
                {
                    throw new FormatException("Cover has unsuitable aspect ratio");
                }
            }

            var properties = isCover ? "cover-image" : null;
            var imageItem  = new ImageDescription($"i_{fileNameBase}", $"{fileNameBase}{imageInfo.Extension}", imageInfo.Width, imageInfo.Height, imageInfo.MimeType, properties);

            await AddBinaryEntryAsync($"{Strings.EpubContentRoot}{imageItem.Path}", srcStream).ConfigureAwait(false);

            memStream?.Dispose();
            Contents.Add(imageItem);
            return(imageItem);
        }