123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Services\File\Upload;
- use App\Http\Resources\API\FileUploadSuccessResource;
- use App\Models\BimFile;
- use App\Models\ContainerContent;
- use App\Models\Enums\FileObjectType;
- use App\Models\File;
- use App\Services\File\BIM\BIMFactory;
- use Illuminate\Http\Request;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Auth;
- trait FilesUploadTrait
- {
- protected ?FileObjectType $fileObjectType = null;
- protected $object = null;
- protected function checkRequestData(Request $request): int
- {
- $this->fileObjectType = FileObjectType::from($request->object_type);
- if ($request->object_id) {
- $this->object = $this->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
- {
- $extension = $file->extension() ? $file->extension() : pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
- $pathname = $file->storeAs(
- sprintf("c%s/%s/%s", Auth::user()->company_id, $request->get("object_type"), date("Ymd")),
- sprintf("%s.%s", md5(uniqid()), $extension)
- );
- $data = [
- 'bim' => [],
- ];
- $isBimFile = 0;
- if (in_array($extension, config("bim.extensions")) && $request->object_type == FileObjectType::CONTAINER->value) {
- $isBimFile = true;
- $data['bim'] = BIMFactory::make()->uploadFile($file, [
- 'pathname' => $pathname,
- 'extension' => $extension,
- 'is_cad' => in_array($extension, ['dwg', 'dwf', 'dws', 'dwt']),
- ]);
- }
- throw_validation_if(! $pathname, "File upload failed.");
- $data['file'] = [
- 'pathname' => $pathname,
- 'title' => $file->getClientOriginalName(),
- 'size' => $file->getSize(),
- 'extension' => $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),
- 'uuid' => $request->get("uuid"),
- 'is_bim' => $isBimFile,
- ];
- return $data;
- }
- protected function storeFiles(array $items)
- {
- $uploadedFiles = [];
- foreach ($items as $item) {
- $fileInfo = $item['file'];
- if ($fileInfo['object_id'] && $fileInfo['source'] == 1||$fileInfo['uuid'] && $fileInfo['source']== 1) {
- $version = File::query()
- ->where('object_type', $fileInfo['object_type'])
- ->where('object_id', $fileInfo['object_id'])
- ->where("title", $fileInfo['title'])
- ->where("source", 1)
- ->where("folder_id", $fileInfo['folder_id'] ?? 0)
- ->count();
- $fileInfo['version'] = $version + 1;
- $fileInfo['is_latest_version'] = 1;
- File::query()
- ->where('object_type', $fileInfo['object_type'])
- ->where('object_id', $fileInfo['object_id'])
- ->where("title", $fileInfo['title'])
- ->where("folder_id", $fileInfo['folder_id'] ?? 0)
- ->where("source", 1)
- ->where("is_latest_version", 1)
- ->update([
- 'is_latest_version' => 0
- ]);
- }
- $file = File::query()->create($fileInfo);
- if (isset($item['bim']) && $item['bim']) {
- BimFile::query()->create([
- 'file_id' => $file->id,
- ...$item['bim']
- ]);
- }
- $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;
- $company->increment("used_storage_capacity", $filesSize);
- $company->save();
- }
- }
|