generated from panliang/owl-admin-starter
64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class WebController extends Controller
|
|
{
|
|
public function upload(Request $request)
|
|
{
|
|
$path = $request->input('path', 'uploads').'/'.date('Y-m-d');
|
|
$result = [];
|
|
|
|
$disk = $this->getDisk();
|
|
|
|
// file 文件
|
|
$files = $request->except('path');
|
|
foreach ($files as $key => $fileData) {
|
|
$item = null;
|
|
if (is_array($fileData)) {
|
|
foreach ($fileData as $file) {
|
|
$item[] = $disk->url($this->saveFile($disk, $path, $file));
|
|
}
|
|
} else {
|
|
$item = $disk->url($this->saveFile($disk, $path, $fileData));
|
|
}
|
|
$result[$key] = $item;
|
|
}
|
|
|
|
return $this->response()->success($result);
|
|
}
|
|
|
|
protected function saveFile($disk, $path, $file = null)
|
|
{
|
|
$filePath = '';
|
|
if ($file instanceof UploadedFile) {
|
|
//限制文件类型:
|
|
if(in_array($file->getClientOriginalExtension(), [
|
|
'jpeg', 'jpg', 'gif', 'bmp', 'png', //图片
|
|
'mp4', //视频
|
|
'mp3', //音频
|
|
])){
|
|
$filePath = $disk->putFile($path, $file);
|
|
}
|
|
} elseif (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)) {
|
|
$type = $result[2];
|
|
if (in_array($type, ['jpeg', 'jpg', 'gif', 'bmp', 'png'])) {
|
|
$filePath = $path.'/'.uniqid().'.'.$type;
|
|
$disk->put($filePath, base64_decode(str_replace($result[1], '', $file)));
|
|
}
|
|
}
|
|
|
|
return $filePath;
|
|
}
|
|
|
|
protected function getDisk($disk = null): Filesystem
|
|
{
|
|
return Storage::disk($disk);
|
|
}
|
|
}
|