From 35d8dd8a22c18e42a4df2bce3984aee777d23545 Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 11:50:00 -0400 Subject: [PATCH 01/10] Fix hashed asset retrieval --- src/services/Compress.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/Compress.php b/src/services/Compress.php index 185201d..8da334a 100644 --- a/src/services/Compress.php +++ b/src/services/Compress.php @@ -60,8 +60,8 @@ public function getArchiveModelForQuery(AssetQuery $query, $lazy = false, $filen // Make sure we haven't already hashed these assets. If so, return the // archive. $record = $this->getArchiveRecordByHash($hash); - if ($record instanceof ArchiveRecord && $record->assetId) { - $asset = \Craft::$app->assets->getAssetById($record->id, $record->siteId); + if ($record instanceof ArchiveRecord && isset($record->assetId)) { + $asset = \Craft::$app->assets->getAssetById($record->assetId, $record->siteId); return ArchiveModel::hydrateFromRecord($record, $asset); } From 99eb48d26e88a802a922d19032011ace9ab738aa Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 14:48:50 -0400 Subject: [PATCH 02/10] Add settings for max filesize and max file count --- src/icon-mask.svg | 1 - src/icon.svg | 1 - src/models/Settings.php | 7 ++++++- src/services/Compress.php | 10 ++++++++++ src/templates/settings.twig | 20 ++++++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) delete mode 100644 src/icon-mask.svg delete mode 100644 src/icon.svg diff --git a/src/icon-mask.svg b/src/icon-mask.svg deleted file mode 100644 index 5c76c16..0000000 --- a/src/icon-mask.svg +++ /dev/null @@ -1 +0,0 @@ -zipper_icon \ No newline at end of file diff --git a/src/icon.svg b/src/icon.svg deleted file mode 100644 index 5c76c16..0000000 --- a/src/icon.svg +++ /dev/null @@ -1 +0,0 @@ -zipper_icon \ No newline at end of file diff --git a/src/models/Settings.php b/src/models/Settings.php index 908eeff..3452d9a 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -32,6 +32,10 @@ class Settings extends Model public $autoRegenerate = true; + public $maxFilesize = 0; + + public $maxFileCount = 0; + /** * @inheritdoc */ @@ -39,7 +43,8 @@ public function rules() { return [ ['defaultVolumeHandle', 'string'], - ['autoRegenerate', 'boolean'] + ['autoRegenerate', 'boolean'], + [['maxFilesize', 'maxFileCount'], 'integer'], ]; } } diff --git a/src/services/Compress.php b/src/services/Compress.php index 8da334a..ced9bc1 100644 --- a/src/services/Compress.php +++ b/src/services/Compress.php @@ -143,7 +143,17 @@ public function createArchiveAsset(ArchiveRecord $archiveRecord, $assetName = 'a if ($zip->open($zipPath, ZipArchive::CREATE) !== true) { throw new \Exception('Cannot create zip at '.$zipPath); } + + if (count($assets) > Plugin::$plugin->getSettings()->maxFileCount) { + throw new \Exception('Cannot create zip; too many files.'); + } + + $totalFilesize = 0; foreach ($assets as $asset) { + $totalFilesize += $asset->size; + if ($totalFilesize > Plugin::$plugin->getSettings()->maxFilesize) { + throw new \Exception('Cannot create zip; max filesize exceeded.'); + } $zip->addFile($asset->getCopyOfFile(), $asset->filename); } diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 712bc6d..809d712 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -30,3 +30,23 @@ value: settings['defaultVolumeHandle'], options: options }) }} + +{{ forms.textField({ + first: true, + label: "Maximum File Size"|t('app'), + id: 'max-filesize', + name: 'max-filesize', + value: (transform is defined ? transform.name : null), + errors: (transform is defined ? transform.getErrors('max-filesize') : null), + required: true, +}) }} + +{{ forms.textField({ + first: true, + label: "Maximum File Count"|t('app'), + id: 'max-filecount', + name: 'max-filecount', + value: (transform is defined ? transform.name : null), + errors: (transform is defined ? transform.getErrors('max-filecount') : null), + required: true, +}) }} From cbc3a6a9861819a9d876f938b717ac96b2fec2af Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 14:49:23 -0400 Subject: [PATCH 03/10] Update icons --- src/icon-mask.svg | 9 +++++++++ src/icon.svg | 10 ++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/icon-mask.svg create mode 100644 src/icon.svg diff --git a/src/icon-mask.svg b/src/icon-mask.svg new file mode 100644 index 0000000..8ede5f1 --- /dev/null +++ b/src/icon-mask.svg @@ -0,0 +1,9 @@ + + + + Compress - mask + Created with Sketch. + + + + \ No newline at end of file diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 0000000..40ef66b --- /dev/null +++ b/src/icon.svg @@ -0,0 +1,10 @@ + + + + Compress + Created with Sketch. + + + + + \ No newline at end of file From bd87dca29d7d5661c0f8b8190ce611c2c32e7670 Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 14:49:33 -0400 Subject: [PATCH 04/10] Update plugin handle --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 24c5359..3c7f7fd 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "venveo/compress", + "name": "venveo/craft-compress", "description": "Create files", "type": "craft-plugin", "version": "1.0.0", @@ -32,7 +32,7 @@ }, "extra": { "name": "Compress", - "handle": "compress", + "handle": "craft-compress", "hasCpSettings": true, "hasCpSection": false, "changelogUrl": "https://raw.githubusercontent.com/venveo/craft-compress/master/CHANGELOG.md", From 87ffaba51eea8db7c609df3810eff5157c0ca5a6 Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 15:13:45 -0400 Subject: [PATCH 05/10] Fix settings template path --- src/Compress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compress.php b/src/Compress.php index 320d55d..c964b2e 100644 --- a/src/Compress.php +++ b/src/Compress.php @@ -121,7 +121,7 @@ protected function createSettingsModel() protected function settingsHtml(): string { return Craft::$app->view->renderTemplate( - 'compress/settings', + 'craft-compress/settings', [ 'settings' => $this->getSettings() ] From a0b9a92edf4777cfc1b93eb082f8156b342c4322 Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 15:14:18 -0400 Subject: [PATCH 06/10] Add handling for unrestricted file count and sizes based on settings --- src/services/Compress.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/services/Compress.php b/src/services/Compress.php index ced9bc1..a35a54e 100644 --- a/src/services/Compress.php +++ b/src/services/Compress.php @@ -144,14 +144,16 @@ public function createArchiveAsset(ArchiveRecord $archiveRecord, $assetName = 'a throw new \Exception('Cannot create zip at '.$zipPath); } - if (count($assets) > Plugin::$plugin->getSettings()->maxFileCount) { + $maxFileCount = Plugin::$plugin->getSettings()->maxFileCount; + if ($maxFileCount > 0 && count($assets) > $maxFileCount) { throw new \Exception('Cannot create zip; too many files.'); } $totalFilesize = 0; + $maxFileSize = Plugin::$plugin->getSettings()->maxFilesize foreach ($assets as $asset) { $totalFilesize += $asset->size; - if ($totalFilesize > Plugin::$plugin->getSettings()->maxFilesize) { + if ($maxFileSize > 0 && $totalFilesize > $maxFileSize) { throw new \Exception('Cannot create zip; max filesize exceeded.'); } $zip->addFile($asset->getCopyOfFile(), $asset->filename); From 87943461666f42cdfe5da365fa5ee9c831a8d490 Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 15:30:49 -0400 Subject: [PATCH 07/10] Update logging categories and translate handles to 'craft-compress' --- src/controllers/CompressController.php | 4 ++-- src/jobs/Compressor.php | 8 ++++---- src/services/Compress.php | 6 +++--- src/templates/settings.twig | 2 +- src/utilities/CompressUtility.php | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/controllers/CompressController.php b/src/controllers/CompressController.php index d5fad6b..7cb39bf 100644 --- a/src/controllers/CompressController.php +++ b/src/controllers/CompressController.php @@ -53,8 +53,8 @@ public function actionGetLink($uid) return \Craft::$app->response->redirect($asset->getUrl()); } catch (\Exception $e) { \Craft::$app->response->setStatusCode(500); - \Craft::error('Archive could not be generated: '.$e->getMessage(), 'compress'); - \Craft::error($e->getTraceAsString(), 'compress'); + \Craft::error('Archive could not be generated: '.$e->getMessage(), 'craft-compress'); + \Craft::error($e->getTraceAsString(), 'craft-compress'); return 'Archive could not be generated'; } } diff --git a/src/jobs/Compressor.php b/src/jobs/Compressor.php index 5dea31c..cf4195b 100644 --- a/src/jobs/Compressor.php +++ b/src/jobs/Compressor.php @@ -53,9 +53,9 @@ public function execute($queue) try { Compress::$plugin->compress->createArchiveAsset($archiveRecord, $this->filename); } catch (\Exception $e) { - Craft::error('Failed to create archive', 'Compress'); - Craft::error($e->getMessage(), 'Compress'); - Craft::error($e->getTraceAsString(), 'Compress'); + Craft::error('Failed to create archive', 'craft-compress'); + Craft::error($e->getMessage(), 'craft-compress'); + Craft::error($e->getTraceAsString(), 'craft-compress'); \Craft::$app->cache->delete($this->cacheKey); \Craft::$app->cache->delete($this->cacheKey.':jobId'); @@ -74,6 +74,6 @@ public function execute($queue) */ protected function defaultDescription(): string { - return Craft::t('compress', 'Creating Archive '. $this->filename); + return Craft::t('craft-compress', 'Creating Archive '. $this->filename); } } diff --git a/src/services/Compress.php b/src/services/Compress.php index a35a54e..51df8e8 100644 --- a/src/services/Compress.php +++ b/src/services/Compress.php @@ -282,8 +282,8 @@ public function handleAssetDeleted(Asset $asset): void try { \Craft::$app->elements->deleteElementById($archiveAsset); } catch (\Throwable $e) { - Craft::error('Failed to delete an archive asset after a dependent file was deleted: '.$e->getMessage(), 'compress'); - Craft::error($e->getTraceAsString(), 'compress'); + Craft::error('Failed to delete an archive asset after a dependent file was deleted: '.$e->getMessage(), 'craft-compress'); + Craft::error($e->getTraceAsString(), 'craft-compress'); } } @@ -299,7 +299,7 @@ public function handleAssetDeleted(Asset $asset): void $jobId = \Craft::$app->queue->push($job); \Craft::$app->cache->set($cacheKey, true); \Craft::$app->cache->set($cacheKey.':'.'jobId', $jobId); - \Craft::info('Regenerating archive after a file was deleted.', 'compress'); + \Craft::info('Regenerating archive after a file was deleted.', 'craft-compress'); } } } diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 809d712..6952e1c 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -23,7 +23,7 @@ {% endfor %} {{ forms.selectField({ - label: 'Default Volume'|t('compress'), + label: 'Default Volume'|t('craft-compress'), instructions: 'Select the volume for archive storage', id: 'defaultVolumeHandle', name: 'defaultVolumeHandle', diff --git a/src/utilities/CompressUtility.php b/src/utilities/CompressUtility.php index 882a707..7eb762a 100644 --- a/src/utilities/CompressUtility.php +++ b/src/utilities/CompressUtility.php @@ -30,7 +30,7 @@ class CompressUtility extends Utility */ public static function displayName(): string { - return Craft::t('compress', 'Compress Cache'); + return Craft::t('craft-compress', 'Compress Cache'); } /** From 607f20f0731c11a7dae886a8f644e22a7b57c6fc Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 15:31:21 -0400 Subject: [PATCH 08/10] Fix missing semicolon --- src/services/Compress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/Compress.php b/src/services/Compress.php index 51df8e8..58061a8 100644 --- a/src/services/Compress.php +++ b/src/services/Compress.php @@ -150,7 +150,7 @@ public function createArchiveAsset(ArchiveRecord $archiveRecord, $assetName = 'a } $totalFilesize = 0; - $maxFileSize = Plugin::$plugin->getSettings()->maxFilesize + $maxFileSize = Plugin::$plugin->getSettings()->maxFilesize; foreach ($assets as $asset) { $totalFilesize += $asset->size; if ($maxFileSize > 0 && $totalFilesize > $maxFileSize) { From dfa3680800039d955096b828cb28bfb60ffc99fe Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 15:35:44 -0400 Subject: [PATCH 09/10] Fix file count/size settings --- src/templates/settings.twig | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/templates/settings.twig b/src/templates/settings.twig index 6952e1c..d86c854 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -34,19 +34,17 @@ {{ forms.textField({ first: true, label: "Maximum File Size"|t('app'), - id: 'max-filesize', - name: 'max-filesize', - value: (transform is defined ? transform.name : null), - errors: (transform is defined ? transform.getErrors('max-filesize') : null), + id: 'maxFilesize', + name: 'maxFilesize', + value: settings['maxFilesize'], required: true, }) }} {{ forms.textField({ first: true, label: "Maximum File Count"|t('app'), - id: 'max-filecount', - name: 'max-filecount', - value: (transform is defined ? transform.name : null), - errors: (transform is defined ? transform.getErrors('max-filecount') : null), + id: 'maxFileCount', + name: 'maxFileCount', + value: settings['maxFileCount'], required: true, }) }} From f874c96bcde9cef98b649b500b3c15c75d409f2d Mon Sep 17 00:00:00 2001 From: Jake Martinez Date: Wed, 20 Mar 2019 16:09:59 -0400 Subject: [PATCH 10/10] Update missing archive response for getLink --- src/controllers/CompressController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/controllers/CompressController.php b/src/controllers/CompressController.php index 7cb39bf..c61bcb4 100644 --- a/src/controllers/CompressController.php +++ b/src/controllers/CompressController.php @@ -26,8 +26,7 @@ public function actionGetLink($uid) { $record = ArchiveRecord::find()->where(['=', 'uid', $uid])->one(); if (!$record instanceof ArchiveRecord) { - \Craft::$app->response->setStatusCode(404); - return 'Archive could not be found'; + return \Craft::$app->response->setStatusCode(404, 'Archive could not be found'); } // If the asset is ready, redirect to its URL