FileAssociationService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Services\File;
  3. use App\Exceptions\ValidationException;
  4. use App\Models\Enums\FileObjectType;
  5. use App\Models\Enums\FileSource;
  6. use App\Models\File;
  7. use App\Models\Library;
  8. use App\Models\NamingRule;
  9. use App\Services\NamingRule\NamingRuleCheck;
  10. use Carbon\Carbon;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Auth;
  13. class FileAssociationService
  14. {
  15. protected array $fileIds = [];
  16. protected ?Collection $files = null;
  17. protected ?FileObjectType $fileObjectType = null;
  18. public function check(array $fileIds, FileObjectType $fileObjectType, string $fileUUID = null)
  19. {
  20. if (! $fileIds) {
  21. return;
  22. }
  23. $this->fileIds = $fileIds;
  24. $this->fileObjectType = $fileObjectType;
  25. $this->files = File::query()
  26. ->with(['folder'])
  27. ->whereIn("id", $fileIds)
  28. ->where("created_by", Auth::id())
  29. ->whereNull("object_id")
  30. ->where("object_type", $fileObjectType->value)
  31. ->where("created_at", ">=", Carbon::now()->subHours(3))
  32. ->orderBy("created_at")
  33. ->get();
  34. foreach ($this->files as $file) {
  35. if (! $file->folder_id) {
  36. continue;
  37. }
  38. throw_validation_if($fileUUID != $file->folder->uuid, "illegal uuid");
  39. throw_validation_if($file->folder->object_id, "The folder has been associated and cannot be associated again");
  40. }
  41. }
  42. public function fileNameNamingRuleCheckByContainer(int $libraryId, int $namingRuleId = 0)
  43. {
  44. if (! $namingRuleId) {
  45. return;
  46. }
  47. $library = Library::query()->findOrFail($libraryId);
  48. $namingRule = NamingRule::query()->findOrFail($namingRuleId);
  49. $namingRuleCheck = new NamingRuleCheck();
  50. $code = $namingRuleCheck->getNamingRuleCode($library);
  51. $errors = [];
  52. if(empty($this->files)){
  53. return ;
  54. }
  55. foreach ($this->files as $file) {
  56. $fileName = pathinfo($file->title)['filename'];
  57. $result = $namingRuleCheck->checkByName($fileName, $namingRule, $code);
  58. if (! $result['result']) {
  59. $errors[] = [
  60. ...$result,
  61. 'name' => $file->title,
  62. 'file_id' => $file->id
  63. ];
  64. }
  65. }
  66. if ($errors) {
  67. throw new ValidationException('The file name does not conform to the naming rules', errors: [
  68. 'naming_rule' => $errors
  69. ]);
  70. }
  71. }
  72. public function association(string $objectId,)
  73. {
  74. if (! $this->files) {
  75. return;
  76. }
  77. foreach ($this->files as $file) {
  78. if ($file->source == FileSource::ATTACHMENT->value) {
  79. $version = File::query()
  80. ->where("created_by", Auth::id())
  81. ->where("object_id", $objectId)
  82. ->where("object_type", $this->fileObjectType->value)
  83. ->where("title", $file->title)
  84. ->where("folder_id", $file->folder_id)
  85. ->count();
  86. $file->version = $version + 1;
  87. }
  88. $file->object_id = $objectId;
  89. $file->save();
  90. if ($file->folder) {
  91. $file->folder->object_id = $objectId;
  92. $file->folder->save();
  93. }
  94. }
  95. }
  96. }