/** * 储存上传文件. * * @param hiyiPlusHttpRequestsAPI2StoreUploadFile $request * @param IlluminateContractsRoutingResponseFactory $response * @param CarbonCarbon $dateTime * @param hiyiPlusModelsFile $fileModel * @param hiyiPlusModelsFileWith $fileWith * @return mixed * @author Seven Du <shiweidu@outlook.com> */ public function store(StoreUploadFileRequest $request, ResponseContract $response, Carbon $dateTime, FileModel $fileModel, FileWithModel $fileWith) { $clientHeight = $request->input('height', 0); $clientWidth = $request->input('width', 0); //判断文件hash(是否上传过) $fileModel = $this->validateFileInDatabase($fileModel, $file = $request->file('file'), function (UploadedFile $file, string $md5) use ($fileModel, $dateTime, $clientWidth, $clientHeight, $response): FileModel { // 图片做旋转处理 if (! in_array($file->getClientMimeType(), ['video/mp4', 'image/gif'])) { ini_set('memory_limit', '-1'); Image::make($file->getRealPath())->orientate()->save($file->getRealPath(), 100); } list($width, $height) = ($imageInfo = @getimagesize($file->getRealPath())) === false ? [null, null] : $imageInfo; //按照日期生成路径 $path = $dateTime->format('Y/m/d/Hi'); //去除本地上传 // if (($filename = $file->store($path, config('cdn.generators.filesystem.disk'))) === false) { // return $response->json(['message' => '上传失败'], 500); // } //oss上传开始 $files=new FilesImagesController(); //生成文件路径 $oss_path = $path.'/'.$file->hashName(); $result=$files->uploadsOss($file->getRealPath(), $oss_path); //oss上传结束 //保存file表 $fileModel->filename = $oss_path; $fileModel->hash = $md5; $fileModel->origin_filename = $file->getClientOriginalName(); $fileModel->mime = $file->getClientMimeType(); $fileModel->width = $width ?? $clientWidth; $fileModel->height = $height ?? $clientHeight; $fileModel->saveOrFail(); return $fileModel; }); //保存files_with表 $fileWith = $this->resolveFileWith($fileWith, $request->user(), $fileModel); //log::info('store_id'.$fileWith->id); return $response->json(['message' => ['上传成功'], 'id' => $fileWith->id,])->setStatusCode(201); } /** * Get or create a uploaded file with id. * * @param IlluminateHttpRequest $request * @param IlluminateContractsRoutingResponseFactory $response * @param hiyiPlusModelsFile $file * @param hiyiPlusModelsFileWith $fileWith * @param string $hash * @return mixed * @author Seven Du <shiweidu@outlook.com> */ public function uploaded(Request $request, ResponseContract $response, FileModel $file, FileWithModel $fileWith, string $hash) { $file = $file->where('hash', strtolower($hash))->firstOr(function () { abort(406); }); // 复用空类型数据~减少资源浪费. $fileWith = $this->resolveFileWith($fileWith, $request->user(), $file); return $response->json([ 'message' => ['success'], 'id' => $fileWith->id, ])->setStatusCode(200); } /** * Validate and return the file database model instance. * * @param hiyiPlusModelsFile $fileModel * @param IlluminateHttpUploadedFile $file * @param callable $call * @return hiyiPlusModelsFile * @author Seven Du <shiweidu@outlook.com> */ public function validateFileInDatabase(FileModel $fileModel, UploadedFile $file, callable $call): FileModel { $hash = md5_file($file->getRealPath()); return $fileModel->where('hash', $hash)->firstOr(function () use ($file, $call, $hash): FileModel { return call_user_func_array($call, [$file, $hash]); }); } /** * 解决数据模型非实例. * * @param hiyiPlusModelsFileWith $fileWith * @param hiyiPlusModelsUser $user * @param hiyiPlusModelsFile $file * @return hiyiPlusModelsFileWith * @author Seven Du <shiweidu@outlook.com> */ public function resolveFileWith(FileWithModel $fileWith, UserModel $user, FileModel $file): FileWithModel { $fileWith->file_id = $file->id; $fileWith->channel = null; $fileWith->raw = null; $fileWith->size = ($size = sprintf('%sx%s', $file->width, $file->height)) === 'x' ? null : $size; $user->files()->save($fileWith); return $fileWith; }
https://www.cnblogs.com/loveyoume/p/6099966.html