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->getClientOriginalExtension() ? $file->getClientOriginalExtension(): 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(); } }