123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Services\File;
- use App\Exceptions\ValidationException;
- use App\Models\Enums\FileObjectType;
- use App\Models\Enums\FileSource;
- use App\Models\File;
- use App\Models\Library;
- use App\Models\NamingRule;
- use App\Services\NamingRule\NamingRuleCheck;
- use Carbon\Carbon;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Auth;
- class FileAssociationService
- {
- protected array $fileIds = [];
- protected ?Collection $files = null;
- protected ?FileObjectType $fileObjectType = null;
- public function check(array $fileIds, FileObjectType $fileObjectType, string $fileUUID = null)
- {
- if (! $fileIds) {
- return;
- }
- $this->fileIds = $fileIds;
- $this->fileObjectType = $fileObjectType;
- $this->files = File::query()
- ->with(['folder'])
- ->whereIn("id", $fileIds)
- ->where("created_by", Auth::id())
- ->whereNull("object_id")
- ->where("object_type", $fileObjectType->value)
- ->where("created_at", ">=", Carbon::now()->subHours(3))
- ->orderBy("created_at")
- ->get();
- foreach ($this->files as $file) {
- if (! $file->folder_id) {
- continue;
- }
- throw_validation_if($fileUUID != $file->folder->uuid, "illegal uuid");
- throw_validation_if($file->folder->object_id, "The folder has been associated and cannot be associated again");
- }
- }
- public function fileNameNamingRuleCheckByContainer(int $libraryId, int $namingRuleId = 0)
- {
- if (! $namingRuleId) {
- return;
- }
- $library = Library::query()->findOrFail($libraryId);
- $namingRule = NamingRule::query()->findOrFail($namingRuleId);
- $namingRuleCheck = new NamingRuleCheck();
- $code = $namingRuleCheck->getNamingRuleCode($library);
- $errors = [];
- if(empty($this->files)){
- return ;
- }
- foreach ($this->files as $file) {
- $fileName = pathinfo($file->title)['filename'];
- $result = $namingRuleCheck->checkByName($fileName, $namingRule, $code);
- if (! $result['result']) {
- $errors[] = [
- ...$result,
- 'name' => $file->title,
- 'file_id' => $file->id
- ];
- }
- }
- if ($errors) {
- throw new ValidationException('The file name does not conform to the naming rules', errors: [
- 'naming_rule' => $errors
- ]);
- }
- }
- public function association(string $objectId,)
- {
- if (! $this->files) {
- return;
- }
- foreach ($this->files as $file) {
- if ($file->source == FileSource::ATTACHMENT->value) {
- $version = File::query()
- ->where("created_by", Auth::id())
- ->where("object_id", $objectId)
- ->where("object_type", $this->fileObjectType->value)
- ->where("title", $file->title)
- ->where("folder_id", $file->folder_id)
- ->count();
- $file->version = $version + 1;
- }
- $file->object_id = $objectId;
- $file->save();
- if ($file->folder) {
- $file->folder->object_id = $objectId;
- $file->folder->save();
- }
- }
- }
- }
|