Skip to content

Commit

Permalink
Add protection to physical delete multiplied files, which are related…
Browse files Browse the repository at this point in the history
… more then one owner
  • Loading branch information
itstructure committed Sep 12, 2020
1 parent ea324c0 commit 08d57c7
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Addition module description you can see in my [Personal site](https://pack-devel

Via composer:

`composer require itstructure/yii2-multi-format-uploader ~3.1.1`
`composer require itstructure/yii2-multi-format-uploader ~3.2.0`

### If you are testing this package from local server directory

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### CHANGE LOG:

**3.2.0 September 12, 2020:**
- Optimize `deleteMediafiles()` method in `MediaFilesTrait`. Add protection to physical delete multiplied files, which are related more then one owner.
- Optimize owner's entity classes.

**3.1.1 August 10, 2020:**
- Add module attribute `useInitialThumbsConfig` with default value **true**.

Expand Down
3 changes: 2 additions & 1 deletion src/behaviors/BehaviorAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Itstructure\MFUploader\behaviors;

use yii\db\ActiveRecordInterface;
use Itstructure\MFUploader\models\OwnerAlbum;
use Itstructure\MFUploader\models\album\Album;

/**
Expand Down Expand Up @@ -38,6 +39,6 @@ protected function loadModel(array $conditions)
*/
protected function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
{
return Album::removeOwner($ownerId, $owner, $ownerAttribute);
return OwnerAlbum::removeOwner($ownerId, $owner, $ownerAttribute);
}
}
4 changes: 2 additions & 2 deletions src/behaviors/BehaviorMediafile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Itstructure\MFUploader\behaviors;

use yii\db\ActiveRecordInterface;
use Itstructure\MFUploader\models\Mediafile;
use Itstructure\MFUploader\models\{Mediafile, OwnerMediafile};

/**
* Class BehaviorMediafile
Expand Down Expand Up @@ -38,6 +38,6 @@ protected function loadModel(array $conditions)
*/
protected function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
{
return Mediafile::removeOwner($ownerId, $owner, $ownerAttribute);
return OwnerMediafile::removeOwner($ownerId, $owner, $ownerAttribute);
}
}
14 changes: 0 additions & 14 deletions src/models/Mediafile.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,6 @@ public function addOwner(int $ownerId, string $owner, string $ownerAttribute): b
return OwnerMediafile::addOwner($this->id, $ownerId, $owner, $ownerAttribute);
}

/**
* Remove this mediafile owner.
*
* @param int $ownerId
* @param string $owner
* @param string $ownerAttribute
*
* @return bool
*/
public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
{
return OwnerMediafile::removeOwner($ownerId, $owner, $ownerAttribute);
}

/**
* @return \yii\db\ActiveQuery
*/
Expand Down
59 changes: 47 additions & 12 deletions src/models/Owner.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,39 @@ public static function addOwner(int $modelId, int $ownerId, string $owner, strin
*
* @param int $ownerId
* @param string $owner
* @param string $ownerAttribute
* @param string|null $ownerAttribute
*
* @return bool
*/
public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute = null): bool
{
$deleted = static::deleteAll([
'ownerId' => $ownerId,
'owner' => $owner,
'ownerAttribute' => $ownerAttribute,
]);
$deleted = static::deleteAll(static::buildFilterOptions($ownerId, $owner, $ownerAttribute));

return $deleted > 0;
}

/**
* Getting entity id's which are related with Other owners too.
*
* @param string $owner
* @param int $ownerId
* @param array $entityIds
*
* @return array|\yii\db\ActiveRecord[]
*/
public static function filterMultipliedEntityIds(string $owner, int $ownerId, array $entityIds)
{
return static::find()
->select(static::getModelKeyName())
->where([static::getModelKeyName() => $entityIds])
->andWhere([
'OR',
['!=', 'ownerId', $ownerId],
['!=', 'owner', $owner]
])
->all();
}

