uploadPath = Config::get('storage.upload_path').date('/Y/m/d'); } /** * Notes : 普通文件上传 * * @Date : 2021/4/25 5:25 下午 * @Author : * @param Request $request * @return mixed */ public function upload(Request $request) { $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('storage.driver'))->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('storage.driver'), 'type' => $upload->getClientMimeType(), 'size' => $size, 'path' => $fullName, ]); } return $this->success([ 'exists' => $exists, 'size' => $size, 'path' => $fullName, 'url' => Storage::url($fullName), ]); } /** * Notes : 多图片统一上传 * * @Date : 2021/8/23 13:49 * @Author : Mr.wang * @param Request $request * @return mixed */ public function uploads(Request $request) { $fullFile = []; $files = []; if ($request->file()) { foreach ($request->file() as $key => $upload) { $size = File::size($upload->path()); if ($size > Config::get('storage.max_upload_size')) { $message = '第'.$key + 1 .'张图片超过最大允许上传大小'; return $this->failed($message, 422); } $hash = File::hash($upload->path()); $existFile = StorageModel::where('driver', Config::get('storage.driver')) ->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); } StorageModel::create([ 'hash' => $hash, 'driver' => Config::get('storage.driver'), 'type' => $upload->getClientMimeType(), 'size' => $size, 'path' => $fullName, ]); } $fullFile[] = Storage::url($fullName); $files[] = $fullName; } return $this->success([ 'path' => $files, 'url' => $fullFile, ]); } else { return $this->failed('没有图片'); } } }