Exemple #1
0
        /// <summary>
        /// Fetch a thumbnail for the file at the specified 'path'.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="size"></param>
        /// <param name="rotate"></param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> ThumbAsync(string path, ThumbSize size = ThumbSize.Small, int rotate = 0)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException($"Argument cannot be null, empty or whitespace.", nameof(path));
            }

            _logger.LogDebug($"Making HTTP request to fetch a thumbnail");
            if (String.IsNullOrWhiteSpace(_sid))
            {
                await AuthenticateAsync();
            }
            var query = new Dictionary <string, string>()
            {
                { "api", "SYNO.FileStation.Thumb" },
                { "version", "2" },
                { "method", "get" },
                { "_sid", _sid },
                { "size", size.GetValue() },
                { "rotate", $"{rotate}" },
                { "path", path },
            };

            var form = new FormUrlEncodedContent(query);

            return(await _client.PostAsync(Path("entry"), form));
        }
Exemple #2
0
    /// <summary>
    /// Given a particular image, calculates the path and filename of the associated
    /// thumbnail for that image and size.
    /// TODO: Use the Thumbnail Last gen date here to avoid passing back images with no thumbs?
    /// </summary>
    /// <param name="imageFile"></param>
    /// <param name="size"></param>
    /// <returns></returns>
    public string GetThumbPath(FileInfo imageFile, ThumbSize size)
    {
        string thumbPath;

        if (Synology)
        {
            // Syno thumbs go in a subdir of the location of the image
            string thumbFileName = $"SYNOPHOTO_THUMB_{GetSizePostFix(size).ToUpper()}.jpg";
            thumbPath = Path.Combine(imageFile.DirectoryName, "@eaDir", imageFile.Name, thumbFileName);
        }
        else
        {
            string extension = imageFile.Extension;

            // Keep the extension if it's JPG, but otherwise change it to JPG (for HEIC etc).
            if (!extension.Equals(".JPG", StringComparison.OrdinalIgnoreCase))
            {
                extension = ".JPG";
            }

            string baseName      = Path.GetFileNameWithoutExtension(imageFile.Name);
            string relativePath  = imageFile.DirectoryName.MakePathRelativeTo(PicturesRoot);
            string thumbFileName = $"{baseName}_{GetSizePostFix(size)}{extension}";
            thumbPath = Path.Combine(_thumbnailRootFolder, relativePath, thumbFileName);
        }

        return(thumbPath);
    }
Exemple #3
0
        public static string GetImageThumbUrl(Image image, ThumbSize size)
        {
            string url = "/no-image.jpg";

            if (image.Folder != null)
            {
                var file = new FileInfo(image.FullPath);

                var path = ThumbnailService.Instance.GetThumbRequestPath(file, size, "/no-image.png");

                // This is a bit tricky. We need to UrlEncode all of the folders in the path
                // but we don't want to UrlEncode the slashes itself. So we have to split,
                // UrlEncode them all, and rejoin.
                //var parts = path.Split(Path.DirectorySeparatorChar)
                //                .Select(x => HttpUtility.UrlEncode(x) );
                // path = string.Join(Path.DirectorySeparatorChar, parts);

                url = path.Replace("#", "%23");
            }
            else
            {
                Logging.Log("ERROR: No folder for image {0}", image.FileName);
            }

            return(url);
        }
Exemple #4
0
        public string StoreThumb(string filePath, ThumbSize size, string extension = null)
        {
            int    maxWidth  = (int)size;
            int    maxHeight = (int)size;
            string fileName  = FileUtils.GetThumbPath(filePath, size, extension);

            if (!File.Exists(filePath))
            {
                return(fileName);
            }

            FileInfo inf = new FileInfo(filePath);

            string newPath = Path.Combine(inf.Directory.FullName, fileName);

            Utils.CreateFolderForFile(newPath);

            if (File.Exists(newPath))
            {
                return(fileName);
            }

            Image  current = Image.FromFile(filePath);
            Bitmap canvas  = ResizeImage(current, maxWidth, maxHeight);

            canvas.Save(newPath);
            return(FileUtils.GetThumbUrl(filePath, size));
        }
 public static string StoreThumb(string path, ThumbSize size, bool relative = true)
 {
     using (var s = new ImagesService())
     {
         path = relative ? FileUtils.RelativeToAbsolute(path) : path;
         return(s.StoreThumb(path, size));
     }
 }
