diff --git a/Admin/Installer.php b/Admin/Installer.php index 1074240..bb3a5a3 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -186,7 +186,7 @@ final class Installer extends InstallerAbstract ? ApiController::createMediaPath() : __DIR__ . '/../../..' . $data['path']; - $status = $upload->upload($files, $data['name'], true); + $status = $upload->upload($files, [$data['name']], true); $mediaFiles = []; foreach ($status as $uFile) { diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 0154cfd..b917e20 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -69,7 +69,7 @@ final class ApiController extends Controller public function apiMediaUpload(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { $uploads = $this->uploadFiles( - $request->getData('name') === null || \count($request->getFiles()) > 1 ? '' : $request->getData('name'), + [$request->getData('name') === null ? '' : $request->getData('name')], $request->getFiles(), $request->header->account, __DIR__ . '/../../../Modules/Media/Files' . \urldecode((string) ($request->getData('path') ?? '')), @@ -120,7 +120,7 @@ final class ApiController extends Controller /** * Upload a media file * - * @param string $name Name + * @param array $names Name * @param array $files Files * @param int $account Uploader * @param string $basePath Base path. The path which is used for the upload. @@ -138,7 +138,7 @@ final class ApiController extends Controller * @since 1.0.0 */ public function uploadFiles( - string $name, + array $names, array $files, int $account, string $basePath = '/Modules/Media/Files', @@ -147,7 +147,8 @@ final class ApiController extends Controller string $password = '', string $encryptionKey = '', int $pathSettings = PathSettings::RANDOM_PATH - ) : array { + ) : array + { if (empty($files)) { return []; } @@ -167,7 +168,7 @@ final class ApiController extends Controller $upload = new UploadFile(); $upload->outputDir = $outputDir; - $status = $upload->upload($files, $name, $absolute, $encryptionKey); + $status = $upload->upload($files, $names, $absolute, $encryptionKey); return $this->createDbEntries($status, $account, $virtualPath, $type); } @@ -176,7 +177,7 @@ final class ApiController extends Controller * Uploads a file to a destination * * @param array $files Files to upload - * @param string $name Name of the file (only if a single file is provided) + * @param array $names Name of the file (only if a single file is provided) * @param string $path Upload path * * @return array @@ -185,13 +186,14 @@ final class ApiController extends Controller */ public static function uploadFilesToDestination( array $files, - string $name = '', + array $names = [], string $path = '', - ) : array { + ) : array + { $upload = new UploadFile(); $upload->outputDir = $path; - $status = $upload->upload($files, $name, true, ''); + $status = $upload->upload($files, $names, true, ''); return $status; } @@ -212,6 +214,8 @@ final class ApiController extends Controller } /** + * Create database entries for uploaded files + * * @param array $status Files * @param int $account Uploader * @param string $virtualPath Virtual path @@ -228,7 +232,8 @@ final class ApiController extends Controller string $virtualPath = '', string $type = '', string $ip = '127.0.0.1' - ) : array { + ) : array + { $mediaCreated = []; foreach ($status as $uFile) { diff --git a/Models/UploadFile.php b/Models/UploadFile.php index 75c3181..c8c8eec 100755 --- a/Models/UploadFile.php +++ b/Models/UploadFile.php @@ -82,11 +82,11 @@ class UploadFile /** * Upload file to server. * - * @param array $files File data ($_FILE) - * @param string $name File name - * @param bool $absolute Use absolute path - * @param string $encryptionKey Encryption key - * @param string $encoding Encoding used for uploaded file. Empty string will not convert file content. + * @param array $files File data ($_FILE) + * @param string[] $names File name + * @param bool $absolute Use absolute path + * @param string $encryptionKey Encryption key + * @param string $encoding Encoding used for uploaded file. Empty string will not convert file content. * * @return array * @@ -96,26 +96,30 @@ class UploadFile */ public function upload( array $files, - string $name = '', + array $names = [], bool $absolute = false, string $encryptionKey = '', string $encoding = 'UTF-8' - ) : array { + ) : array + { $result = []; if (\count($files) === \count($files, \COUNT_RECURSIVE)) { $files = [$files]; } - $fileCount = \count($files); - if (!$absolute && \count($files) > 1) { $this->outputDir = $this->findOutputDir(); } - $path = $this->outputDir; + $path = $this->outputDir; + $fCounter = -1; + $areNamed = \count($files) === \count($names); foreach ($files as $key => $f) { + ++$fCounter; + $name = $areNamed ? $names[$fCounter] : ''; + if ($path === '') { $path = File::dirpath($f['tmp_name']); } @@ -148,9 +152,7 @@ class UploadFile } $split = \explode('.', $f['name']); - $result[$key]['filename'] = \count($files) === 1 && !empty($name) - ? $name - : $f['name']; + $result[$key]['filename'] = !empty($name) ? $name : $f['name']; $extension = \count($split) > 1 ? $split[\count($split) - 1] : ''; $result[$key]['extension'] = $extension; @@ -160,7 +162,7 @@ class UploadFile $result[$key]['filename'] = $name; } - if (!$this->preserveFileName || $fileCount !== 1 || empty($name) || \is_file($path . '/' . $name)) { + if (!$this->preserveFileName || empty($name) || \is_file($path . '/' . $name)) { try { $name = $this->createFileName($path, $f['tmp_name'], $extension); $result[$key]['filename'] = $name; diff --git a/tests/Controller/Api/ApiControllerMediaTrait.php b/tests/Controller/Api/ApiControllerMediaTrait.php index 48b7682..8320af5 100755 --- a/tests/Controller/Api/ApiControllerMediaTrait.php +++ b/tests/Controller/Api/ApiControllerMediaTrait.php @@ -172,7 +172,7 @@ trait ApiControllerMediaTrait { self::assertEquals( [], - $this->module->uploadFiles('test', ['test'], 1, '/test', '', '', '', '', 99) + $this->module->uploadFiles(['test'], ['test'], 1, '/test', '', '', '', '', 99) ); }