Skip to content

Commit

Permalink
Updated Compress
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Mar 22, 2019
1 parent e4a155c commit e2f8603
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 20 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "venveo/compress",
"name": "venveo/craft-compress",
"description": "Create files",
"type": "craft-plugin",
"version": "1.0.0",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function createSettingsModel()
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'compress/settings',
'craft-compress/settings',
[
'settings' => $this->getSettings()
]
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/CompressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class CompressController extends Controller
{
public $allowAnonymous = ['getLink'];
public $allowAnonymous = ['get-link'];

/**
* Gets a direct link to the asset
Expand Down Expand Up @@ -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';
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/icon-mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/jobs/Compressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
}
7 changes: 6 additions & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ class Settings extends Model

public $autoRegenerate = true;

public $maxFilesize = 0;

public $maxFileCount = 0;

/**
* @inheritdoc
*/
public function rules()
{
return [
['defaultVolumeHandle', 'string'],
['autoRegenerate', 'boolean']
['autoRegenerate', 'boolean'],
[['maxFilesize', 'maxFileCount'], 'integer'],
];
}
}
22 changes: 17 additions & 5 deletions src/services/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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');
}
}

Expand All @@ -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');
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) }}
2 changes: 1 addition & 1 deletion src/utilities/CompressUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit e2f8603

Please sign in to comment.