public Response.IResponse Execute(CommandArgs args)
        {
            var pa = args.As <tmbArgs>();

            if (pa.targets == null || pa.targets.Length == 0)
            {
                return(new Response.ErrorResponse("target(s) not specified"));
            }

            string thumbsDestPath = _config.LocalFSThumbsDirectoryPath;             //TODO: this directory CAN'T be set in CONFIG!

            if (string.IsNullOrWhiteSpace(thumbsDestPath))
            {
                return(new Response.ErrorResponse("thumbs target directory not specified in config"));
            }

            Dictionary <string, string> images = new Dictionary <string, string>();

            foreach (var fileHash in pa.targets)
            {
                // get volume for our target
                var vol = _volumeManager.GetByHash(fileHash);
                if (vol == null)
                {
                    return(new Response.ErrorResponse("invalid target"));
                }
                var fileToProcess = vol.GetFileByHash(fileHash);
                if (fileToProcess == null)
                {
                    return(new Response.ErrorResponse("invalid target"));
                }

                // check if we can generate thumbnail
                if (!_imageEditorService.CanGenerateThumbnail(fileToProcess.Name))
                {
                    continue;
                }

                string generatedFileName = _imageEditorService.CreateThumbnail(vol.DecodeHashToPath(fileHash), thumbsDestPath, fileHash,
                                                                               _config.ThumbsSize, false);
                if (generatedFileName != null)
                {
                    images.Add(fileHash, generatedFileName);
                }
            }

            return(new Response.TmbResponse(images));
        }
Exemplo n.º 2
0
        public Response.IResponse Execute(CommandArgs args)
        {
            var pa = args.As <resizeArgs>();

            if (string.IsNullOrWhiteSpace(pa.target))
            {
                return(new Response.ErrorResponse("target not specified"));
            }

            // get volume for our target
            var vol = _volumeManager.GetByHash(pa.target);

            if (vol == null)
            {
                return(new Response.ErrorResponse("invalid target"));
            }

            var fileToModify = vol.GetFileByHash(pa.target);

            if (fileToModify == null)
            {
                return(new Response.ErrorResponse("invalid target"));
            }

            // check if we can generate thumbnail for this file
            if (!_imageEditorService.CanGenerateThumbnail(fileToModify.Name))
            {
                return(new Response.ErrorResponse("unsupported extension"));
            }

            // check what operation we want to perform
            string sourceFilePath = vol.DecodeHashToPath(pa.target);

            switch (pa.mode)
            {
            case imageEditMode.crop:
                if (!_imageEditorService.CropImage(
                        sourceFilePath,
                        new System.Drawing.Point(pa.x ?? 0, pa.y ?? 0),
                        new System.Drawing.Size(pa.width, pa.height)))
                {
                    return(new Response.ErrorResponse("other error"));
                }
                break;

            case imageEditMode.resize:
                if (!_imageEditorService.ResizeImage(
                        sourceFilePath,
                        new System.Drawing.Size(pa.width, pa.height)))
                {
                    return(new Response.ErrorResponse("other error"));
                }
                break;

            case imageEditMode.rotate:
                if (!_imageEditorService.RotateImage(sourceFilePath, pa.degree ?? 0))
                {
                    return(new Response.ErrorResponse("other error"));
                }
                break;
            }


            // hmm since we need to recreate the thumbnail, then we need to delete existing
            vol.DeleteThumbnailFor(fileToModify);
            // and set that thumbnail is supported
            fileToModify.Thumbnail = Model.FileModel.ThumbnailIsSupported;

            return(new Response.ResizeResponse(new Model.FileModel[] { fileToModify }));
        }