134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Storage\Http\Controllers;
|
|
|
|
use App\Api\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Storage\Models\Storage as StorageModel;
|
|
|
|
class OssController extends Controller
|
|
{
|
|
|
|
private string $uploadPath;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->uploadPath = Config::get('storage.upload_path').date('/Y/m/d');
|
|
}
|
|
|
|
/**
|
|
* Notes : 普通文件上传
|
|
*
|
|
* @Date : 2021/4/25 5:25 下午
|
|
* @Author : <Jason.C>
|
|
* @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('没有图片');
|
|
}
|
|
}
|
|
|
|
}
|