|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Services\File\Upload;
|
|
|
|
|
|
use App\Http\Resources\API\FileUploadSuccessResource;
|
|
|
+use App\Models\ContainerContent;
|
|
|
use App\Models\Enums\FileObjectType;
|
|
|
use App\Models\File;
|
|
|
use Illuminate\Http\Request;
|
|
@@ -11,12 +12,16 @@ use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
trait FilesUploadTrait
|
|
|
{
|
|
|
+ protected ?FileObjectType $fileObjectType = null;
|
|
|
+
|
|
|
+ protected $object = null;
|
|
|
+
|
|
|
protected function checkRequestData(Request $request): int
|
|
|
{
|
|
|
- $fileObjectType = FileObjectType::from($request->object_type);
|
|
|
+ $this->fileObjectType = FileObjectType::from($request->object_type);
|
|
|
|
|
|
if ($request->object_id) {
|
|
|
- $fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
|
|
|
+ $this->object = $this->fileObjectType->modelBuilderAllowed($request->object_id)->findOrFail($request->object_id);
|
|
|
}
|
|
|
|
|
|
$filesSize = 0;
|
|
@@ -88,9 +93,37 @@ trait FilesUploadTrait
|
|
|
$uploadedFiles[] = new FileUploadSuccessResource($file);
|
|
|
}
|
|
|
|
|
|
+ $this->updateObjectVersion();
|
|
|
+
|
|
|
return $uploadedFiles;
|
|
|
}
|
|
|
|
|
|
+ protected function updateObjectVersion()
|
|
|
+ {
|
|
|
+ if (!$this->object) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->fileObjectType == FileObjectType::CONTAINER) {
|
|
|
+ $version = $this->object->version;
|
|
|
+
|
|
|
+ $this->object->version++;
|
|
|
+ $this->object->save();
|
|
|
+
|
|
|
+ $fileIds = File::query()->where([
|
|
|
+ 'object_type' => $this->fileObjectType->value,
|
|
|
+ 'object_id' => $this->object->id
|
|
|
+ ])->pluck("id");
|
|
|
+
|
|
|
+ ContainerContent::query()->create([
|
|
|
+ ...$this->object->content($version)->first()->toArray(),
|
|
|
+ 'created_by' => Auth::id(),
|
|
|
+ 'files' => $fileIds->join(","),
|
|
|
+ 'version' => $this->object->version,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected function updateUsedStorageCapacity(int $filesSize)
|
|
|
{
|
|
|
$company = Auth::user()->company;
|