/**
* Get Id's by owner.
*
Expand All @@ -121,22 +139,22 @@ protected static function getEntityIdsQuery(string $nameId, array $args): Active
{
$conditions = [];

if (isset($args['owner'])) {
if (!is_string($args['owner']) || empty($args['owner'])) {
if (!empty($args['owner'])) {
if (!is_string($args['owner'])) {
throw new InvalidArgumentException('Parameter owner must be a string.');
}
$conditions['owner'] = $args['owner'];

if (isset($args['ownerId'])) {
if (!empty($args['ownerId'])) {
if (!is_numeric($args['ownerId'])) {
throw new InvalidArgumentException('Parameter ownerId must be numeric.');
}
$conditions['ownerId'] = $args['ownerId'];
}
}

if (isset($args['ownerAttribute'])) {
if (!is_string($args['ownerAttribute']) || empty($args['ownerAttribute'])) {
if (!empty($args['ownerAttribute'])) {
if (!is_string($args['ownerAttribute'])) {
throw new InvalidArgumentException('Parameter ownerAttribute must be a string.');
}
$conditions['ownerAttribute'] = $args['ownerAttribute'];
Expand All @@ -151,4 +169,21 @@ protected static function getEntityIdsQuery(string $nameId, array $args): Active

return $query;
}

/**
* Build filter options for some actions.
*
* @param int $ownerId
* @param string $owner
* @param string|null $ownerAttribute
*
* @return array
*/
protected static function buildFilterOptions(int $ownerId, string $owner, string $ownerAttribute = null)
{
return array_merge([
'ownerId' => $ownerId,
'owner' => $owner
], empty($ownerAttribute) ? [] : ['ownerAttribute' => $ownerAttribute]);
}
}
6 changes: 1 addition & 5 deletions src/models/OwnerAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ public function getAlbum()
*/
public static function getAlbums(string $owner, int $ownerId, string $ownerAttribute = null)
{
return static::getAlbumsQuery([
'owner' => $owner,
'ownerId' => $ownerId,
'ownerAttribute' => $ownerAttribute,
])->all();
return static::getAlbumsQuery(static::buildFilterOptions($ownerId, $owner, $ownerAttribute))->all();
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/models/OwnerMediafile.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ public function getMediaFile()
*/
public static function getMediaFiles(string $owner, int $ownerId, string $ownerAttribute = null)
{
return static::getMediaFilesQuery([
'owner' => $owner,
'ownerId' => $ownerId,
'ownerAttribute' => $ownerAttribute,
])->all();
return static::getMediaFilesQuery(static::buildFilterOptions($ownerId, $owner, $ownerAttribute))->all();
}

/**
Expand Down
14 changes: 0 additions & 14 deletions src/models/album/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,6 @@ public function addOwner(int $ownerId, string $owner, string $ownerAttribute): b
return OwnerAlbum::addOwner($this->id, $ownerId, $owner, $ownerAttribute);
}

/**
* Remove this mediafile owner.
*
* @param int $ownerId
* @param string $owner
* @param string $ownerAttribute
*
* @return bool
*/
public static function removeOwner(int $ownerId, string $owner, string $ownerAttribute): bool
{
return OwnerAlbum::removeOwner($ownerId, $owner, $ownerAttribute);
}

/**
* Get album's owners.
*
Expand Down
32 changes: 26 additions & 6 deletions src/traits/MediaFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,39 @@ trait MediaFilesTrait
* @param string $owner
* @param int $ownerId
* @param Module $module
* @param bool $protectMultiplied
*
* @return void
*/
protected function deleteMediafiles(string $owner, int $ownerId, Module $module): void
protected function deleteMediafiles(string $owner, int $ownerId, Module $module, bool $protectMultiplied = true): void
{
$mediafileIds = OwnerMediafile::getMediaFilesQuery([
$mediafileIds = array_map(function ($data) {
return $data->id;

}, OwnerMediafile::getMediaFilesQuery([
'owner' => $owner,
'ownerId' => $ownerId,
'ownerId' => $ownerId
])
->select('id')
->all();
->select('id')
->all()
);

if ($protectMultiplied) {
$multipliedMediafileIds = array_map(function ($data) {
return $data->mediafileId;

}, OwnerMediafile::filterMultipliedEntityIds($owner, $ownerId, $mediafileIds));

$mediafileIds = array_map(function ($data) {return $data->id;}, $mediafileIds);
$mediafileIds = array_filter($mediafileIds, function ($item) use ($multipliedMediafileIds) {
return !in_array($item, $multipliedMediafileIds);
});

OwnerMediafile::deleteAll([
'owner' => $owner,
'ownerId' => $ownerId,
'mediafileId' => $multipliedMediafileIds
]);
}

$this->deleteMediafileEntry($mediafileIds, $module);
}
Expand Down

0 comments on commit 08d57c7

Please sign in to comment.