Example #1
0
        public TImage Convert(uModels.Media uMedia)
        {
            TImage image = new TImage()
            {
                Id          = uMedia.Id.ToString(),
                Name        = uMedia.Name,
                AbsoluteUrl = SolutionExtensions.GetAbsoluteUrl(uMedia)
            };

            if (string.IsNullOrEmpty(image.AbsoluteUrl))
            {
                image.AbsoluteUrl = _umbracoDbRepository.GetMediaAbsoluteUrl(uMedia.Id);
            }

            return(image);
        }
Example #2
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");
            }
        }