예제 #1
0
        private void ConvertPdf(string fileName, UploadType type, int announcementId = 0)
        {
            var newFileName = $"{Path.GetFileNameWithoutExtension(fileName)}.jpg";
            var dest        = FilesUtilities.GetRelativePath(fileName, type, announcementId);

            if (announcementId != 0)
            {
                dest = $"{Path.GetDirectoryName(dest)}/{Path.GetFileName(dest)}";
            }
            var newDest = FilesUtilities.GetRelativePdfFile(newFileName, type, announcementId);
            var theFile = Path.Combine(_webRootPath, dest);
            var newFile = Path.Combine(_webRootPath, newDest);

            Path.GetDirectoryName(newFile).CreateDirectory();
            newFile = newFile.SlashConverter();
            DocumentCore      document = DocumentCore.Load(theFile);
            DocumentPaginator dp       = document.GetPaginator(new PaginatorOptions());
            var page = dp.Pages[0];

            using (var image = page.Rasterize(800, Color.White))
            {
                image.Save(newFile);
            }
        }
예제 #2
0
        public async Task <DownloadFileModel> Resize(UploadType type, string fileName, int maxWidth, int maxHeight, int id, bool isBlur)
        {
            new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var mime);
            string resultFileName;

            if (type == UploadType.AnnouncementDocument)
            {
                resultFileName = FilesUtilities.GetRelativePdfFile($"{Path.GetFileNameWithoutExtension(fileName)}.jpg", type, id);
                new FileExtensionContentTypeProvider().TryGetContentType(resultFileName, out mime);
            }
            else if (isBlur)
            {
                resultFileName = FilesUtilities.GetRelativePathBlurs(
                    $"{Path.GetFileNameWithoutExtension(fileName)}_blur{Path.GetExtension(fileName)}", type, id);
            }
            else
            {
                resultFileName = FilesUtilities.GetRelativePath(fileName, type, id);
            }
            var file = Path.Combine(_webRootPath, resultFileName);

            if (mime == null)
            {
                return(null);
            }
            if (!mime.StartsWith("image"))
            {
                return new DownloadFileModel
                       {
                           ContentType = mime,
                           Bytes       = await File.ReadAllBytesAsync(file)
                       }
            }
            ;
            var theFile = new FileInfo(file);

            if (!theFile.Exists)
            {
                return(null);
            }
            var key       = fileName.CacheKeyGenerator(type, isBlur, maxWidth, maxHeight).ToString();
            var x         = key.GetType();
            var fromCache = _cache.Get(key);

            if (fromCache != null)
            {
                return new DownloadFileModel
                       {
                           Bytes       = fromCache,
                           ContentType = mime
                       }
            }
            ;
            byte[] fileBytes;
            try
            {
                using (var bitmap = new Bitmap(file))
                {
                    int width;
                    int height;
                    if (bitmap.Width > bitmap.Height)
                    {
                        width  = maxWidth;
                        height = Convert.ToInt32(bitmap.Height * maxWidth / (double)bitmap.Width);
                    }
                    else
                    {
                        width  = Convert.ToInt32(bitmap.Width * maxHeight / (double)bitmap.Height);
                        height = maxHeight;
                    }
                    var newBitmap = new Bitmap(bitmap, new Size(width, height));
                    fileBytes = newBitmap.ToByteArray(Path.GetExtension(resultFileName));
                    newBitmap.Dispose();
                }
            }
            catch (IOException)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(mime))
            {
                mime = "image/jpg";
            }
            _cache.Set(key, fileBytes);
            return(new DownloadFileModel
            {
                Bytes = fileBytes,
                ContentType = mime
            });
        }