FilesUploadTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Services\File\Upload;
  3. use App\Http\Resources\API\FileUploadSuccessResource;
  4. use App\Models\BimFile;
  5. use App\Models\ContainerContent;
  6. use App\Models\Enums\FileObjectType;
  7. use App\Models\File;
  8. use App\Services\File\BIM\BIMFactory;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\UploadedFile;
  11. use Illuminate\Support\Facades\Auth;
  12. trait FilesUploadTrait
  13. {
  14. protected ?FileObjectType $fileObjectType = null;
  15. protected $object = null;
  16. protected function checkRequestData(Request $request): int
  17. {
  18. $this->fileObjectType = FileObjectType::from($request->object_type);
  19. if ($request->object_id) {
  20. $this->object = $this->fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
  21. }
  22. $filesSize = 0;
  23. foreach ($request->file("files") as $file) {
  24. throw_validation_if(! $file->isValid(), "File upload failed.");
  25. $filesSize += $file->getSize();
  26. }
  27. throw_validation_if(
  28. $filesSize + Auth::user()->company->used_storage_capacity > Auth::user()->company->storage_size,
  29. "Storage capacity is insufficient, please contact the administrator."
  30. );
  31. return $filesSize;
  32. }
  33. protected function uploadFile(Request $request, UploadedFile $file): array
  34. {
  35. $pathname = $file->storeAs(
  36. sprintf("c%s/%s/%s", Auth::user()->company_id, $request->get("object_type"), date("Ymd")),
  37. sprintf("%s.%s", md5(uniqid()), $file->extension())
  38. );
  39. $data = [
  40. 'bim' => [],
  41. ];
  42. $extension = $file->extension() ? $file->extension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
  43. $isBimFile = 0;
  44. if (in_array($extension, config("bim.extensions")) && $request->object_type == FileObjectType::CONTAINER->value) {
  45. $isBimFile = true;
  46. $data['bim'] = BIMFactory::make()->uploadFile($file);
  47. }
  48. throw_validation_if(! $pathname, "File upload failed.");
  49. $data['file'] = [
  50. 'pathname' => $pathname,
  51. 'title' => $file->getClientOriginalName(),
  52. 'size' => $file->getSize(),
  53. 'extension' => $extension,
  54. 'object_type' => $request->object_type,
  55. 'object_id' => $request->object_id,
  56. 'created_by' => Auth::id(),
  57. 'company_id' => Auth::user()->company_id,
  58. 'source' => $request->get("source", 1),
  59. 'uuid' => $request->get("uuid"),
  60. 'is_bim' => $isBimFile,
  61. ];
  62. return $data;
  63. }
  64. protected function storeFiles(array $items)
  65. {
  66. $uploadedFiles = [];
  67. foreach ($items as $item) {
  68. $fileInfo = $item['file'];
  69. if ($fileInfo['object_id'] && $fileInfo['source'] == 1||$fileInfo['uuid'] && $fileInfo['source']== 1) {
  70. $version = File::query()
  71. ->where('object_type', $fileInfo['object_type'])
  72. ->where('object_id', $fileInfo['object_id'])
  73. ->where("title", $fileInfo['title'])
  74. ->where("source", 1)
  75. ->where("folder_id", $fileInfo['folder_id'] ?? 0)
  76. ->count();
  77. $fileInfo['version'] = $version + 1;
  78. $fileInfo['is_latest_version'] = 1;
  79. File::query()
  80. ->where('object_type', $fileInfo['object_type'])
  81. ->where('object_id', $fileInfo['object_id'])
  82. ->where("title", $fileInfo['title'])
  83. ->where("folder_id", $fileInfo['folder_id'] ?? 0)
  84. ->where("source", 1)
  85. ->where("is_latest_version", 1)
  86. ->update([
  87. 'is_latest_version' => 0
  88. ]);
  89. }
  90. $file = File::query()->create($fileInfo);
  91. if (isset($item['bim']) && $item['bim']) {
  92. BimFile::query()->create([
  93. 'file_id' => $file->id,
  94. ...$item['bim']
  95. ]);
  96. }
  97. $uploadedFiles[] = new FileUploadSuccessResource($file);
  98. }
  99. $this->updateObjectVersion();
  100. return $uploadedFiles;
  101. }
  102. protected function updateObjectVersion()
  103. {
  104. if (!$this->object) {
  105. return;
  106. }
  107. if ($this->fileObjectType == FileObjectType::CONTAINER) {
  108. $version = $this->object->version;
  109. $this->object->version++;
  110. $this->object->save();
  111. $fileIds = File::query()->where([
  112. 'object_type' => $this->fileObjectType->value,
  113. 'object_id' => $this->object->id
  114. ])->pluck("id");
  115. ContainerContent::query()->create([
  116. ...$this->object->content($version)->first()->toArray(),
  117. 'created_by' => Auth::id(),
  118. 'files' => $fileIds->join(","),
  119. 'version' => $this->object->version,
  120. ]);
  121. }
  122. }
  123. protected function updateUsedStorageCapacity(int $filesSize)
  124. {
  125. $company = Auth::user()->company;
  126. $company->increment("used_storage_capacity", $filesSize);
  127. $company->save();
  128. }
  129. }