Ejemplo n.º 1
0
        public void UndoTinify(int mediaId)
        {
            byte[] imageBytes;
            var    originImage = _imageHistoryService.Get(mediaId);

            if (originImage != null)
            {
                var mediaFile = _mediaService.GetById(mediaId) as uMedia;

                if (File.Exists(originImage.OriginFilePath))
                {
                    using (var file = new FileStream(originImage.OriginFilePath, FileMode.Open))
                        imageBytes = SolutionExtensions.ReadFully(file);

                    if (Directory.Exists(Path.GetDirectoryName(originImage.OriginFilePath)))
                    {
                        File.Delete(originImage.OriginFilePath);
                    }

                    var image = new TImage
                    {
                        Id          = mediaId.ToString(),
                        Name        = mediaFile.Name,
                        AbsoluteUrl = SolutionExtensions.GetAbsoluteUrl(mediaFile)
                    };

                    // update physical file
                    UpdateMedia(image, imageBytes);
                    // update umbraco media attributes
                    _imageRepository.Update(mediaId, imageBytes.Length);
                    _historyService.Delete(mediaId.ToString());
                    // update statistic
                    _statisticService.UpdateStatistic();
                    //delete image history
                    _imageHistoryService.Delete(mediaId);
                }
            }
            else
            {
                throw new UndoTinifierException("Image not optimized or Undo tinify not enabled");
            }
        }
        private byte[] GetImageBytesFromPath(TImage tImage, IFileSystem fs, string path)
        {
            byte[] imageBytes;
            var    fileSystem = _fileSystemProviderRepository.GetFileSystem();

            if (fileSystem != null)
            {
                if (fileSystem.Type.Contains("PhysicalFileSystem"))
                {
                    path = fs.GetRelativePath(tImage.AbsoluteUrl);
                }
            }

            using (var file = fs.OpenFile(path))
            {
                imageBytes = SolutionExtensions.ReadFully(file);
            }

            return(imageBytes);
        }