formatting fixes, bug fixes and support impl.

This commit is contained in:
Dennis Eichhorn 2021-07-04 18:27:21 +02:00
parent 391b48dbff
commit d0f7703b54
4 changed files with 33 additions and 26 deletions

View File

@ -186,7 +186,7 @@ final class Installer extends InstallerAbstract
? ApiController::createMediaPath() ? ApiController::createMediaPath()
: __DIR__ . '/../../..' . $data['path']; : __DIR__ . '/../../..' . $data['path'];
$status = $upload->upload($files, $data['name'], true); $status = $upload->upload($files, [$data['name']], true);
$mediaFiles = []; $mediaFiles = [];
foreach ($status as $uFile) { foreach ($status as $uFile) {

View File

@ -69,7 +69,7 @@ final class ApiController extends Controller
public function apiMediaUpload(RequestAbstract $request, ResponseAbstract $response, $data = null) : void public function apiMediaUpload(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{ {
$uploads = $this->uploadFiles( $uploads = $this->uploadFiles(
$request->getData('name') === null || \count($request->getFiles()) > 1 ? '' : $request->getData('name'), [$request->getData('name') === null ? '' : $request->getData('name')],
$request->getFiles(), $request->getFiles(),
$request->header->account, $request->header->account,
__DIR__ . '/../../../Modules/Media/Files' . \urldecode((string) ($request->getData('path') ?? '')), __DIR__ . '/../../../Modules/Media/Files' . \urldecode((string) ($request->getData('path') ?? '')),
@ -120,7 +120,7 @@ final class ApiController extends Controller
/** /**
* Upload a media file * Upload a media file
* *
* @param string $name Name * @param array $names Name
* @param array $files Files * @param array $files Files
* @param int $account Uploader * @param int $account Uploader
* @param string $basePath Base path. The path which is used for the upload. * @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 * @since 1.0.0
*/ */
public function uploadFiles( public function uploadFiles(
string $name, array $names,
array $files, array $files,
int $account, int $account,
string $basePath = '/Modules/Media/Files', string $basePath = '/Modules/Media/Files',
@ -147,7 +147,8 @@ final class ApiController extends Controller
string $password = '', string $password = '',
string $encryptionKey = '', string $encryptionKey = '',
int $pathSettings = PathSettings::RANDOM_PATH int $pathSettings = PathSettings::RANDOM_PATH
) : array { ) : array
{
if (empty($files)) { if (empty($files)) {
return []; return [];
} }
@ -167,7 +168,7 @@ final class ApiController extends Controller
$upload = new UploadFile(); $upload = new UploadFile();
$upload->outputDir = $outputDir; $upload->outputDir = $outputDir;
$status = $upload->upload($files, $name, $absolute, $encryptionKey); $status = $upload->upload($files, $names, $absolute, $encryptionKey);
return $this->createDbEntries($status, $account, $virtualPath, $type); return $this->createDbEntries($status, $account, $virtualPath, $type);
} }
@ -176,7 +177,7 @@ final class ApiController extends Controller
* Uploads a file to a destination * Uploads a file to a destination
* *
* @param array $files Files to upload * @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 * @param string $path Upload path
* *
* @return array * @return array
@ -185,13 +186,14 @@ final class ApiController extends Controller
*/ */
public static function uploadFilesToDestination( public static function uploadFilesToDestination(
array $files, array $files,
string $name = '', array $names = [],
string $path = '', string $path = '',
) : array { ) : array
{
$upload = new UploadFile(); $upload = new UploadFile();
$upload->outputDir = $path; $upload->outputDir = $path;
$status = $upload->upload($files, $name, true, ''); $status = $upload->upload($files, $names, true, '');
return $status; return $status;
} }
@ -212,6 +214,8 @@ final class ApiController extends Controller
} }
/** /**
* Create database entries for uploaded files
*
* @param array $status Files * @param array $status Files
* @param int $account Uploader * @param int $account Uploader
* @param string $virtualPath Virtual path * @param string $virtualPath Virtual path
@ -228,7 +232,8 @@ final class ApiController extends Controller
string $virtualPath = '', string $virtualPath = '',
string $type = '', string $type = '',
string $ip = '127.0.0.1' string $ip = '127.0.0.1'
) : array { ) : array
{
$mediaCreated = []; $mediaCreated = [];
foreach ($status as $uFile) { foreach ($status as $uFile) {

View File

@ -82,11 +82,11 @@ class UploadFile
/** /**
* Upload file to server. * Upload file to server.
* *
* @param array $files File data ($_FILE) * @param array $files File data ($_FILE)
* @param string $name File name * @param string[] $names File name
* @param bool $absolute Use absolute path * @param bool $absolute Use absolute path
* @param string $encryptionKey Encryption key * @param string $encryptionKey Encryption key
* @param string $encoding Encoding used for uploaded file. Empty string will not convert file content. * @param string $encoding Encoding used for uploaded file. Empty string will not convert file content.
* *
* @return array * @return array
* *
@ -96,26 +96,30 @@ class UploadFile
*/ */
public function upload( public function upload(
array $files, array $files,
string $name = '', array $names = [],
bool $absolute = false, bool $absolute = false,
string $encryptionKey = '', string $encryptionKey = '',
string $encoding = 'UTF-8' string $encoding = 'UTF-8'
) : array { ) : array
{
$result = []; $result = [];
if (\count($files) === \count($files, \COUNT_RECURSIVE)) { if (\count($files) === \count($files, \COUNT_RECURSIVE)) {
$files = [$files]; $files = [$files];
} }
$fileCount = \count($files);
if (!$absolute && \count($files) > 1) { if (!$absolute && \count($files) > 1) {
$this->outputDir = $this->findOutputDir(); $this->outputDir = $this->findOutputDir();
} }
$path = $this->outputDir; $path = $this->outputDir;
$fCounter = -1;
$areNamed = \count($files) === \count($names);
foreach ($files as $key => $f) { foreach ($files as $key => $f) {
++$fCounter;
$name = $areNamed ? $names[$fCounter] : '';
if ($path === '') { if ($path === '') {
$path = File::dirpath($f['tmp_name']); $path = File::dirpath($f['tmp_name']);
} }
@ -148,9 +152,7 @@ class UploadFile
} }
$split = \explode('.', $f['name']); $split = \explode('.', $f['name']);
$result[$key]['filename'] = \count($files) === 1 && !empty($name) $result[$key]['filename'] = !empty($name) ? $name : $f['name'];
? $name
: $f['name'];
$extension = \count($split) > 1 ? $split[\count($split) - 1] : ''; $extension = \count($split) > 1 ? $split[\count($split) - 1] : '';
$result[$key]['extension'] = $extension; $result[$key]['extension'] = $extension;
@ -160,7 +162,7 @@ class UploadFile
$result[$key]['filename'] = $name; $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 { try {
$name = $this->createFileName($path, $f['tmp_name'], $extension); $name = $this->createFileName($path, $f['tmp_name'], $extension);
$result[$key]['filename'] = $name; $result[$key]['filename'] = $name;

View File

@ -172,7 +172,7 @@ trait ApiControllerMediaTrait
{ {
self::assertEquals( self::assertEquals(
[], [],
$this->module->uploadFiles('test', ['test'], 1, '/test', '', '', '', '', 99) $this->module->uploadFiles(['test'], ['test'], 1, '/test', '', '', '', '', 99)
); );
} }