diff --git a/Admin/Installer.php b/Admin/Installer.php index 51cf4fb..4ba9006 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -20,6 +20,12 @@ use Modules\Media\Models\CollectionMapper; use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\Module\InstallerAbstract; use phpOMS\System\File\PathException; +use phpOMS\System\File\Local\Directory; +use phpOMS\System\File\Local\File; +use Modules\Media\Models\UploadFile; +use Modules\Media\Models\Media; +use Modules\Media\Models\MediaMapper; +use Modules\Media\Controller\ApiController; /** * Installer class. @@ -66,13 +72,26 @@ final class Installer extends InstallerAbstract throw new \Exception(); // @codeCoverageIgnore } - foreach ($mediaData as $media) { - self::installMedia($dbPool, $media); + if (\is_dir(__DIR__ . '/tmp')) { + Directory::delete(__DIR__ . '/tmp'); } + + \mkdir(__DIR__ . '/tmp'); + foreach ($mediaData as $media) { + switch ($media['type']) { + case 'collection': + self::createCollection($dbPool, $media); + break; + case 'upload': + self::uploadMedia($dbPool, $media); + break; + } + } + Directory::delete(__DIR__ . '/tmp'); } /** - * Install media element. + * Create collection. * * @param DatabasePool $dbPool Database instance * @param array $data Media info @@ -81,7 +100,7 @@ final class Installer extends InstallerAbstract * * @since 1.0.0 */ - private static function installMedia($dbPool, $data) : void + private static function createCollection($dbPool, $data) : void { $collection = new Collection(); $collection->name = (string) $data['name'] ?? ''; @@ -91,4 +110,85 @@ final class Installer extends InstallerAbstract CollectionMapper::create($collection); } + + /** + * Upload media. + * + * @param DatabasePool $dbPool Database instance + * @param array $data Media info + * + * @return void + * + * @since 1.0.0 + */ + private static function uploadMedia($dbPool, $data) : void + { + $files = []; + foreach ($data['files'] as $file) { + if (\is_file(__DIR__ . '/../../..' . $file)) { + File::copy(__DIR__ . '/../../..' . $file, __DIR__ . '/tmp' . $file); + + $files[] = [ + 'size' => \filesize(__DIR__ . '/tmp' . $file), + 'name' => \basename($file), + 'tmp_name' => __DIR__ . '/tmp' . $file, + 'error' => \UPLOAD_ERR_OK, + ]; + } if (\is_dir(__DIR__ . '/../../..' . $file)) { + Directory::copy(__DIR__ . '/../../..' . $file, __DIR__ . '/tmp' . $file); + + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator(__DIR__ . '/tmp' . $file . '/', \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::SELF_FIRST + ); + + foreach ($iterator as $item) { + if ($item->isDir()) { + continue; + } + + $files[] = [ + 'size' => \filesize($item->getPathname()), + 'name' => \basename($item->getPathname()), + 'tmp_name' => $item->getPathname(), + 'error' => \UPLOAD_ERR_OK, + ]; + } + } + } + + $upload = new UploadFile(); + $upload->setOutputDir(empty($data['path'] ?? '') ? ApiController::createMediaPath() : __DIR__ . '/../../..' . $data['path']); + + $status = $upload->upload($files, $data['name'], true); + + $mediaFiles = []; + foreach ($status as $uFile) { + $media = new Media(); + + $media->setPath(ApiController::normalizeDbPath($data['path']) . '/' . $uFile['filename']); + $media->name = $uFile['name']; + $media->size = $uFile['size']; + $media->createdBy = new NullAccount((int) $data['user'] ?? 1); + $media->extension = $uFile['extension']; + $media->setVirtualPath((string) ($data['virtualPath'] ?? '/') . '/' . $data['name']); + $media->type = $data['type'] ?? ''; // = identifier for modules + + MediaMapper::create($media); + + $mediaFiles[] = $media; + } + + if ($data['create_collection']) { + $collection = new Collection(); + $collection->name = (string) $data['name'] ?? ''; + $collection->setVirtualPath((string) $data['virtualPath'] ?? '/'); + $collection->setPath((string) ($data['path'] ?? '/Modules/Media/Files/' . ((string) $data['name'] ?? ''))); + $collection->createdBy = new NullAccount((int) $data['user'] ?? 1); + + $collection->setSources($mediaFiles); + + CollectionMapper::create($collection); + } + } } diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 3ab5a00..bbdc4eb 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -162,7 +162,7 @@ final class ApiController extends Controller * * @since 1.0.0 */ - public static function createMediaPath(string $basePath = 'Modules/Media/Files') : string + public static function createMediaPath(string $basePath = '/Modules/Media/Files') : string { $rndPath = \str_pad(\dechex(\mt_rand(0, 65535)), 4, '0', \STR_PAD_LEFT); return $basePath . '/' . $rndPath[0] . $rndPath[1] . '/' . $rndPath[2] . $rndPath[3]; @@ -172,7 +172,7 @@ final class ApiController extends Controller * @param array $status Files * @param int $account Uploader * @param string $virtualPath Virtual path - * @param string $type Media type (internal categorization) + * @param string $type Media type (internal categorization = identifier for modules) * @param string $ip Ip * * @return Media[] @@ -254,18 +254,18 @@ final class ApiController extends Controller * * @since 1.0.0 */ - private static function normalizeDbPath(string $path) : string + public static function normalizeDbPath(string $path) : string { $realpath = \realpath(__DIR__ . '/../../../'); if ($realpath === false) { throw new \Exception(); // @codeCoverageIgnore } - return \str_replace('\\', '/', + return FileUtils::absolute(\str_replace('\\', '/', \str_replace($realpath, '', \rtrim($path, '\\/') ) - ); + )); } /** diff --git a/Models/UploadFile.php b/Models/UploadFile.php index 83bc015..b15b72b 100755 --- a/Models/UploadFile.php +++ b/Models/UploadFile.php @@ -153,8 +153,13 @@ class UploadFile return $result; } - $split = \explode('.', $f['name']); - $result[$key]['name'] = \count($files) === 1 && !empty($name) ? $name : $split[0]; + $split = \explode('.', $f['name']); + $result[$key]['name'] = \count($files) === 1 && !empty($name) + ? $name + : (\count($split) > 1 + ? \substr($f['name'], 0, \strripos($f['name'], '.')) + : $f['name']); + $extension = \count($split) > 1 ? $split[\count($split) - 1] : ''; $result[$key]['extension'] = $extension;