Skip to content

Commit

Permalink
Merge pull request #13 from webinertia/0.0.9
Browse files Browse the repository at this point in the history
Adds multi file delete support
  • Loading branch information
tyrsson authored Jul 18, 2023
2 parents 8132357 + 44d7cc1 commit 7fdf1bc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"webinertia/webinertia-db": "^0.0.3 || ^0.0.4 || ^0.0.5"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.5.0",
"phpunit/phpunit": "10.2.6",
"laminas/laminas-coding-standard": "^2.5",
"phpunit/phpunit": "^10.2",
"psalm/plugin-phpunit": "^0.18.4",
"phpstan/phpstan": "1.10.25",
"phpstan/phpstan": "^1.10",
"laminas/laminas-test": "^4.8",
"phpstan/extension-installer": "^1.2",
"slam/phpstan-laminas-framework": "^1.4"
Expand Down
5 changes: 5 additions & 0 deletions src/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ abstract class AbstractHandler extends AbstractModel implements UploaderHandlerI
public const BASE_PUBLIC_PATH = __DIR__ . '/upload';
public const BASE_LOCAL_PATH = __DIR__ . '/../../../../public/upload';

/** @var array<string, string> $messageTemplate */
protected $messageTemplate = [
'FILE_NOT_FOUND' => 'The requested file could not be found.',
'FILE_NOT_DELETED' => 'File could not be deleted.'
];
/** @var array $columns */
protected array $columnMap = [
'id',
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Webinertia\Uploader\Exception;

use RuntimeException;

class Exception extends RuntimeException
{
}
9 changes: 9 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Webinertia\Uploader\Exception;

final class InvalidArgumentException extends Exception
{
}
57 changes: 39 additions & 18 deletions src/UploadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Webinertia\Uploader;

use Laminas\Db\ResultSet\ResultSetInterface;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Diactoros\UploadedFile;
use Laminas\EventManager\ResponseCollection;
use Laminas\Filter\BaseName;
use Laminas\Filter\Exception\InvalidArgumentException;
use Laminas\Filter\Exception\RuntimeException;
use RecursiveArrayIterator;
use SplFileInfo;
use Webinertia\Db\Exception\RecordNotFound;
use Webinertia\Db\ModelTrait;
use Webinertia\Filter\Uuid;

Expand All @@ -21,55 +21,75 @@ class UploadManager extends AbstractHandler
{
use ModelTrait;

public function handleUpload(UploaderEvent $event)
/** @var array<int, array> $uploaded */
protected array $uploaded = [];

public function handleUpload(UploaderEvent $event): array
{
$request = $event->getRequest();
$this->exchangeArray($event->getParams());
$files = $request->getUploadedFiles();
$iterator = new RecursiveArrayIterator($files);
iterator_apply($iterator, self::class . '::fileSearch', [$iterator]);
return 'last';
return $this->uploaded;
}

public function handleDelete(UploaderEvent $event)
/**
* @param UploaderEvent $event
* @return array
* @throws Exception\RunTimeException
* @throws RecordNotFound
*/
public function handleDelete(UploaderEvent $event): array
{
$deleted = [];
$params = $event->getParams();
if (! isset($params['uuid'])) {
throw new Exception\RunTimeException('Delete Event requires the file uuid to be passed as a param');
}
$data = $this->fetchByColumn('uuid', $params['uuid']);
if ($data instanceof self) {
$fileInfo = new SplFileInfo($data->target . $data->fileName);
if ($fileInfo->isFile()) {
if(unlink($fileInfo->getRealPath())) {
// this must use offsetGet due to uuid being a class property
if ($this->delete(['uuid' => $data->offsetGet('uuid')])) {
return $data->offsetGet('uuid');
} else {
/** @var ResultSetInterface|HydratingResultSet $fileData */
$resultSet = $this->fetchByColumn('uuid', $params['uuid']);
if ($resultSet->count() === 0) {
throw new RecordNotFound('The requested records could not be found');
}

}
foreach ($resultSet as $file) {
if ($file instanceof self) {
$data = $file->getArrayCopy();
$result = $this->deleteFile(new SplFileInfo($data['target'] . $data['fileName']));
if ($result && $this->delete(['uuid' => $data['uuid']]) !== 0) {
$deleted[] = [$data['uuid'] => $data['fileName']];
}
}
}
return $deleted;
}

protected function deleteFile(SplFileInfo $file): bool
{
if (! $file->isFile()) {
throw new Exception\RunTimeException('File not found.');
}
return unlink($file->getRealPath());
}
protected function fileSearch($iterator)
{
while ($iterator->valid() ) {
if ($iterator->hasChildren()) {
$current = $iterator->current();
if ($current instanceof UploadedFile) {
$this->processUploadedFile($current);
$this->uploaded[] = $this->processUploadedFile($current);
}
$this->fileSearch($iterator->getChildren());
} else {
$current = $iterator->current();
if ($current instanceof UploadedFile) {
$this->processUploadedFile($current);
$this->uploaded[] = $this->processUploadedFile($current);
}
}
$iterator->next();
}
return $this->uploaded;
}

protected function processUploadedFile(UploadedFile $file)
Expand All @@ -83,5 +103,6 @@ protected function processUploadedFile(UploadedFile $file)
$this->offsetSet('uuid', $uuid->filter(null));
$this->save($this);
}
return $this->getArrayCopy();
}
}

0 comments on commit 7fdf1bc

Please sign in to comment.