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", 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() ] diff --git a/src/controllers/CompressController.php b/src/controllers/CompressController.php index d5fad6b..a35e053 100644 --- a/src/controllers/CompressController.php +++ b/src/controllers/CompressController.php @@ -17,7 +17,7 @@ class CompressController extends Controller { - public $allowAnonymous = ['getLink']; + public $allowAnonymous = ['get-link']; /** * Gets a direct link to the asset @@ -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/icon-mask.svg b/src/icon-mask.svg index 5c76c16..8ede5f1 100644 --- a/src/icon-mask.svg +++ b/src/icon-mask.svg @@ -1 +1,9 @@ -zipper_icon \ No newline at end of file + + + + Compress - mask + Created with Sketch. + + + + \ No newline at end of file diff --git a/src/icon.svg b/src/icon.svg index 5c76c16..40ef66b 100644 --- a/src/icon.svg +++ b/src/icon.svg @@ -1 +1,10 @@ -zipper_icon \ No newline at end of file + + + + Compress + Created with Sketch. + + + + + \ No newline at end of file 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/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 185201d..58061a8 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); } @@ -143,7 +143,19 @@ public function createArchiveAsset(ArchiveRecord $archiveRecord, $assetName = 'a if ($zip->open($zipPath, ZipArchive::CREATE) !== true) { throw new \Exception('Cannot create zip at '.$zipPath); } + + $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 ($maxFileSize > 0 && $totalFilesize > $maxFileSize) { + throw new \Exception('Cannot create zip; max filesize exceeded.'); + } $zip->addFile($asset->getCopyOfFile(), $asset->filename); } @@ -270,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'); } } @@ -287,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 712bc6d..d86c854 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -23,10 +23,28 @@ {% 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', value: settings['defaultVolumeHandle'], options: options }) }} + +{{ forms.textField({ + first: true, + label: "Maximum File Size"|t('app'), + id: 'maxFilesize', + name: 'maxFilesize', + value: settings['maxFilesize'], + required: true, +}) }} + +{{ forms.textField({ + first: true, + label: "Maximum File Count"|t('app'), + id: 'maxFileCount', + name: 'maxFileCount', + value: settings['maxFileCount'], + required: true, +}) }} 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'); } /**