diff --git a/.gitignore b/.gitignore index 9e142a0..ce064ce 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /formwidgets/responsiveimage/assets/images/ /formwidgets/responsiveimage/assets/mix-manifest.json /formwidgets/responsiveimage/assets/node_modules/ +*.swp diff --git a/classes/CompressJob.php b/classes/CompressJob.php new file mode 100644 index 0000000..492370f --- /dev/null +++ b/classes/CompressJob.php @@ -0,0 +1,85 @@ +path = $data['path']; + $this->filename = $data['filename']; + $this->disk = $data['disk']; + $this->strategy = $data['strategy']; + $this->crop = $data['crop']; + + $this->sizes = Setting::get('srcx'); + + $this->crop(); + $this->createVersions(); + } + + public function crop() { + if ($this->crop === null) { + return; + } + + $fullPath = Storage::disk($this->disk)->path($this->path.'/'.$this->filename); + + Storage::disk($this->disk)->makeDirectory($this->getStrategy()->croppedPath()); + + $r = Resizer::open($fullPath); + + $r->crop(floor($this->crop['x']), floor($this->crop['y']), floor($this->crop['w']), floor($this->crop['h'])); + + $this->path = $this->getStrategy()->croppedPath($this->filename); + $this->filename = $this->getStrategy()->croppedFilename($this->filename, $this->crop); + + $r->save(Storage::disk($this->disk)->path($this->path.'/'.$this->filename)); + } + + public function createVersions() + { + $fullPath = Storage::disk($this->disk)->path($this->path.'/'.$this->filename); + [ $width, $height ] = getimagesize($fullPath); + + if ($width > $this->maxFilesize) { + $r = Resizer::open($fullPath); + $r->resize($this->maxFilesize, 0); + $r->save($fullPath); + } + + [ $width, $height ] = getimagesize($fullPath); + + Storage::disk($this->disk)->makeDirectory($this->getStrategy()->publicPath($this->filename)); + + foreach ($this->sizes as $w) { + + $filename = $this->getStrategy()->smallFilename($this->filename, $w); + + if ($width < $w) { + continue; + } + + $destination = Storage::disk($this->disk)->path($this->getStrategy()->publicPath($this->filename).'/'.$this->getStrategy()->smallFilename($this->filename, $w)); + + $r = Resizer::open($fullPath); + $r->resize($w, 0); + $r->save($destination); + } + } + + private function getStrategy() { + return app($this->strategy); + } + +} diff --git a/classes/FirstLetterStrategy.php b/classes/FirstLetterStrategy.php new file mode 100644 index 0000000..d2f30bf --- /dev/null +++ b/classes/FirstLetterStrategy.php @@ -0,0 +1,31 @@ +file = $uploadedFile; + $this->data = $data; + + $fileName = $this->getStrategy()->sourceFileBasename($this->file, $this->data); + $sourcePath = $this->getStrategy()->sourcePath($fileName); + $fileName = $this->transformFileName($sourcePath, $fileName).'.'.$this->file->getClientOriginalExtension(); + + $uploadedFile->storeAs($sourcePath, $fileName, $this->disk); + + Queue::push(CompressJob::class, [ + 'path' => $sourcePath, + 'filename' => $fileName, + 'disk' => $this->disk, + 'strategy' => $this->strategy, + 'crop' => $this->data['crop'] + ]); + + return $fileName; + } + + private function storage() { + return Storage::disk($this->disk); + } + + private function transformFileName($path, $basename) { + if (count(glob($this->storage()->path($path).'/'.$basename.'*')) == 0) { + return $basename; + } + + $i = 1; + + while(count(glob($this->storage()->path($path).'/'.$basename.'-'.$i.'*')) != 0) { + $i++; + } + + return $basename.'-'.$i; + } + + private function originalExtension() { + return $this->file->getClientOriginalExtension(); + } + + private function getStrategy() { + return app($this->strategy); + } + +} diff --git a/formwidgets/.Responsiveimage.php.swp b/formwidgets/.Responsiveimage.php.swp deleted file mode 100644 index 14d77f9..0000000 Binary files a/formwidgets/.Responsiveimage.php.swp and /dev/null differ diff --git a/formwidgets/Responsiveimage.php b/formwidgets/Responsiveimage.php index 03dcb02..710a334 100644 --- a/formwidgets/Responsiveimage.php +++ b/formwidgets/Responsiveimage.php @@ -14,6 +14,7 @@ use ValidationException; use Exception; use Aweos\Resizer\Traits\ResponsiveSaver; use Aweos\Resizer\Traits\ResponsiveWidget; +use Aweos\Resizer\Classes\UploadStorage; /** * File upload field @@ -230,24 +231,7 @@ class Responsiveimage extends FormWidgetBase */ public function onRemoveAttachment() { - $fileModel = $this->getRelationModel(); - if (($fileId = post('file_id')) && ($file = $fileModel::find($fileId))) { - $this->getRelationObject()->remove($file, $this->sessionKey); - } - } - - /** - * Sorts file attachments. - */ - public function onSortAttachments() - { - if ($sortData = post('sortOrder')) { - $ids = array_keys($sortData); - $orders = array_values($sortData); - - $fileModel = $this->getRelationModel(); - $fileModel->setSortableOrder($ids, $orders); - } + // } /** @@ -255,20 +239,7 @@ class Responsiveimage extends FormWidgetBase */ public function onLoadAttachmentConfig() { - $fileModel = $this->getRelationModel(); - if ($file = $this->getFileRecord()) { - $file = $this->decorateFileAttributes($file); - - $this->vars['file'] = $file; - $this->vars['displayMode'] = $this->getDisplayMode(); - $this->vars['cssDimensions'] = $this->getCssDimensions(); - $this->vars['relationManageId'] = post('manage_id'); - $this->vars['relationField'] = post('_relation_field'); - - return $this->makePartial('config_form'); - } - - throw new ApplicationException('Unable to find file, it may no longer exist'); + // } /** @@ -318,70 +289,20 @@ class Responsiveimage extends FormWidgetBase */ public function onUpload() { - try { - if (!Input::hasFile('file_data')) { - throw new ApplicationException('File missing from request'); - } - - $fileModel = $this->getRelationModel(); - $uploadedFile = Input::file('file_data'); - - $validationRules = ['max:'.$fileModel::getMaxFilesize()]; - if ($fileTypes = $this->getAcceptedFileTypes()) { - $validationRules[] = 'extensions:'.$fileTypes; - } - - if ($this->mimeTypes) { - $validationRules[] = 'mimes:'.$this->mimeTypes; - } - - $validation = Validator::make( - ['file_data' => $uploadedFile], - ['file_data' => $validationRules] - ); - - if ($validation->fails()) { - throw new ValidationException($validation); - } - - if (!$uploadedFile->isValid()) { - throw new ApplicationException('File is not valid'); - } - - $fileRelation = $this->getRelationObject(); - - $file = $fileModel; - $file->data = $uploadedFile; - $file->is_public = $fileRelation->isPublic(); - $file->save(); - - /** - * Attach directly to the parent model if it exists and attachOnUpload has been set to true - * else attach via deferred binding - */ - $parent = $fileRelation->getParent(); - if ($this->attachOnUpload && $parent && $parent->exists) { - $fileRelation->add($file); - } - else { - $fileRelation->add($file, $this->sessionKey); - } - - $file = $this->decorateFileAttributes($file); - - $result = [ - 'id' => $file->id, - 'thumb' => $file->thumbUrl, - 'path' => $file->pathUrl - ]; - - $response = Response::make($result, 200); - } - catch (Exception $ex) { - $response = Response::make($ex->getMessage(), 400); + if (!Input::hasFile('file_data')) { + throw new ApplicationException('File missing from request'); } - return $response; + $data = json_decode(Input::get('extraData'), true); + $uploadedFile = Input::file('file_data'); + + if (!$uploadedFile->isValid()) { + throw new ApplicationException('File is not valid'); + } + + app(UploadStorage::class)->storeFileFromUpload($uploadedFile, $data); + + return Response::make('', 200); } /** diff --git a/formwidgets/responsiveimage/assets/src/App.vue b/formwidgets/responsiveimage/assets/src/App.vue index 15c6567..c304758 100644 --- a/formwidgets/responsiveimage/assets/src/App.vue +++ b/formwidgets/responsiveimage/assets/src/App.vue @@ -9,7 +9,7 @@ - +
@@ -77,6 +77,7 @@ export default { data: function() { return { + extraData: {}, addVisible: true, content: { sections: [], @@ -110,7 +111,7 @@ export default { dropzoneOptions() { return { paramName: 'file_data', - autoQueue: false, + autoProcessQueue: false, url: window.location, clickable: this.$refs.addQueueTrigger, previewsContainer: this.$refs.previewsContainer, @@ -147,14 +148,16 @@ export default { this.addVisible = false; this.showPopup(file).then((ret) => { - console.log(ret); + this.extraData[file.name] = ret; + this.$refs.dropzone.processQueue(); + console.log('AAA'); }).catch((err) => { this.$refs.dropzone.removeFile(file); this.addVisible = true; }); }, - upload() { - + onSend(file, xhr, formData) { + formData.append('extraData', JSON.stringify(this.extraData[file.name])); } }, mounted() { diff --git a/formwidgets/responsiveimage/partials/._responsiveimage.htm.swp b/formwidgets/responsiveimage/partials/._responsiveimage.htm.swp deleted file mode 100644 index 46aa16e..0000000 Binary files a/formwidgets/responsiveimage/partials/._responsiveimage.htm.swp and /dev/null differ