Skip to content

Commit

Permalink
Merge pull request #18 from venveo/develop
Browse files Browse the repository at this point in the history
4.0.0
  • Loading branch information
Mosnar authored Jun 16, 2022
2 parents 6356694 + 26e922d commit 6161c6d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 60 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Compress Changelog

## 4.0.0 - 2022-06-16
### Change
- Compress now requires Craft 4
- Compress now requires PHP 8.0.1
- Hashing mechanism now accounts for dateUpdated and sorts records. All zip files will need to be regenerated.

## 1.0.3 - 2019-11-22
### Fixed
- Improved compatibility with PHP 7.0.x (again-again)
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "venveo/craft-compress",
"description": "Create smart zip files from Craft assets on the fly",
"type": "craft-plugin",
"version": "1.0.3",
"version": "4.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -22,8 +22,8 @@
}
],
"require": {
"craftcms/cms": "^3.2.0",
"ext-zip": "*"
"php": "^8.0.2|^9.0",
"craftcms/cms": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions src/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Compress extends Plugin
/**
* @var string
*/
public $schemaVersion = '1.0.0';
public string $schemaVersion = '1.0.0';

public $hasCpSettings = true;
public bool $hasCpSettings = true;

// Public Methods
// =========================================================================
Expand Down Expand Up @@ -117,15 +117,15 @@ function (RegisterComponentTypesEvent $event) {
/**
* @inheritdoc
*/
protected function createSettingsModel()
protected function createSettingsModel(): ?\craft\base\Model
{
return new Settings();
}

/**
* @inheritdoc
*/
protected function settingsHtml(): string
protected function settingsHtml(): ?string
{
return Craft::$app->view->renderTemplate(
'compress/settings',
Expand Down
24 changes: 15 additions & 9 deletions src/controllers/CompressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@

namespace venveo\compress\controllers;

use Craft;
use craft\elements\Asset;
use craft\helpers\DateTimeHelper;
use craft\web\Controller;
use venveo\compress\Compress as Plugin;
use venveo\compress\errors\CompressException;
use venveo\compress\models\Archive as ArchiveModel;
use venveo\compress\records\Archive as ArchiveRecord;
use yii\base\InvalidConfigException;

/**
* Class CompressController
* @package venveo\compress\controllers
*/
class CompressController extends Controller
{
public $allowAnonymous = ['get-link'];
public int|bool|array $allowAnonymous = ['get-link'];

/**
* Gets a direct link to the asset
* @param $uid
* @return \craft\web\Response|string|\yii\console\Response
* @return \yii\web\Response
* @throws CompressException
* @throws InvalidConfigException
*/
public function actionGetLink($uid)
{
/** @var ArchiveRecord $record */
$record = ArchiveRecord::find()->where(['=', 'uid', $uid])->one();
if (!$record instanceof ArchiveRecord) {
if (!$record) {
return \Craft::$app->response->setStatusCode(404, 'Archive could not be found');
}

Expand All @@ -45,9 +46,14 @@ public function actionGetLink($uid)
$archiveModel = ArchiveModel::hydrateFromRecord($record);
// It's possible for an asset ID to exist, but getAsset to return false on soft-deleted assets
if ($archiveModel->getAsset()) {
$record->dateLastAccessed = DateTimeHelper::currentUTCDateTime();
$record->save();
return \Craft::$app->response->redirect($archiveModel->getAsset()->getUrl());
$assetUrl = $archiveModel->getAsset()->getUrl();
if ($assetUrl) {
$record->dateLastAccessed = DateTimeHelper::currentUTCDateTime();
$record->save();
return \Craft::$app->response->redirect($archiveModel->getAsset()->getUrl());
}

return \Craft::$app->response->setStatusCode(404, 'Could not produce zip file URL.');
}
}

Expand All @@ -69,7 +75,7 @@ public function actionGetLink($uid)
} catch (\Exception $e) {
\Craft::error('Archive could not be generated: ' . $e->getMessage(), __METHOD__);
\Craft::error($e->getTraceAsString(), __METHOD__);
throw new CompressException('Archive could not be generated: '. $e->getMessage());
throw new CompressException('Archive could not be generated: ' . $e->getMessage());
}
}
}
10 changes: 5 additions & 5 deletions src/jobs/CreateArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ class CreateArchive extends BaseJob
/**
* @inheritdoc
*/
public function execute($queue)
public function execute($queue): void
{
// If it's not in the cache, we'll assume it got completed on-demand
if (!\Craft::$app->cache->get($this->cacheKey)) {
Craft::info('Archive already completed');
return true;
return;
}

$archiveRecord = ArchiveRecord::find()->where(['=', 'uid', $this->archiveUid])->one();
if (!$archiveRecord instanceof ArchiveRecord) {
Craft::error('Archive was deleted before it could be created');
return false;
return;
}

try {
Expand All @@ -58,7 +58,7 @@ public function execute($queue)
\Craft::$app->cache->delete($this->cacheKey);
\Craft::$app->cache->delete($this->cacheKey . ':jobId');
// Go ahead and blow up
return false;
return;
}
\Craft::$app->cache->delete($this->cacheKey);
\Craft::$app->cache->delete($this->cacheKey . ':jobId');
Expand All @@ -70,7 +70,7 @@ public function execute($queue)
/**
* @inheritdoc
*/
protected function defaultDescription(): string
protected function defaultDescription(): ?string
{
return Craft::t('compress', 'Creating Archive');
}
Expand Down
35 changes: 23 additions & 12 deletions src/models/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@
use craft\db\ActiveRecord;
use craft\elements\Asset;
use craft\helpers\UrlHelper;
use DateTime;
use venveo\compress\Compress as Plugin;

/**
* @author Venveo
* @package Compress
* @since 1.0.0
*
* @property-read mixed $contents
* @property-read null|string $lazyLink
*/
class Archive extends Model
{
public $id;
public $uid;
public $assetId;
public $hash;
public ?int $id = null;
public ?string $uid = null;
public ?int $assetId = null;
public ?string $hash = null;

public $dateCreated;
public $dateUpdated;
public $dateLastAccessed;
public ?DateTime $dateUpdated;
public ?DateTime $dateCreated;
public ?DateTime $dateLastAccessed = null;

public $asset;
public ?Asset $asset = null;


/**
Expand All @@ -52,26 +56,33 @@ public static function hydrateFromRecord(ActiveRecord $record, Asset $asset = nu
/**
* @return Asset|null
*/
public function getAsset()
public function getAsset($siteId = null): ?Asset
{
if (!$this->assetId) {
return null;
}
if ($this->asset instanceof Asset) {
return $this->asset;
}
if (!$siteId) {
$siteId = \Craft::$app->sites?->currentSite?->id;
}

$this->asset = \Craft::$app->assets->getAssetById($this->assetId);
$this->asset = \Craft::$app->assets->getAssetById($this->assetId, $siteId);
return $this->asset;
}

/**
* @return string|null
*/
public function getLazyLink()
public function getLazyLink(): ?string
{
if ($this->asset instanceof Asset) {
return $this->asset->getUrl();
// Ensure we _can_ get a url for the asset
$assetUrl = $this->asset->getUrl();
if($assetUrl) {
return $assetUrl;
}
}
return UrlHelper::actionUrl('compress/compress/get-link', ['uid' => $this->uid]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Settings extends Model
/**
* @inheritdoc
*/
public function rules()
public function rules(): array
{
return [
['defaultVolumeHandle', 'string'],
Expand Down
21 changes: 10 additions & 11 deletions src/records/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
namespace venveo\compress\records;

use craft\db\ActiveRecord;
use craft\helpers\Db;
use craft\records\Asset;
use Ramsey\Uuid\Uuid;
use DateTime;
use yii\db\ActiveQueryInterface;

/**
Expand All @@ -14,7 +15,6 @@
* @property \yii\db\ActiveQueryInterface $asset
* @property mixed $fileAssets
* @property integer id
* @property Uuid uid
* @property integer assetId
* @property \DateTime dateLastAccessed
* @property string hash
Expand Down Expand Up @@ -42,15 +42,14 @@ public function getFileAssets()
return $this->hasMany(File::class, ['archiveId' => 'id']);
}

/**
* @inheritdoc
*/
public function datetimeAttributes(): array
protected function prepareForDb(): void
{
$attributes = parent::datetimeAttributes();

$attributes[] = 'dateLastAccessed';

return $attributes;
parent::prepareForDb();
$now = Db::prepareDateForDb(new DateTime());
if ($this->getIsNewRecord()) {
if (!isset($this->dateLastAccessed)) {
$this->dateLastAccessed = $now;
}
}
}
}
31 changes: 17 additions & 14 deletions src/services/Compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

use Craft;
use craft\base\Component;
use craft\base\Volume;
use craft\elements\Asset;
use craft\elements\db\AssetQuery;
use craft\helpers\DateTimeHelper;
use craft\helpers\StringHelper;
use craft\models\Volume;
use venveo\compress\Compress as Plugin;
use venveo\compress\errors\CompressException;
use venveo\compress\events\CompressEvent;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function getArchiveModelForQuery($query, $lazy = false, $filename = null)
// Get the assets and create a unique hash to represent them
if ($query instanceof AssetQuery) {
$assets = $query->all();
} elseif (is_array($query)) {
} elseif ($query instanceof \ArrayAccess) {
$assets = $query;
} else {
Craft::error('Unexpected input provided for asset query', __METHOD__);
Expand Down Expand Up @@ -120,16 +120,14 @@ public function createArchiveRecord($assets, $archiveAsset = null)
* @param string $assetName
* @return ArchiveModel
* @throws \craft\errors\VolumeException
* @throws \craft\errors\VolumeObjectExistsException
* @throws \craft\errors\VolumeObjectNotFoundException
* @throws \Exception
*/
public function createArchiveAsset(ArchiveRecord $archiveRecord)
public function createArchiveAsset(ArchiveRecord $archiveRecord): ?ArchiveModel
{
$uuid = StringHelper::UUID();
$fileAssetRecords = $archiveRecord->fileAssets;
$assetIds = [];
/** @var File $fileAssetRecord */
/** @var FileRecord $fileAssetRecord */
foreach ($fileAssetRecords as $fileAssetRecord) {
$assetIds[] = $fileAssetRecord->assetId;
}
Expand Down Expand Up @@ -180,9 +178,11 @@ public function createArchiveAsset(ArchiveRecord $archiveRecord)
throw new CompressException('Default volume not set.');
}
$finalFilePath = $assetName;
$volume->createFileByStream($finalFilePath, $stream, []);
$fs = $volume->getFs();
$fs->writeFileFromStream($finalFilePath, $stream, []);
unlink($zipPath);
$asset = Craft::$app->getAssetIndexer()->indexFile($volume, $finalFilePath);
$session = Craft::$app->getAssetIndexer()->createIndexingSession([$volume]);
$asset = Craft::$app->getAssetIndexer()->indexFile($volume, $finalFilePath, $session->id);
$archiveRecord->assetId = $asset->id;
$archiveRecord->dateLastAccessed = DateTimeHelper::currentUTCDateTime();
$archiveRecord->save();
Expand Down Expand Up @@ -219,7 +219,6 @@ private function createArchiveRecords($zippedAssets, $asset, $archiveRecord = nu
$archiveRecord = $event->archiveRecord;

$rows = [];
/** @var Asset $zippedAsset */
foreach ($zippedAssets as $zippedAsset) {
$rows[] = [
$archiveRecord->id,
Expand Down Expand Up @@ -248,9 +247,13 @@ private function getHashForAssets($assets): string
{
$ids = [];
foreach ($assets as $asset) {
$ids[] = [$asset->id];
$updatedAt = $asset->dateUpdated->getTimestamp();
$key = $asset->id . ':' . $updatedAt;
$ids[] = $key;
}
return md5(\GuzzleHttp\json_encode($ids));
sort($ids);
$hashKey = implode('', $ids);
return md5($hashKey);
}

/**
Expand Down Expand Up @@ -334,7 +337,7 @@ public function getArchiveContents(ArchiveModel $archive): AssetQuery
* @param null $limit
* @return array
*/
public function getArchives($offset = 0, $limit = null): array
public function getArchives(?int $offset = 0, ?int $limit = null): array
{
$records = ArchiveRecord::find();
if ($offset) {
Expand All @@ -352,12 +355,12 @@ public function getArchives($offset = 0, $limit = null): array
}

/**
* Get an archive model from it's record's UID
* Get an archive model from its record's UID
*
* @param $uid
* @return ArchiveModel|null
*/
public function getArchiveModelByUID($uid)
public function getArchiveModelByUID($uid): ?ArchiveModel
{
$record = ArchiveRecord::find()->where(['=', 'uid', $uid])->one();
if (!$record instanceof ArchiveRecord) {
Expand Down
Loading

0 comments on commit 6161c6d

Please sign in to comment.