示例#1
0
 public ActionResult Get(string path, int depth = 1)
 {
     if (string.IsNullOrEmpty(path))
     {
         var folders = _filesData.GetTopLevelFolders(depth, Math.Max(depth - 1, 0));
         return(Ok(new DirectoryDto()
         {
             Name = "",
             Path = _pathHelper.PathToUrl(_options.Value.OriginalsLogicalPrefix),
             ApiPath = _pathHelper.PathToUrl(_pathHelper.GetApiPath(_options.Value.OriginalsLogicalPrefix)),
             HasSubdirectories = folders.Any(),
             Children = depth == 0 ? null : folders.OrderBy(x => x.Name).Select(x => MapToDto(x, depth - 1)).ToList()
         }));
     }
     else
     {
         var folder = _filesData.GetFolder(_pathHelper.JoinLogicalPaths(_options.Value.OriginalsLogicalPrefix, path), depth + 1, depth);
         if (folder != null)
         {
             return(Ok(MapToDto(folder, depth)));
         }
         var file = _filesData.GetFile(_pathHelper.JoinLogicalPaths(_options.Value.OriginalsLogicalPrefix, path));
         if (file != null)
         {
             return(Ok(MapToDto(file)));
         }
         return(NotFound());
     }
 }
示例#2
0
        public void CreateOrUpdateThumbnail()
        {
            var originalPhysicalPath  = _pathHelper.GetPhysicalPath(LogicalPath);
            var thumbnailPath         = _pathHelper.GetThumbnailPath(LogicalPath, true);
            var thumbnailPhysicalPath = _pathHelper.GetPhysicalPath(thumbnailPath);

            var file          = _filesData.GetFile(LogicalPath) ?? new Entities.File();
            var fileTimestamp = File.GetLastWriteTime(originalPhysicalPath);

            file.ModificationTimestamp = new DateTime(fileTimestamp.Year, fileTimestamp.Month, fileTimestamp.Day, fileTimestamp.Hour, fileTimestamp.Minute, fileTimestamp.Second);
            file.Name          = _pathHelper.GetName(LogicalPath);
            file.Path          = LogicalPath;
            file.ThumbnailPath = thumbnailPath;

            try
            {
                using (var image = Image.Load(originalPhysicalPath))
                {
                    file.Width  = image.Width;
                    file.Height = image.Height;
                    file.OriginalCreationTime = GetOriginalCreationDate(image.MetaData);
                    var desiredHeight = 200;
                    var desiredWidth  = image.Width * desiredHeight / image.Height;
                    image.Mutate(x => x.Resize(desiredWidth, desiredHeight));
                    image.Save(thumbnailPhysicalPath, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder());
                }
            }
            catch (Exception e)
            {
                throw new ImageProcessingException("An error occurred while processing image file.", e);
            }

            if (file.Id == 0)
            {
                file.FolderId = _filesData.GetFolder(_pathHelper.GetParentPath(LogicalPath)).Id;
                _filesData.Add(file);
            }
            _filesData.SaveChanges();
        }