private void Blur(string fileName, UploadType type, int announcementId = 0) { var newFileName = $"{Path.GetFileNameWithoutExtension(fileName)}_blur{Path.GetExtension(fileName)}"; var dest = FilesUtilities.GetRelativePath(fileName, type, announcementId); if (announcementId != 0) { dest = $"{Path.GetDirectoryName(dest)}/{Path.GetFileName(dest)}"; } var newDest = FilesUtilities.GetRelativePathBlurs(newFileName, type, announcementId); if (announcementId != 0) { newDest = $"{Path.GetDirectoryName(newDest)}/{Path.GetFileName(newDest)}"; } var theFile = Path.Combine(_webRootPath, dest); var newFile = Path.Combine(_webRootPath, newDest); var extension = Path.GetExtension(newFile); try { Path.GetDirectoryName(newFile).CreateDirectory(); using (var stream = new FileStream(newFile, FileMode.Create)) { using (var image = SixImage.Load(theFile)) { try { image.Mutate(x => x.GaussianBlur(Constants.BlurLevel).ApplyProcessors()); } catch (Exception e) { throw e; } if (string.IsNullOrEmpty(extension) || !extension.ToLower().Contains("png")) { image.SaveAsJpeg(stream); } else { image.SaveAsPng(stream); } } } } catch (Exception) { if (!File.Exists(newFile)) { return; } File.Delete(newFile); } }
public bool RemoveFile(string fileName, UploadType type, int id) { var path = Path.Combine(_webRootPath, FilesUtilities.GetRelativePath(fileName, type, id)).SlashConverter(); if (!File.Exists(path)) { return(true); } File.Delete(path); var blurPath = Path.Combine(_webRootPath, FilesUtilities.GetRelativePathBlurs( $"{Path.GetFileNameWithoutExtension(fileName)}_blur{Path.GetExtension(fileName)}", type, id)).SlashConverter(); if (!File.Exists(blurPath)) { return(true); } File.Delete(blurPath); return(true); }
public async Task <DownloadFileModel> Download(UploadType type, string fileName, int id, bool isBlur) { string resultFileName; 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); var theFile = new FileInfo(file); if (!theFile.Exists) { return(null); } new FileExtensionContentTypeProvider().TryGetContentType(fileName, out var contentType); if (string.IsNullOrEmpty(contentType)) { contentType = "image/jpg"; } if (!contentType.StartsWith("image")) { return new DownloadFileModel { Bytes = await File.ReadAllBytesAsync(file), ContentType = contentType } } ; var key = fileName.CacheKeyGenerator(type, isBlur); var fromCache = _cache.Get(key); if (fromCache != null) { return new DownloadFileModel { Bytes = fromCache, ContentType = contentType } } ; byte[] fileBytes; try { fileBytes = await File.ReadAllBytesAsync(file); } catch (IOException) { return(null); } _cache.Set(key, fileBytes); return(new DownloadFileModel { ContentType = contentType, Bytes = fileBytes }); }
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 }); }