public virtual CachedImageResult GetCachedImage(int? pictureId, string seoFileName, string extension, object settings = null) { var imagePath = this.GetCachedImagePath(pictureId, seoFileName, extension, ImageResizerUtils.CreateResizeSettings(settings)); var localPath = this.GetImageLocalPath(imagePath); var result = new CachedImageResult { Path = imagePath, //"Media/Thumbs/" + imagePath, LocalPath = localPath, FileName = Path.GetFileName(imagePath), Extension = GetCleanFileExtension(imagePath), Exists = File.Exists(localPath) }; return result; }
public virtual CachedImageResult Get(IFile file, ProcessImageQuery query) { Guard.NotNull(file, nameof(file)); Guard.NotNull(query, nameof(query)); var imagePath = GetCachedImagePath(file, query); var thumbFile = _fileSystem.GetFile(BuildPath(imagePath)); var result = new CachedImageResult(thumbFile) { Path = imagePath, Extension = file.Extension.TrimStart('.'), IsRemote = _fileSystem.IsCloudStorage }; if (file.Exists && file.Size <= 0) { result.Exists = false; } return(result); }
public byte[] ProcessAndAddImageToCache(CachedImageResult cachedImage, byte[] source, int targetSize) { byte[] result; if (targetSize == 0) { AddImageToCache(cachedImage, source); result = source; } else { var sourceStream = new MemoryStream(source); using (var resultStream = _imageResizerService.ResizeImage(sourceStream, targetSize, targetSize, _mediaSettings.DefaultImageQuality)) { result = resultStream.GetBuffer(); AddImageToCache(cachedImage, result); } } cachedImage.Exists = true; return(result); }
public virtual CachedImageResult Get(int?pictureId, string seoFileName, string extension, ProcessImageQuery query = null) { Guard.NotEmpty(extension, nameof(extension)); extension = query?.GetResultExtension() ?? extension.TrimStart('.').ToLower(); var imagePath = GetCachedImagePath(pictureId, seoFileName, extension, query); var file = _fileSystem.GetFile(BuildPath(imagePath)); var result = new CachedImageResult(file) { Path = imagePath, Extension = extension, IsRemote = _fileSystem.IsCloudStorage }; if (file.Exists && file.Size <= 0) { result.Exists = false; } return(result); }
private bool PrepareAddImageToCache(CachedImageResult cachedImage, byte[] buffer) { Guard.NotNull(cachedImage, nameof(cachedImage)); if (buffer == null || buffer.Length == 0) { return(false); } if (cachedImage.Exists) { _fileSystem.DeleteFile(BuildPath(cachedImage.Path)); } // create folder if needed string imageDir = System.IO.Path.GetDirectoryName(cachedImage.Path); if (imageDir.HasValue()) { _fileSystem.TryCreateFolder(BuildPath(imageDir)); } return(true); }
public void AddImageToCache(CachedImageResult cachedImage, byte[] buffer) { Guard.ArgumentNotNull(() => cachedImage); if (buffer == null || buffer.Length == 0) { throw new ArgumentException("The image buffer cannot be empty.", "buffer"); } if (cachedImage.Exists) { File.Delete(cachedImage.LocalPath); } // create directory if necessary string imageDir = Path.GetDirectoryName(cachedImage.LocalPath); if (imageDir.HasValue() && !System.IO.Directory.Exists(imageDir)) { System.IO.Directory.CreateDirectory(imageDir); } // save file File.WriteAllBytes(cachedImage.LocalPath, buffer); }
public virtual Stream OpenCachedImage(CachedImageResult cachedImage) { Guard.NotNull(cachedImage, nameof(cachedImage)); return(_fileSystem.GetFile(BuildPath(cachedImage.Path)).OpenRead()); }