KeepDirectoryUploadService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Services\File\Upload;
  3. use App\Models\Folder;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. class KeepDirectoryUploadService
  7. {
  8. use FilesUploadTrait;
  9. protected array $objectWhere = [];
  10. protected ?Folder $parentFolder = null;
  11. protected int $filesSize = 0;
  12. public function __construct(protected Request $request)
  13. {
  14. $this->checkFormData();
  15. $this->objectWhere = [
  16. 'object_id' => $this->request->get("object_id", 0),
  17. 'object_type' => $this->request->get("object_type"),
  18. ];
  19. $this->parentFolder = $this->request->get("folder_id")
  20. ? Folder::query()->where($this->objectWhere)->findOrFail($this->request->get("folder_id"))
  21. : null;
  22. }
  23. public function upload(): array
  24. {
  25. $folderRelations = $this->buildFolder();
  26. $items = [];
  27. $fileNames = $this->request->get("file_names", []);
  28. foreach ($this->request->file("files") as $index => $file) {
  29. $item = $this->uploadFile($this->request, $file);
  30. $item['file']['folder_id'] = $folderRelations[$index] ?? 0;
  31. $item['file']['title'] = $fileNames[$index] ?? $item['file']['title'];
  32. $items[] = $item;
  33. }
  34. $uploadedFiles = $this->storeFiles($items);
  35. $this->updateUsedStorageCapacity($this->filesSize);
  36. return $uploadedFiles;
  37. }
  38. protected function buildFolder(): array
  39. {
  40. $folderRelations = [];
  41. $uuid = $this->request->get("uuid");
  42. foreach ($this->request->folders as $index => $folder) {
  43. $parentFolder = $this->parentFolder;
  44. $folderPathArr = array_filter(explode('/', $folder));
  45. if (! $folderPathArr) {
  46. $folderRelations[$index] = $parentFolder?->id ?? 0;
  47. }
  48. foreach ($folderPathArr as $folderName) {
  49. $parentId = $parentFolder?->id ?? 0;
  50. $folder = Folder::query()
  51. ->where($this->objectWhere)
  52. ->where("name", $folderName)
  53. ->when($uuid, fn($query) => $query->where("uuid", $uuid))
  54. ->where("parent_id", $parentId)
  55. ->first();
  56. if (! $folder) {
  57. $folder = Folder::query()->create([
  58. 'company_id' => Auth::user()->company_id,
  59. ...$this->objectWhere,
  60. 'uuid' => $uuid,
  61. 'parent_id' => $parentId,
  62. 'name' => $folderName
  63. ]);
  64. $folder->path = $parentFolder ? $parentFolder?->path . $folder->id . "," : sprintf(",%s,", $folder->id);
  65. $folder->save();
  66. }
  67. $parentFolder = $folder;
  68. $folderRelations[$index] = $folder->id;
  69. }
  70. }
  71. return $folderRelations;
  72. }
  73. protected function checkFormData()
  74. {
  75. throw_validation_if(
  76. count($this->request->file("files")) != count($this->request->folders),
  77. "File and directory do not match"
  78. );
  79. $this->filesSize = $this->checkRequestData($this->request);
  80. }
  81. }