|
@@ -5,7 +5,9 @@ namespace App\Http\Controllers\API;
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use App\Http\Requests\API\File\DownloadRequest;
|
|
|
use App\Http\Requests\API\File\UploadRequest;
|
|
|
+use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class FileController extends Controller
|
|
|
{
|
|
@@ -14,14 +16,22 @@ class FileController extends Controller
|
|
|
{
|
|
|
// 获取上传的文件
|
|
|
$file = $request->file('file');
|
|
|
- // 存储文件到腾讯云COS
|
|
|
- $path='uploads/' .date('Ym/d/'). $file->getClientOriginalName();//文件相对路径
|
|
|
+ $fileName = $request->fileName ? $request->fileName : Carbon::now()->timestamp. '_' . Str::random(10) ;
|
|
|
+ $path = $request->path ? $request->path : 'uploads/' .date('Ym/d/');
|
|
|
+ $path .= $fileName.'.'.$file->getClientOriginalExtension();
|
|
|
+
|
|
|
+ //验证文件是否存在
|
|
|
+ if (Storage::exists($path)){
|
|
|
+ return $this->badRequest('File already exists');
|
|
|
+ }
|
|
|
+
|
|
|
Storage::put($path, file_get_contents($file->getRealPath()));
|
|
|
// 返回响应
|
|
|
- return response()->json([
|
|
|
- 'message' => 'File uploaded successfully.',
|
|
|
- 'path' => $path,
|
|
|
- ]);
|
|
|
+ return [
|
|
|
+ 'path' => $path,
|
|
|
+ 'fileName' => $fileName.'.'.$file->getClientOriginalExtension(),
|
|
|
+ 'ext' => $file->getClientOriginalExtension(),
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
public function download(DownloadRequest $request)
|