84 lines
1.9 KiB
PHP
84 lines
1.9 KiB
PHP
<?php
|
|
namespace App\Domains\File\Http\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\File\Services\FileService;
|
|
use App\Exceptions\InvalidArgumentException;
|
|
|
|
class FileController extends Controller
|
|
{
|
|
protected $request;
|
|
protected $fileService;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request, FileService $fileService)
|
|
{
|
|
$this->request = $request;
|
|
$this->fileService = $fileService;
|
|
}
|
|
|
|
/**
|
|
* 文件列表
|
|
*
|
|
* @return void
|
|
*/
|
|
public function index()
|
|
{
|
|
$conditions = [];
|
|
$conditions['limit'] = $this->request->get('limit', 20);
|
|
|
|
$files = $this->fileService->index($conditions);
|
|
|
|
return res($files, '文件列表', 201);
|
|
}
|
|
|
|
/**
|
|
* 删除.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy()
|
|
{
|
|
$ids = $this->request->ids();
|
|
$this->fileService->destroy($ids);
|
|
return res(true, '删除成功');
|
|
}
|
|
|
|
/**
|
|
* 上传文件.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function upload()
|
|
{
|
|
$rule = [];
|
|
|
|
foreach (FileService::$mimeTypes as $type => $mimeType) {
|
|
$rule[$type] = 'mimetypes:'.implode(',', $mimeType);
|
|
}
|
|
|
|
Validator::validate($this->request->all(), $rule);
|
|
|
|
foreach (array_keys(FileService::$mimeTypes) as $type) {
|
|
if ($this->request->has($type)) {
|
|
$file = $this->request->file($type);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$file) {
|
|
throw new InvalidArgumentException('请上传正确的文件');
|
|
}
|
|
|
|
$input = $this->request->only(['height', 'width', 'duration', 'type', 'typeid', 'cover']);
|
|
|
|
$fileModel = $this->fileService->upload($file, $input);
|
|
|
|
return res($fileModel, '上传成功');
|
|
}
|
|
}
|