From c63bc0c75ae2f9a1b29c8fed9a7517b31fda963b Mon Sep 17 00:00:00 2001 From: ArrayIterator Date: Thu, 16 Nov 2023 07:55:58 +0700 Subject: [PATCH] add changes --- Entities/Attachment.php | 147 +++++++++++++++++++++ Entities/UserAttachment.php | 147 +++++++++++++++++++++ Languages/{media-module.pot => module.pot} | 35 +---- Media.php | 28 ++-- Servant/DataServe.php | 1 - Traits/MediaFilterTrait.php | 2 +- Uploader/Abstracts/AbstractUploader.php | 46 ++++--- phpcs.xml | 14 +- 8 files changed, 344 insertions(+), 76 deletions(-) create mode 100644 Entities/Attachment.php create mode 100644 Entities/UserAttachment.php rename Languages/{media-module.pot => module.pot} (58%) diff --git a/Entities/Attachment.php b/Entities/Attachment.php new file mode 100644 index 0000000..fa2157a --- /dev/null +++ b/Entities/Attachment.php @@ -0,0 +1,147 @@ + 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'comment' => 'Attachments created by admin user', + ] +)] +#[UniqueConstraint( + name: 'unique_path_storage_type', + columns: ['path', 'storage_type'] +)] +#[Index( + columns: ['id', 'site_id'], + name: 'index_id_site_id' +)] +#[Index( + columns: ['storage_type', 'mime_type'], + name: 'index_storage_type_mime_type' +)] +#[Index( + columns: ['user_id'], + name: 'relation_attachments_user_id_admins_id' +)] +#[Index( + columns: ['site_id'], + name: 'relation_attachments_site_id_sites_id' +)] +#[Index( + columns: ['name', 'file_name', 'status', 'mime_type', 'storage_type', 'site_id'], + name: 'index_name_file_name_status_mime_type_storage_type_site_id' +)] +#[HasLifecycleCallbacks] +class Attachment extends AbstractAttachment +{ + public const TABLE_NAME = 'attachments'; + + #[Column( + name: 'site_id', + type: Types::BIGINT, + length: 20, + nullable: true, + options: [ + 'unsigned' => true, + 'default' => null, + 'comment' => 'Site id' + ] + )] + protected ?int $site_id = null; + #[ + JoinColumn( + name: 'site_id', + referencedColumnName: 'id', + nullable: true, + onDelete: 'RESTRICT', + options: [ + 'relation_name' => 'relation_attachments_site_id_sites_id', + 'onUpdate' => 'CASCADE', + 'onDelete' => 'RESTRICT' + ] + ), + ManyToOne( + targetEntity: Site::class, + cascade: [ + "persist" + ], + fetch: 'EAGER' + ) + ] + protected ?Site $site = null; + + #[ + JoinColumn( + name: 'user_id', + referencedColumnName: 'id', + nullable: true, + onDelete: 'RESTRICT', + options: [ + 'relation_name' => 'relation_attachments_user_id_admins_id', + 'onUpdate' => 'CASCADE', + 'onDelete' => 'RESTRICT' + ], + ), + ManyToOne( + targetEntity: Admin::class, + cascade: [ + 'persist' + ], + fetch: 'LAZY', + ) + ] + protected ?Admin $user = null; + + public function setUser(?Admin $user): void + { + $this->user = $user; + $this->setUserId($user?->getId()); + } + + public function getUser(): ?Admin + { + return $this->user; + } + + public function getSiteId(): ?int + { + return $this->site_id; + } + + public function setSiteId(?int $site_id): void + { + $this->site_id = $site_id; + } + + public function getSite(): ?Site + { + return $this->site; + } + + public function setSite(?Site $site): void + { + $this->site = $site; + $this->setSiteId($site?->getId()); + } +} diff --git a/Entities/UserAttachment.php b/Entities/UserAttachment.php new file mode 100644 index 0000000..ee06441 --- /dev/null +++ b/Entities/UserAttachment.php @@ -0,0 +1,147 @@ + 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'comment' => 'Attachments created by common user', + ] +)] +#[UniqueConstraint( + name: 'unique_path_storage_type', + columns: ['path', 'storage_type'] +)] +#[Index( + columns: ['id', 'site_id'], + name: 'index_id_site_id' +)] +#[Index( + columns: ['storage_type', 'mime_type'], + name: 'index_storage_type_mime_type' +)] +#[Index( + columns: ['user_id'], + name: 'relation_user_attachments_user_id_users_id' +)] +#[Index( + columns: ['site_id'], + name: 'relation_user_attachments_site_id_sites_id' +)] +#[Index( + columns: ['name', 'file_name', 'status', 'mime_type', 'storage_type', 'site_id'], + name: 'index_name_file_name_status_mime_type_storage_type_site_id' +)] +#[HasLifecycleCallbacks] +class UserAttachment extends AbstractAttachment +{ + public const TABLE_NAME = 'user_attachments'; + + #[Column( + name: 'site_id', + type: Types::BIGINT, + length: 20, + nullable: true, + options: [ + 'unsigned' => true, + 'comment' => 'Site id' + ] + )] + protected ?int $site_id = null; + + #[ + JoinColumn( + name: 'site_id', + referencedColumnName: 'id', + nullable: true, + onDelete: 'RESTRICT', + options: [ + 'relation_name' => 'relation_user_attachments_site_id_sites_id', + 'onUpdate' => 'CASCADE', + 'onDelete' => 'RESTRICT' + ] + ), + ManyToOne( + targetEntity: Site::class, + cascade: [ + "persist" + ], + fetch: 'EAGER' + ) + ] + protected ?Site $site = null; + + #[ + JoinColumn( + name: 'user_id', + referencedColumnName: 'id', + nullable: true, + onDelete: 'RESTRICT', + options: [ + 'relation_name' => 'relation_user_attachments_user_id_users_id', + 'onUpdate' => 'CASCADE', + 'onDelete' => 'RESTRICT' + ], + ), + ManyToOne( + targetEntity: User::class, + cascade: [ + 'persist' + ], + fetch: 'LAZY', + ) + ] + protected ?User $user = null; + + public function setUser(?User $user): void + { + $this->user = $user; + $this->setUserId($user?->getId()); + } + + public function getUser(): ?User + { + return $this->user; + } + + public function getSiteId(): ?int + { + return $this->site_id; + } + + public function setSiteId(?int $site_id): void + { + $this->site_id = $site_id; + } + + public function getSite(): ?Site + { + return $this->site; + } + + public function setSite(?Site $site): void + { + $this->site = $site; + $this->setSiteId($site?->getId()); + } +} diff --git a/Languages/media-module.pot b/Languages/module.pot similarity index 58% rename from Languages/media-module.pot rename to Languages/module.pot index 3b37d92..171fed7 100644 --- a/Languages/media-module.pot +++ b/Languages/module.pot @@ -25,37 +25,4 @@ msgstr "" "X-Poedit-SearchPathExcluded-0: *.css\n" "X-Poedit-SearchPathExcluded-1: *.html\n" "X-Poedit-SearchPathExcluded-2: *.json\n" - -#: Media.php:43 -msgctxt "module" -msgid "Media Manager" -msgstr "" - -#: Media.php:52 -msgctxt "module" -msgid "" -"Module to make application support media & file " -"attachments" -msgstr "" - -#: Uploader/Abstracts/AbstractUploader.php:128 -msgctxt "module" -msgid "File does not have file name" -msgstr "" - -#: Uploader/Abstracts/AbstractUploader.php:143 -#, php-format -msgctxt "module" -msgid "Could not determine file type from mimetype %s" -msgstr "" - -#: Uploader/Abstracts/AbstractUploader.php:259 -msgctxt "module" -msgid "Could not save uploaded file" -msgstr "" - -#: Uploader/Abstracts/AbstractUploader.php:271 -msgctxt "module" -msgid "" -"Could not save uploaded file & determine target file." -msgstr "" +"X-Poedit-SearchPathExcluded-3: *.json\n" diff --git a/Media.php b/Media.php index ce89752..0cd8919 100644 --- a/Media.php +++ b/Media.php @@ -8,11 +8,12 @@ use ArrayAccess\TrayDigita\App\Modules\Media\Traits\MediaPathTrait; use ArrayAccess\TrayDigita\App\Modules\Media\Uploader\AdminUpload; use ArrayAccess\TrayDigita\App\Modules\Media\Uploader\UserUpload; -use ArrayAccess\TrayDigita\L10n\Translations\Adapter\Gettext\PoMoAdapter; use ArrayAccess\TrayDigita\Module\AbstractModule; +use ArrayAccess\TrayDigita\Traits\Database\ConnectionTrait; use ArrayAccess\TrayDigita\Traits\Service\TranslatorTrait; use ArrayAccess\TrayDigita\Uploader\Chunk; use ArrayAccess\TrayDigita\Uploader\StartProgress; +use ArrayAccess\TrayDigita\Util\Filter\Consolidation; use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UploadedFileInterface; @@ -21,6 +22,7 @@ final class Media extends AbstractModule { use TranslatorTrait, MediaPathTrait, + ConnectionTrait, MediaFilterTrait; protected ?ServerRequestInterface $request = null; @@ -41,8 +43,8 @@ public function getName(): string { return $this->translateContext( 'Media Manager', - 'module', - 'media-module' + 'media-module', + 'module' ); } @@ -50,27 +52,24 @@ public function getDescription(): string { return $this->translateContext( 'Module to make application support media & file attachments', - 'module', - 'media-module' + 'media-module', + 'module' ); } protected function doInit(): void { + /** @noinspection DuplicatedCode */ if ($this->didInit) { return; } + Consolidation::registerAutoloader(__NAMESPACE__, __DIR__); $this->didInit = true; - foreach ($this->getTranslator()?->getAdapters()??[] as $adapter) { - if ($adapter instanceof PoMoAdapter) { - $adapter->registerDirectory( - __DIR__ .'/Languages', - 'media-module' - ); - } - } - + $kernel = $this->getKernel(); + $kernel->registerControllerDirectory(__DIR__ .'/Controllers'); + $this->getTranslator()?->registerDirectory('module', __DIR__ . '/Languages'); + $this->getConnection()->registerEntityDirectory(__DIR__.'/Entities'); $this->doFilterPath(); } @@ -84,7 +83,6 @@ public function setRequest(ServerRequestInterface $request): void $this->request = $request; } - /** @noinspection PhpUnused */ public function getDataServe(): DataServe { return $this->dataServe ??= new DataServe($this); diff --git a/Servant/DataServe.php b/Servant/DataServe.php index b3891c3..65506ad 100644 --- a/Servant/DataServe.php +++ b/Servant/DataServe.php @@ -75,7 +75,6 @@ public function getMimeType(string $file): ?string return MimeType::fileMimeType($file); } - /** @noinspection PhpUnused */ public function getLastModified(string $file) : ?DateTimeImmutable { $file = $this->getNormalizeFile($file); diff --git a/Traits/MediaFilterTrait.php b/Traits/MediaFilterTrait.php index 23f6c18..a8839ca 100644 --- a/Traits/MediaFilterTrait.php +++ b/Traits/MediaFilterTrait.php @@ -36,7 +36,7 @@ trait MediaFilterTrait { - const SCRIPT_REGEX = '~\.(?: + public const SCRIPT_REGEX = '~\.(?: py # python |php[0-9]*|hh # php |[xp]?html[0-9]* # html diff --git a/Uploader/Abstracts/AbstractUploader.php b/Uploader/Abstracts/AbstractUploader.php index 2733289..bb61a9f 100644 --- a/Uploader/Abstracts/AbstractUploader.php +++ b/Uploader/Abstracts/AbstractUploader.php @@ -3,22 +3,21 @@ namespace ArrayAccess\TrayDigita\App\Modules\Media\Uploader\Abstracts; +use ArrayAccess\TrayDigita\App\Modules\Media\Entities\Attachment; +use ArrayAccess\TrayDigita\App\Modules\Media\Entities\UserAttachment; use ArrayAccess\TrayDigita\App\Modules\Media\Media; use ArrayAccess\TrayDigita\App\Modules\Media\Uploader\UploadedFileMetadata; use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Admin; -use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Attachment; use ArrayAccess\TrayDigita\App\Modules\Users\Entities\User; -use ArrayAccess\TrayDigita\App\Modules\Users\Entities\UserAttachment; -use ArrayAccess\TrayDigita\Database\Connection; use ArrayAccess\TrayDigita\Database\Entities\Abstracts\AbstractAttachment; use ArrayAccess\TrayDigita\Database\Entities\Abstracts\AbstractUser; use ArrayAccess\TrayDigita\Exceptions\Runtime\RuntimeException; use ArrayAccess\TrayDigita\Http\UploadedFile; +use ArrayAccess\TrayDigita\Traits\Database\ConnectionTrait; use ArrayAccess\TrayDigita\Traits\Service\TranslatorTrait; use ArrayAccess\TrayDigita\Uploader\Exceptions\UploadedFileExtensionException; use ArrayAccess\TrayDigita\Uploader\Exceptions\UploadedFileNameException; use ArrayAccess\TrayDigita\Util\Filter\Consolidation; -use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; use ArrayAccess\TrayDigita\Util\Filter\MimeType; use Psr\Container\ContainerInterface; use Psr\Http\Message\ServerRequestInterface; @@ -38,11 +37,14 @@ class AbstractUploader { - final const TYPE_UPLOAD = AbstractAttachment::TYPE_UPLOAD; - final const TYPE_DATA = AbstractAttachment::TYPE_DATA; - final const TYPE_AVATAR = AbstractAttachment::TYPE_AVATAR; + final public const TYPE_UPLOAD = AbstractAttachment::TYPE_UPLOAD; - use TranslatorTrait; + final public const TYPE_DATA = AbstractAttachment::TYPE_DATA; + + final public const TYPE_AVATAR = AbstractAttachment::TYPE_AVATAR; + + use TranslatorTrait, + ConnectionTrait; public function __construct(public readonly Media $media) { @@ -98,7 +100,7 @@ public function uploadData( public function uploadPublic( ServerRequestInterface $request, UploadedFileInterface $uploadedFile, - AbstractUser $user + ?AbstractUser $user ): UploadedFileMetadata { return $this->uploadAttachment( $request, @@ -126,8 +128,8 @@ public function uploadAttachment( throw new UploadedFileNameException( $this->translateContext( 'File does not have file name', - 'module', - 'media-module' + 'media-module', + 'module' ) ); } @@ -141,18 +143,15 @@ public function uploadAttachment( sprintf( $this->translateContext( 'Could not determine file type from mimetype %s', - 'module', - 'media-module' + 'media-module', + 'module' ), $uploadedFile->getClientMediaType() ) ); } - $em = ContainerHelper::service( - Connection::class, - $this->getContainer() - )->getEntityManager(); + $em = $this->getEntityManager(); /** * @var class-string $className */ @@ -257,8 +256,8 @@ function_exists('mime_content_type') throw new RuntimeException( $this->translateContext( 'Could not save uploaded file', - 'module', - 'media-module' + 'media-module', + 'module' ) ); } @@ -269,8 +268,8 @@ function_exists('mime_content_type') throw new RuntimeException( $this->translateContext( 'Could not save uploaded file & determine target file.', - 'module', - 'media-module' + 'media-module', + 'module' ) ); } @@ -293,6 +292,11 @@ function_exists('mime_content_type') if ($user instanceof Admin || $user instanceof User) { $attachment->setUser($user); $attachment->setUserId($user->getId()); + $attachment->setSite($user->getSite()); + } else { + $attachment->setUser(null); + $attachment->setUserId(null); + $attachment->setSite(null); } $attachment->setFileName($originalFileName); diff --git a/phpcs.xml b/phpcs.xml index c3a5121..73bc37a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -24,10 +24,16 @@ - Servant/ - Traits/ - Uploader/ - Media.php + + . + vendor/ + + + + + + +