Exemple #6
0
        public async Task <IActionResult> ThumbnailAsync(string path, ThumbSize size = ThumbSize.Small, int rotate = 0)
        {
            var response = await _fileStation.ThumbAsync(path, size, rotate);

            var fileName = response.Content.Headers.ContentDisposition?.FileName ?? Path.GetFileName(path);
            var data     = await response.Content.ReadAsStreamAsync();

            return(File(data, response.Content.Headers.ContentType.MediaType, fileName));
        }
Exemple #7
0
 /// <summary>
 /// 指定したサイズの解像度のサムネイルを取得する
 /// </summary>
 /// <param name="size"></param>
 /// <returns></returns>
 public string GetSpecifiedThumbnail(ThumbSize size)
 {
     return(size switch
     {
         ThumbSize.Large => this.large ?? this.GetSpecifiedThumbnail(ThumbSize.Middle),
         ThumbSize.Middle => this.middle ?? this.GetSpecifiedThumbnail(ThumbSize.Normal),
         ThumbSize.Normal => this.normal ?? this.GetSpecifiedThumbnail(ThumbSize.Player),
         _ => this.player ?? throw new InvalidOperationException($"解像度{ThumbSize.Player}がnullです。")
     });
        public static string GetThumbPath(string path, ThumbSize size, string extension = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            FileInfo inf = new FileInfo(path);

            return(size.ToString().ToLower() + "\\" + inf.Name.GetBeforeLast(".") + (extension ?? inf.Extension));
        }
Exemple #9
0
 private string GetSizePostFix(ThumbSize size)
 {
     return(size switch
     {
         ThumbSize.ExtraLarge => "xl",
         ThumbSize.Large => "l",
         ThumbSize.Big => "b",
         ThumbSize.Medium => "m",
         ThumbSize.Small => "s",
         _ => "PREVIEW",
     });
Exemple #10
0
        /// <summary>
        /// Convert a thumbnail path into a URL request path (i.e., relative to wwwroot).
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="size"></param>
        /// <param name="pathIfNotExist"></param>
        /// <returns></returns>
        public string GetThumbRequestPath(FileInfo imageFile, ThumbSize size, string pathIfNotExist)
        {
            string localPath = GetThumbPath(imageFile, size);

            if (!string.IsNullOrEmpty(pathIfNotExist) && !File.Exists(localPath))
            {
                return(pathIfNotExist);
            }

            return(ConvertToRequestPath(localPath));
        }
Exemple #11
0
        private static void SetQualityParams(ThumbQuality quality_)
        {
            switch (quality_)
            {
            case ThumbQuality.fastest:
                _currentCompositingQuality = CompositingQuality.HighSpeed;
                _currentInterpolationMode  = InterpolationMode.NearestNeighbor;
                _currentSmoothingMode      = SmoothingMode.None;
                _currentThumbSize          = ThumbSize.small;
                _currentLargeThumbSize     = LargeThumbSize.small;
                break;

            case ThumbQuality.fast:
                _currentCompositingQuality = CompositingQuality.HighSpeed;
                _currentInterpolationMode  = InterpolationMode.Low;
                _currentSmoothingMode      = SmoothingMode.HighSpeed;
                _currentThumbSize          = ThumbSize.small;
                _currentLargeThumbSize     = LargeThumbSize.small;
                break;

            case ThumbQuality.higher:
                _currentCompositingQuality = CompositingQuality.AssumeLinear;
                _currentInterpolationMode  = InterpolationMode.High;
                _currentSmoothingMode      = SmoothingMode.HighQuality;
                _currentThumbSize          = ThumbSize.average;
                _currentLargeThumbSize     = LargeThumbSize.average;
                break;

            case ThumbQuality.highest:
                _currentCompositingQuality = CompositingQuality.HighQuality;
                _currentInterpolationMode  = InterpolationMode.HighQualityBicubic;
                _currentSmoothingMode      = SmoothingMode.HighQuality;
                _currentThumbSize          = ThumbSize.large;
                _currentLargeThumbSize     = LargeThumbSize.large;
                break;

            case ThumbQuality.uhd:
                _currentCompositingQuality = CompositingQuality.HighQuality;
                _currentInterpolationMode  = InterpolationMode.HighQualityBicubic;
                _currentSmoothingMode      = SmoothingMode.HighQuality;
                _currentThumbSize          = ThumbSize.uhd;
                _currentLargeThumbSize     = LargeThumbSize.uhd;
                break;

            default:
                _currentCompositingQuality = CompositingQuality.Default;
                _currentInterpolationMode  = InterpolationMode.Default;
                _currentSmoothingMode      = SmoothingMode.Default;
                _currentThumbSize          = ThumbSize.average;
                _currentLargeThumbSize     = LargeThumbSize.average;
                break;
            }
        }
        public static string GetThumbUrl(string path, ThumbSize size, string extension = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            FileInfo inf  = new FileInfo(path);
            string   name = inf.Name.GetBeforeLast(".") + (extension ?? inf.Extension);
            string   url  = inf.Directory.FullName.Replace(Shell.AppRootPath + "\\", "");

            url = url.Replace(Shell.PublicRoot + "\\", "");
            return(Utils.CombineUrl(url, size.ToString().ToLower(), name));
        }
Exemple #13
0
        /// <summary>
        /// Given a particular image, calculates the path and filename of the associated
        /// thumbnail for that image and size.
        /// TODO: Use the Thumbnail Last gen date here to avoid passing back images with no thumbs?
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public string GetThumbPath(FileInfo imageFile, ThumbSize size)
        {
            string thumbPath;

            if (Synology)
            {
                // Syno thumbs go in a subdir of the location of the image
                string thumbFileName = $"SYNOPHOTO_THUMB_{GetSizePostFix(size).ToUpper()}.jpg";
                thumbPath = Path.Combine(imageFile.DirectoryName, "@eaDir", imageFile.Name, thumbFileName);
            }
            else
            {
                string extension     = Path.GetExtension(imageFile.Name);
                string baseName      = Path.GetFileNameWithoutExtension(imageFile.Name);
                string relativePath  = imageFile.DirectoryName.MakePathRelativeTo(PicturesRoot);
                string thumbFileName = $"{baseName}_{GetSizePostFix(size)}{extension}";
                thumbPath = Path.Combine(_thumbnailRootFolder, relativePath, thumbFileName);
            }

            return(thumbPath);
        }
Exemple #14
0
        public string StoreThumb(string filePath, CropPixels rect, ThumbSize size, string extension = null)
        {
            int maxWidth  = (int)size;
            int maxHeight = (int)size;

            FileInfo inf      = new FileInfo(filePath);
            string   fileName = FileUtils.GetThumbPath(filePath, size, extension);
            string   newPath  = Path.Combine(inf.Directory.FullName, fileName);

            Utils.CreateFolderForFile(newPath);

            Bitmap image   = new Bitmap(maxWidth, maxHeight);
            Image  im      = Image.FromFile(filePath);
            Bitmap cropped = CropImage(im, rect);

            float ratio = image.Width / rect.W;

            using (Graphics gr = Graphics.FromImage(image))
            {
                gr.FillRectangle(Brushes.White, 0, 0, maxWidth, maxHeight);

                float start = rect.X < 0 ? (rect.X * -1 * ratio) : 0;

                gr.CompositingQuality = CompositingQuality.HighSpeed;
                gr.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                gr.CompositingMode    = CompositingMode.SourceCopy;
                gr.DrawImage(cropped, start, 0, image.Width, image.Height);
            }
            if (File.Exists(newPath))
            {
                File.Delete(newPath);
            }
            image.Save(newPath, ImageFormat.Png);
            string th = FileUtils.GetThumbUrl(filePath, size);

            return(th);
        }
Exemple #15
0
 /// <summary>
 /// Convert a an image's path into a URL request path (i.e., relative to wwwroot).
 /// </summary>
 /// <param name="imageFile"></param>
 /// <param name="size"></param>
 /// <param name="pathIfNotExist"></param>
 /// <returns></returns>
 public string GetThumbRequestPath(Models.Image image, ThumbSize size, string pathIfNotExist)
 {
     return(GetThumbRequestPath(new FileInfo(image.FullPath), size, pathIfNotExist));
 }
Exemple #16
0
 /// <summary>
 /// URL mapped with last-updated time to ensure we always refresh the thumb
 /// when the image is updated.
 /// </summary>
 /// <param name="size"></param>
 /// <returns></returns>
 public string ThumbUrl(ThumbSize size)
 {
     return($"/thumb/{size}/{this.ImageId}?nocache={this?.LastUpdated:yyyyMMddHHmmss}");
 }
Exemple #17
0
 public ListableImage(Image image, ThumbSize size)
 {
     Image    = image;
     ThumbURL = ThumbnailService.Instance.GetThumbRequestPath(image, size, "/no-image.png");
 }
Exemple #18
0
    private static void SetQualityParams(ThumbQuality quality_)
    {
      switch (quality_)
      {
        case ThumbQuality.fastest:
          _currentCompositingQuality = CompositingQuality.HighSpeed;
          _currentInterpolationMode = InterpolationMode.NearestNeighbor;
          _currentSmoothingMode = SmoothingMode.None;
          _currentThumbSize = ThumbSize.small;
          _currentLargeThumbSize = LargeThumbSize.small;
          break;

        case ThumbQuality.fast:
          _currentCompositingQuality = CompositingQuality.HighSpeed;
          _currentInterpolationMode = InterpolationMode.Low;
          _currentSmoothingMode = SmoothingMode.HighSpeed;
          _currentThumbSize = ThumbSize.small;
          _currentLargeThumbSize = LargeThumbSize.small;
          break;

        case ThumbQuality.higher:
          _currentCompositingQuality = CompositingQuality.AssumeLinear;
          _currentInterpolationMode = InterpolationMode.High;
          _currentSmoothingMode = SmoothingMode.HighQuality;
          _currentThumbSize = ThumbSize.average;
          _currentLargeThumbSize = LargeThumbSize.average;
          break;

        case ThumbQuality.highest:
          _currentCompositingQuality = CompositingQuality.HighQuality;
          _currentInterpolationMode = InterpolationMode.HighQualityBicubic;
          _currentSmoothingMode = SmoothingMode.HighQuality;
          _currentThumbSize = ThumbSize.large;
          _currentLargeThumbSize = LargeThumbSize.large;
          break;

        case ThumbQuality.uhd:
          _currentCompositingQuality = CompositingQuality.HighQuality;
          _currentInterpolationMode = InterpolationMode.HighQualityBicubic;
          _currentSmoothingMode = SmoothingMode.HighQuality;
          _currentThumbSize = ThumbSize.uhd;
          _currentLargeThumbSize = LargeThumbSize.uhd;
          break;

        default:
          _currentCompositingQuality = CompositingQuality.Default;
          _currentInterpolationMode = InterpolationMode.Default;
          _currentSmoothingMode = SmoothingMode.Default;
          _currentThumbSize = ThumbSize.average;
          _currentLargeThumbSize = LargeThumbSize.average;
          break;
      }
    }
Exemple #19
0
 /// <summary>
 ///     Obhtiene un Thumbnail de la última imagen cargada.
 /// </summary>
 /// <param name="ts">ThumbnailSize</param>
 /// <returns>Objeto System.Drawing.Image de la última imagen cargada.</returns>
 public Image GetThumbnail(ThumbSize ts)
 {
     return this.GetImageFromUrl(String.Format("http://twitpic.com/show/{0}/{1}", (ts == ThumbSize.thumb) ? "thumb" : "mini", this.MediaId));
 }
Exemple #20
0
 public ListableImage(Image image, ThumbSize size)
 {
     Image = image;
     Size  = size;
 }
Exemple #21
0
 public string GetThumbSize()
 {
     return(ThumbSize.ToString().ToLower());
 }