Merge branch 'develop'
This commit is contained in:
@@ -3,8 +3,12 @@
|
||||
namespace app\common\controller;
|
||||
|
||||
use app\common\service\OssService;
|
||||
use app\common\service\UploadedFilesService;
|
||||
use app\common\service\UploadService;
|
||||
use app\common\traits\ResponseTrait;
|
||||
use support\Log;
|
||||
use support\Request;
|
||||
use support\exception\BusinessException;
|
||||
use Shopwwi\WebmanFilesystem\Facade\Storage;
|
||||
|
||||
class UploadController
|
||||
@@ -22,15 +26,41 @@ class UploadController
|
||||
$scene = $request->input('scene', 'image');
|
||||
$file = $request->file('file');
|
||||
if ($file && $file->isValid()) {
|
||||
$pathUrl = '/uploads/'.$scene.'/'.date('Ymd').'/'.md5($file->getUploadName() . time()).'.'.$file->getUploadExtension();
|
||||
$fileName = md5($file->getUploadName() . time()).'.'.$file->getUploadExtension();
|
||||
$pathUrl = '/uploads/'.$scene.'/'.date('Ymd').'/'.$fileName;
|
||||
$fullPathUrl = public_path() . $pathUrl;
|
||||
$file->move($fullPathUrl);
|
||||
|
||||
$data = [
|
||||
'domain' => getenv('MAIN_DOMAIN'),
|
||||
'file_url' => $pathUrl,
|
||||
'full_url' => getenv('MAIN_DOMAIN') . $pathUrl,
|
||||
'scene' => $scene,
|
||||
];
|
||||
|
||||
// 记录文件信息到数据库
|
||||
try {
|
||||
$uploadedFilesService = new UploadedFilesService();
|
||||
$partition = $scene;
|
||||
$category = $request->input('category', 'general');
|
||||
|
||||
$fileInfo = [
|
||||
'file_name' => basename($fileName),
|
||||
'file_path' => $pathUrl,
|
||||
'file_size' => $file->getSize(),
|
||||
'file_type' => $file->getUploadMimeType(),
|
||||
'original_name' => $file->getUploadName(),
|
||||
];
|
||||
|
||||
$recordResult = $uploadedFilesService->recordFileInfo($fileInfo, $request, $partition, $category);
|
||||
$data['file_id'] = $recordResult['id'];
|
||||
$data['partition'] = $partition;
|
||||
$data['category'] = $category;
|
||||
} catch (\Throwable $e) {
|
||||
// 记录失败不影响上传结果,只记录日志
|
||||
Log::error('记录文件信息失败: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success($data, 'upload success');
|
||||
}
|
||||
return $this->error('file not found');
|
||||
@@ -43,15 +73,22 @@ class UploadController
|
||||
public function uploadFile(Request $request)
|
||||
{
|
||||
try {
|
||||
$scene = $request->input('scene');
|
||||
$file = $request->file('file');
|
||||
|
||||
if (!$file || !$file->isValid()) {
|
||||
return $this->error('上传失败');
|
||||
}
|
||||
if (empty($scene)) {
|
||||
// return $this->error('上传场景不能为空');
|
||||
$sceneUrl = '';
|
||||
}else{
|
||||
$sceneUrl = $scene . '/';
|
||||
}
|
||||
$originalName = $file->getUploadName();
|
||||
|
||||
$result = Storage::adapter('oss')
|
||||
->path('vehicle_album/'.date('Ymd'))
|
||||
->path('x_server/' . $sceneUrl . date('Ymd'))
|
||||
->size(1024*1024*5)
|
||||
->upload($file);
|
||||
|
||||
@@ -63,6 +100,11 @@ class UploadController
|
||||
'file_url' => $ossservice->generatePrivateUrl($result->file_name)
|
||||
];
|
||||
|
||||
// 记录文件信息到数据库
|
||||
$category = $request->input('category', 'general');
|
||||
$res = $this->recordFileInfo($result,$request,$file,$scene,$category);
|
||||
$data = array_merge($data, $res);
|
||||
|
||||
return $this->success($data);
|
||||
}catch (\Throwable $e){
|
||||
return $this->error($e->getMessage());
|
||||
@@ -73,14 +115,21 @@ class UploadController
|
||||
public function uploadImage(Request $request)
|
||||
{
|
||||
try {
|
||||
$scene = $request->input('scene');
|
||||
$file = $request->file('image');
|
||||
|
||||
if (!$file || !$file->isValid()) {
|
||||
return $this->error('上传失败');
|
||||
}
|
||||
if (empty($scene)) {
|
||||
// return $this->error('上传场景不能为空');
|
||||
$sceneUrl = '';
|
||||
}else{
|
||||
$sceneUrl = $scene . '/';
|
||||
}
|
||||
|
||||
$result = Storage::adapter('oss')
|
||||
->path('vehicle_album/'.date('Ymd'))
|
||||
->path('x_server/' . $sceneUrl . date('Ymd'))
|
||||
->size(1024*1024*5)
|
||||
->extYes(['image/jpeg','image/png'])
|
||||
->extNo(['image/gif'])
|
||||
@@ -94,6 +143,11 @@ class UploadController
|
||||
'url' => $ossservice->generatePrivateUrl($result->file_name)
|
||||
];
|
||||
|
||||
// 记录文件信息到数据库
|
||||
$category = $request->input('category', 'general');
|
||||
$res = $this->recordFileInfo($result,$request,$file,$scene,$category);
|
||||
$data = array_merge($data, $res);
|
||||
|
||||
return json(['errno' => 0, 'data' => $data]);
|
||||
// return $this->success($data);
|
||||
} catch (\Throwable $e) {
|
||||
@@ -102,6 +156,123 @@ class UploadController
|
||||
}
|
||||
}
|
||||
|
||||
public function recordFileInfo($result, $request, $file, $scene, $category)
|
||||
{
|
||||
try {
|
||||
$uploadedFilesService = new UploadedFilesService();
|
||||
$partition = $scene;
|
||||
|
||||
$fileInfo = [
|
||||
'file_name' => basename($result->file_name),
|
||||
'file_path' => $result->file_name, // OSS存储路径
|
||||
'file_size' => $file->getSize(),
|
||||
'file_type' => $file->getUploadMimeType(),
|
||||
'original_name' => $file->getUploadName(),
|
||||
];
|
||||
|
||||
$recordResult = $uploadedFilesService->recordFileInfo($fileInfo, $request, $partition, $category);
|
||||
$data = [];
|
||||
$data['file_id'] = $recordResult['id'];
|
||||
$data['partition'] = $partition;
|
||||
$data['category'] = $category;
|
||||
return $data;
|
||||
} catch (\Throwable $e) {
|
||||
// 记录失败不影响上传结果,只记录日志
|
||||
Log::error('记录文件信息失败: ' . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件信息
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function getFileInfo(Request $request)
|
||||
{
|
||||
try {
|
||||
$fileId = $request->input('id');
|
||||
|
||||
if (!$fileId) {
|
||||
return $this->error('文件ID不能为空');
|
||||
}
|
||||
|
||||
$uploadedFilesService = new UploadedFilesService();
|
||||
$fileInfo = $uploadedFilesService->getFileInfo($fileId);
|
||||
|
||||
if (!$fileInfo) {
|
||||
return $this->error('文件不存在');
|
||||
}
|
||||
|
||||
return $this->success($fileInfo);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error('获取文件信息失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function deleteFile(Request $request)
|
||||
{
|
||||
try {
|
||||
$fileId = $request->input('id');
|
||||
$path = $request->input('path');
|
||||
|
||||
$uploadedFilesService = new UploadedFilesService();
|
||||
if(!empty($path) && empty($fileId)){
|
||||
$where = ['file_path' => $path];
|
||||
$file = $uploadedFilesService->getFile($where);
|
||||
$fileId = $file->id;
|
||||
}elseif(empty($path) && !empty($fileId)){
|
||||
$where = ['id' => $fileId];
|
||||
$file = $uploadedFilesService->getFile($where);
|
||||
$path = $file->file_path;
|
||||
}
|
||||
if (!$fileId) {
|
||||
return $this->error('文件ID与路径不能为空');
|
||||
}
|
||||
|
||||
// 删除 OSS 上的文件
|
||||
$ossService = new OssService();
|
||||
$ossService->deleteFile($path);
|
||||
|
||||
$result = $uploadedFilesService->deleteFile($fileId);
|
||||
|
||||
if ($result) {
|
||||
return $this->success([], '文件删除成功');
|
||||
} else {
|
||||
return $this->error('文件删除失败');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error('文件删除失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分区配置
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function getPartitionConfig(Request $request)
|
||||
{
|
||||
try {
|
||||
$uploadedFilesService = new UploadedFilesService();
|
||||
$config = $uploadedFilesService->getPartitionConfig();
|
||||
|
||||
return $this->success($config);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error('获取分区配置失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function proxy(Request $request)
|
||||
{
|
||||
$filePath = $request->get('file'); // 原OSS文件路径
|
||||
|
||||
Reference in New Issue
Block a user