180 lines
4.1 KiB
PHP
180 lines
4.1 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
|
// +----------------------------------------------------------------------
|
|
namespace App\Http\Controllers\Admin\System;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Http\Controllers\BaseController;
|
|
|
|
class File extends BaseController {
|
|
|
|
/**
|
|
* @title 上传文件
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function upload(Request $request){
|
|
$type = $request->input('type', 'images');
|
|
|
|
try {
|
|
switch ($type) {
|
|
case 'avatar':
|
|
$this->data['data'] = $this->avatar($request);
|
|
break;
|
|
case 'images':
|
|
$this->data['data'] = $this->images($request);
|
|
break;
|
|
case 'files':
|
|
$this->data['data'] = $this->files($request);
|
|
break;
|
|
default:
|
|
$this->data['data'] = $this->images($request);
|
|
break;
|
|
}
|
|
} catch (\Throwable $th) {
|
|
$this->data['code'] = 0;
|
|
$this->data['message'] = $th->getMessage();
|
|
}
|
|
return response()->json($this->data);
|
|
}
|
|
|
|
/**
|
|
* @title 文件列表
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function lists(Request $request){
|
|
$path = $request->input('groupId', 'images');
|
|
|
|
$files = Storage::allFiles($path);
|
|
|
|
$data = [];
|
|
foreach ($files as $key => $value) {
|
|
$data[] = [
|
|
'path' => $value,
|
|
'url' => Storage::disk()->url($path),
|
|
'name'=> explode('/', $value)[2],
|
|
];
|
|
}
|
|
$this->data['data'] = ['data' => $data];
|
|
return response()->json($this->data);
|
|
}
|
|
|
|
|
|
/**
|
|
* @title 列表菜单
|
|
*
|
|
* @return void
|
|
*/
|
|
public function menu(Request $request){
|
|
$file = [
|
|
['id'=> 'images','title'=>'图片', 'children' => []],
|
|
['id'=> 'files','title'=>'文件', 'children' => []],
|
|
['id'=> 'videos','title'=>'视频', 'children' => []],
|
|
['id'=> 'audios','title'=>'音频', 'children' => []],
|
|
];
|
|
|
|
foreach ($file as $key => $value) {
|
|
$paths = Storage::disk()->allDirectories($value['id']);
|
|
foreach ($paths as $key => $value) {
|
|
$file[$key]['children'][] = ['id'=> $value, 'title'=>explode('/', $value)[1]];
|
|
}
|
|
}
|
|
|
|
$this->data['data'] = $file;
|
|
return response()->json($this->data);
|
|
}
|
|
|
|
/**
|
|
* @title 删除文件
|
|
*
|
|
* @return void
|
|
*/
|
|
public function delete(Request $request){
|
|
return response()->json($this->data);
|
|
}
|
|
|
|
/**
|
|
* @title 上传头像
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
protected function avatar(Request $request){
|
|
$file = $request->file('file');
|
|
|
|
if ($file->isValid()) {
|
|
$ext = $file->extension();
|
|
$name = $file->hashName();
|
|
|
|
$path = $file->store('avatar/'.date('Ymd'));
|
|
|
|
$data = [
|
|
'path' => $path,
|
|
'url' => Storage::disk()->url($path),
|
|
'src' => Storage::disk()->url($path),
|
|
'name'=> $name,
|
|
'size'=> $file->getSize(),
|
|
'ext' => $ext,
|
|
];
|
|
return $data;
|
|
}else{
|
|
throw new \Exception('上传失败');
|
|
}
|
|
}
|
|
|
|
protected function images(Request $request){
|
|
$file = $request->file('file');
|
|
|
|
if ($file->isValid()) {
|
|
$ext = $file->extension();
|
|
$name = $file->hashName();
|
|
|
|
$path = $file->store('images/'.date('Ymd'));
|
|
|
|
$data = [
|
|
'path' => $path,
|
|
'url' => Storage::disk()->url($path),
|
|
'src' => Storage::disk()->url($path),
|
|
'name'=> $name,
|
|
'size'=> $file->getSize(),
|
|
'ext' => $ext,
|
|
];
|
|
return $data;
|
|
}else{
|
|
throw new \Exception('上传失败');
|
|
}
|
|
}
|
|
|
|
protected function files(Request $request){
|
|
$file = $request->file('file');
|
|
|
|
if ($file->isValid()) {
|
|
$ext = $file->extension();
|
|
$name = $file->hashName();
|
|
|
|
$path = $file->store('files/'.date('Ymd'));
|
|
|
|
$data = [
|
|
'path' => $path,
|
|
'url' => Storage::disk()->url($path),
|
|
'src' => Storage::disk()->url($path),
|
|
'name'=> $name,
|
|
'size'=> $file->getSize(),
|
|
'ext' => $ext,
|
|
];
|
|
return $data;
|
|
}else{
|
|
throw new \Exception('上传失败');
|
|
}
|
|
}
|
|
}
|