|
@@ -0,0 +1,96 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\File\Upload;
|
|
|
+
|
|
|
+use App\Http\Resources\API\FileUploadSuccessResource;
|
|
|
+use App\Models\Enums\FileObjectType;
|
|
|
+use App\Models\File;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Http\UploadedFile;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+
|
|
|
+trait FilesUploadTrait
|
|
|
+{
|
|
|
+ protected function checkRequestData(Request $request): int
|
|
|
+ {
|
|
|
+ $fileObjectType = FileObjectType::from($request->object_type);
|
|
|
+
|
|
|
+ $fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
|
|
|
+
|
|
|
+ $filesSize = 0;
|
|
|
+ foreach ($request->file("files") as $file) {
|
|
|
+ throw_validation_if(! $file->isValid(), "File upload failed.");
|
|
|
+
|
|
|
+ $filesSize += $file->getSize();
|
|
|
+ }
|
|
|
+
|
|
|
+ throw_validation_if(
|
|
|
+ $filesSize + Auth::user()->company->used_storage_capacity > Auth::user()->company->storage_size,
|
|
|
+ "Storage capacity is insufficient, please contact the administrator."
|
|
|
+ );
|
|
|
+
|
|
|
+ return $filesSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function uploadFile(Request $request, UploadedFile $file): array
|
|
|
+ {
|
|
|
+ $pathname = $file->storeAs(
|
|
|
+ sprintf("c%s/%s/%s", Auth::user()->company_id, $request->get("object_type"), date("Ymd")),
|
|
|
+ sprintf("%s.%s", md5(uniqid()), $file->extension())
|
|
|
+ );
|
|
|
+
|
|
|
+ throw_validation_if(! $pathname, "File upload failed.");
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pathname' => $pathname,
|
|
|
+ 'title' => $file->getClientOriginalName(),
|
|
|
+ 'size' => $file->getSize(),
|
|
|
+ 'extension' => $file->extension(),
|
|
|
+ 'object_type' => $request->object_type,
|
|
|
+ 'object_id' => $request->object_id,
|
|
|
+ 'created_by' => Auth::id(),
|
|
|
+ 'company_id' => Auth::user()->company_id,
|
|
|
+ 'source' => $request->get("source", 1),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function storeFiles(array $items)
|
|
|
+ {
|
|
|
+ $uploadedFiles = [];
|
|
|
+ foreach ($items as $item) {
|
|
|
+ if ($item['object_id'] && $item['source'] == 1) {
|
|
|
+ $version = File::query()
|
|
|
+ ->where('object_type', $item['object_type'])
|
|
|
+ ->where('object_id', $item['object_id'])
|
|
|
+ ->where("title", $item['title'])
|
|
|
+ ->where("folder_id", $item['folder_id'] ?? 0)
|
|
|
+ ->count();
|
|
|
+ $item['version'] = $version + 1;
|
|
|
+ $item['is_latest_version'] = 1;
|
|
|
+
|
|
|
+ File::query()
|
|
|
+ ->where('object_type', $item['object_type'])
|
|
|
+ ->where('object_id', $item['object_id'])
|
|
|
+ ->where("title", $item['title'])
|
|
|
+ ->where("folder_id", $item['folder_id'] ?? 0)
|
|
|
+ ->where("is_latest_version", 1)
|
|
|
+ ->update([
|
|
|
+ 'is_latest_version' => 0
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $file = File::query()->create($item);
|
|
|
+
|
|
|
+ $uploadedFiles[] = new FileUploadSuccessResource($file);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $uploadedFiles;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function updateUsedStorageCapacity(int $filesSize)
|
|
|
+ {
|
|
|
+ $company = Auth::user()->company;
|
|
|
+ $company->increment("used_storage_capacity", $filesSize);
|
|
|
+ $company->save();
|
|
|
+ }
|
|
|
+}
|