mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
完成静态文件上传功能并去除外部依赖;
This commit is contained in:
@@ -3,14 +3,12 @@
|
||||
|
||||
namespace app\common\command;
|
||||
|
||||
|
||||
use EasyAdmin\console\CliEcho;
|
||||
use EasyAdmin\tool\CommonTool;
|
||||
use EasyAdmin\upload\driver\alioss\Oss;
|
||||
use app\common\service\UploadService;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Filesystem;
|
||||
use think\File;
|
||||
|
||||
class OssStatic extends Command
|
||||
{
|
||||
@@ -24,26 +22,31 @@ class OssStatic extends Command
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln("========正在上传静态资源到OSS上:========" . date('Y-m-d H:i:s'));
|
||||
$dir = root_path() . 'public' . DIRECTORY_SEPARATOR . 'static';
|
||||
$list = CommonTool::readDirAllFiles($dir);
|
||||
$uploadConfig = sysconfig('upload');
|
||||
|
||||
$list = Filesystem::disk('local_static')->listContents('/', true);
|
||||
$upload_service = new UploadService();
|
||||
$uploadPrefix = config('app.oss_static_prefix', 'oss_static_prefix');
|
||||
foreach ($list as $key => $val) {
|
||||
list($objectName, $filePath) = [$uploadPrefix . DIRECTORY_SEPARATOR . $key, $val];
|
||||
try {
|
||||
$upload = Oss::instance($uploadConfig)
|
||||
->save($objectName, $filePath);
|
||||
} catch (\Exception $e) {
|
||||
CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $e->getMessage());
|
||||
|
||||
foreach ($list as $file_item) {
|
||||
|
||||
if ($file_item['type'] != 'file') {
|
||||
continue;
|
||||
}
|
||||
if ($upload['save'] == true) {
|
||||
CliEcho::success('文件上传成功:' . $filePath . '。上传地址:' . $upload['url']);
|
||||
} else {
|
||||
CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $upload['msg']);
|
||||
|
||||
$file_path = $file_item['path'];
|
||||
|
||||
$file_path = Filesystem::disk('local_static')->path($file_path);
|
||||
|
||||
$file = new File($file_path, false);
|
||||
|
||||
$save_name = $file_item['path'];
|
||||
try {
|
||||
$model_file = $upload_service->save($file, $save_name, true, $uploadPrefix, true);
|
||||
$output->info('文件上传成功:' . $save_name . '。上传地址:' . $model_file['url']);
|
||||
} catch (\Throwable $th) {
|
||||
$output->error('文件上传失败:' . $save_name . '。错误信息:' . $th->getMessage());
|
||||
}
|
||||
}
|
||||
$output->writeln("========已完成静态资源上传到OSS上:========" . date('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class UploadService
|
||||
|
||||
protected $uploadType = 'local_public';
|
||||
|
||||
public function __construct($upload_type = 'local_public')
|
||||
public function __construct($upload_type = null)
|
||||
{
|
||||
$uploadConfig = sysconfig('upload');
|
||||
|
||||
@@ -56,10 +56,32 @@ class UploadService
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function save(File $file, string $save_name = null)
|
||||
/**
|
||||
* 存储文件
|
||||
*
|
||||
* @param File $file
|
||||
* @param string|null $save_name
|
||||
* @param boolean $force_save 指定$save_name才可以用,设为true强制写入(不报错,如果存在则删除),否则是驱动的默认行为(覆盖、失败、异常)
|
||||
* @param string $upload_dir
|
||||
* @param bool $disable_model
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(File $file, string $save_name = null, $force_save = false, $upload_dir = 'upload', $disable_model = false)
|
||||
{
|
||||
|
||||
$model_file = new SystemUploadfile();
|
||||
$model_file = null;
|
||||
|
||||
if ($force_save && !is_null($save_name)) {
|
||||
$file_path = $upload_dir . '/' . $save_name;
|
||||
if (Filesystem::disk($this->uploadType)->has($file_path)) {
|
||||
$model_file = SystemUploadfile::where('save_name', $save_name)->where('upload_type', $this->uploadType)->find();
|
||||
Filesystem::disk($this->uploadType)->delete($file_path);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($model_file)) {
|
||||
$model_file = new SystemUploadfile();
|
||||
}
|
||||
|
||||
$model_file->upload_type = $this->uploadType;
|
||||
$model_file->file_ext = strtolower($file->extension());
|
||||
@@ -74,9 +96,21 @@ class UploadService
|
||||
$model_file->mime_type = $file->getMime();
|
||||
}
|
||||
|
||||
$save_name = Filesystem::disk($this->uploadType)->putFile('upload', $file, function () use ($save_name) {
|
||||
|
||||
$save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name,$file) {
|
||||
if (!is_null($save_name)) {
|
||||
return $save_name;
|
||||
|
||||
$ext_name = $file->extension();
|
||||
|
||||
if(empty($ext_name)){
|
||||
return $save_name;
|
||||
}
|
||||
|
||||
$list_name = explode('.',$save_name);
|
||||
|
||||
array_pop($list_name);
|
||||
|
||||
return implode('.', $list_name);
|
||||
}
|
||||
return date('Ymd') . '/' . uniqid();
|
||||
});
|
||||
@@ -88,13 +122,11 @@ class UploadService
|
||||
|
||||
$model_file->sha1 = $file->sha1();
|
||||
|
||||
|
||||
$model_file->file_size = $file->getSize();
|
||||
|
||||
$model_file->save();
|
||||
return [
|
||||
'url' => $url,
|
||||
'save_name' => $save_name
|
||||
];
|
||||
|
||||
if (!$disable_model) {
|
||||
$model_file->save();
|
||||
}
|
||||
return $model_file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,16 @@ return [
|
||||
// 可见性
|
||||
'visibility' => 'public',
|
||||
],
|
||||
'local_static' => [
|
||||
// 磁盘类型
|
||||
'type' => 'local',
|
||||
// 磁盘路径
|
||||
'root' => app()->getRootPath() . 'public/static',
|
||||
// 磁盘路径对应的外部URL路径
|
||||
'url' => Request::domain() . '/static',
|
||||
// 可见性
|
||||
'visibility' => 'public',
|
||||
],
|
||||
'qnoss' => [
|
||||
'type' => 'Qiniu'
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user