KeepDirectoryUploadService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. protected ?ProgressBar $progressBar = null;
  13. public function __construct(protected Request $request)
  14. {
  15. $this->checkFormData();
  16. $this->objectWhere = [
  17. 'object_id' => $this->request->get("object_id", 0),
  18. 'object_type' => $this->request->get("object_type"),
  19. ];
  20. $this->parentFolder = $this->request->get("folder_id")
  21. ? Folder::query()->where($this->objectWhere)->findOrFail($this->request->get("folder_id"))
  22. : null;
  23. }
  24. public function upload(): array
  25. {
  26. $folderRelations = $this->buildFolder();
  27. $items = [];
  28. $fileNames = $this->request->get("file_names", []);
  29. foreach ($this->request->file("files") as $index => $file) {
  30. $item = $this->uploadFile($this->request, $file, $this->progressBar);
  31. $item['file']['folder_id'] = $folderRelations[$index] ?? 0;
  32. $item['file']['title'] = $fileNames[$index] ?? $item['file']['title'];
  33. $items[] = $item;
  34. }
  35. $uploadedFiles = $this->storeFiles($items);
  36. $this->updateUsedStorageCapacity($this->filesSize);
  37. $this->progressBar?->completed();
  38. return $uploadedFiles;
  39. }
  40. protected function buildFolder(): array
  41. {
  42. $folderRelations = [];
  43. $uuid = $this->request->get("uuid");
  44. foreach ($this->request->folders as $index => $folder) {
  45. $parentFolder = $this->parentFolder;
  46. $folderPathArr = array_filter(explode('/', $folder));
  47. if (! $folderPathArr) {
  48. $folderRelations[$index] = $parentFolder?->id ?? 0;
  49. }
  50. foreach ($folderPathArr as $folderName) {
  51. $parentId = $parentFolder?->id ?? 0;
  52. $folder = Folder::query()
  53. ->where($this->objectWhere)
  54. ->where("name", $folderName)
  55. ->when($uuid, fn($query) => $query->where("uuid", $uuid))
  56. ->where("parent_id", $parentId)
  57. ->first();
  58. if (! $folder) {
  59. $folder = Folder::query()->create([
  60. 'company_id' => Auth::user()->company_id,
  61. ...$this->objectWhere,
  62. 'uuid' => $uuid,
  63. 'parent_id' => $parentId,
  64. 'name' => $folderName
  65. ]);
  66. $folder->path = $parentFolder ? $parentFolder?->path . $folder->id . "," : sprintf(",%s,", $folder->id);
  67. $folder->save();
  68. }
  69. $parentFolder = $folder;
  70. $folderRelations[$index] = $folder->id;
  71. }
  72. }
  73. return $folderRelations;
  74. }
  75. protected function checkFormData()
  76. {
  77. throw_validation_if(
  78. count($this->request->file("files")) != count($this->request->folders),
  79. "File and directory do not match"
  80. );
  81. $this->filesSize = $this->checkRequestData($this->request);
  82. if ($this->request->get("request_id")) {
  83. $this->progressBar = new ProgressBar($this->request->get("request_id"), $this->filesSize);
  84. }
  85. }
  86. }