Ejemplo n.º 1
0
        public async Task <UploadResult> CropFile(
            ImageProcessingOptions options,
            string sourceFilePath,
            int offsetX,
            int offsetY,
            int widthToCrop,
            int heightToCrop,
            int finalWidth,
            int finalHeight
            )
        {
            if (string.IsNullOrWhiteSpace(sourceFilePath))
            {
                _log.LogError($"sourceFilePath not provided for crop");
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            await EnsureProjectSettings().ConfigureAwait(false);

            string currentFsPath      = _rootPath.RootFileSystemPath;
            string currentVirtualPath = _rootPath.RootVirtualPath;

            string[] virtualSegments = options.ImageDefaultVirtualSubPath.Split('/');

            if (!sourceFilePath.StartsWith(_rootPath.RootVirtualPath))
            {
                _log.LogError($"{sourceFilePath} not a sub path of root path {_rootPath.RootVirtualPath}");
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            var fileToCropName = Path.GetFileName(sourceFilePath);
            var fileToCropNameWithooutExtenstion = Path.GetFileNameWithoutExtension(sourceFilePath);
            var ext      = Path.GetExtension(sourceFilePath);
            var mimeType = GetMimeType(ext);
            var isImage  = IsWebImageFile(ext);

            if (!isImage)
            {
                _log.LogError($"{sourceFilePath} is not not an image file");
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            var fileToCropFolderVPath = sourceFilePath.Replace(fileToCropName, "");

            var virtualSubPath = fileToCropFolderVPath.Substring(_rootPath.RootVirtualPath.Length);
            var segments       = virtualSubPath.Split('/');

            if (segments.Length <= 0)
            {
                _log.LogError($"{sourceFilePath} not found");
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            var requestedFsPath = Path.Combine(_rootPath.RootFileSystemPath, Path.Combine(segments));

            if (!Directory.Exists(requestedFsPath))
            {
                _log.LogError("directory not found for currentPath " + requestedFsPath);
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            currentVirtualPath = virtualSubPath;
            virtualSegments    = segments;
            currentFsPath      = Path.Combine(currentFsPath, Path.Combine(virtualSegments));
            var sourceFsPath      = Path.Combine(currentFsPath, fileToCropName);
            var cropNameSegment   = "-crop";
            int previousCropCount = 0;
            var targetFsPath      = Path.Combine(currentFsPath, fileToCropNameWithooutExtenstion + cropNameSegment + ext);

            while (File.Exists(targetFsPath))
            {
                previousCropCount += 1;
                targetFsPath       = Path.Combine(currentFsPath, fileToCropNameWithooutExtenstion + cropNameSegment + previousCropCount.ToString() + ext);
            }
            ;

            var didCrop = _imageResizer.CropExistingImage(
                sourceFsPath,
                targetFsPath,
                offsetX,
                offsetY,
                widthToCrop,
                heightToCrop,
                finalWidth,
                finalHeight
                );

            if (!didCrop)
            {
                _log.LogError($"failed to crop image {requestedFsPath}");
                return(new UploadResult
                {
                    ErrorMessage = _sr["There was an error logged during file processing"]
                });
            }

            var result = new UploadResult
            {
                OriginalUrl = sourceFilePath,
                ResizedUrl  = currentVirtualPath + Path.GetFileName(targetFsPath)
            };

            //var list = new List<UploadResult>()
            //{
            //    result
            //};

            //foreach(var handler in _uploadHandlers)
            //{
            //    try
            //    {
            //        await handler.Handle(list);
            //    }
            //    catch(Exception ex)
            //    {
            //        _log.LogError($"{ex.Message}-{ex.StackTrace}");
            //    }
            //}


            return(result);
        }