コード例 #1
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 }));
        }