uploadPath = Config::get('storage.upload_path').date('/Y/m/d'); } public function upload(Request $request): JsonResponse { $upload = $request->file('upload'); $size = File::size($upload->path()); if ($size > Config::get('storage.max_upload_size')) { return $this->failed('超过最大允许上传大小', 422); } $exists = true; $hash = File::hash($upload->path()); $existFile = StorageModel::where('driver', Config::get('filesystems.default'))->where('hash', $hash)->first(); if ($existFile) { $fullName = $existFile->path; } else { $path = $this->uploadPath; $fileName = $hash.'.'.$upload->getClientOriginalExtension(); $fullName = $path.'/'.$fileName; $uploaded = Storage::putFileAs($path, $upload, $fileName); if (! $uploaded) { return $this->failed('文件上传失败', 422); } $exists = false; StorageModel::create([ 'hash' => $hash, 'driver' => Config::get('filesystems.default'), 'type' => $upload->getClientMimeType(), 'size' => $size, 'path' => $fullName, ]); } return $this->success([ 'exists' => $exists, 'size' => $size, 'path' => $fullName, 'url' => Storage::url($fullName), ]); } }