mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-29 05:41:37 +08:00
1.优化默认文件上传
2.上传文件失败抛出异常
This commit is contained in:
parent
d5353ff5d2
commit
9b37570a78
@ -7,6 +7,7 @@
|
||||
|
||||
namespace App\Http\Admin\Controllers;
|
||||
|
||||
use App\Http\Admin\Services\Upload as UploadService;
|
||||
use App\Services\MyStorage as StorageService;
|
||||
use App\Services\Vod as VodService;
|
||||
use App\Validators\Validator as AppValidator;
|
||||
@ -119,20 +120,25 @@ class UploadController extends Controller
|
||||
*/
|
||||
public function uploadDefaultImageAction()
|
||||
{
|
||||
$service = new StorageService();
|
||||
$service = new UploadService();
|
||||
|
||||
$items = [];
|
||||
|
||||
$items['category_icon'] = $service->uploadDefaultCategoryIcon();
|
||||
$items['user_avatar'] = $service->uploadDefaultUserAvatar();
|
||||
$items['article_cover'] = $service->uploadDefaultArticleCover();
|
||||
$items['course_cover'] = $service->uploadDefaultCourseCover();
|
||||
$items['package_cover'] = $service->uploadDefaultPackageCover();
|
||||
$items['topic_cover'] = $service->uploadDefaultTopicCover();
|
||||
$items['slide_cover'] = $service->uploadDefaultSlideCover();
|
||||
$items['gift_cover'] = $service->uploadDefaultGiftCover();
|
||||
$items['vip_cover'] = $service->uploadDefaultVipCover();
|
||||
$items['category_icon'] = $service->uploadDefaultCategoryIcon();
|
||||
|
||||
foreach ($items as $item) {
|
||||
if (!$item) return $this->jsonError(['msg' => '上传文件失败']);
|
||||
foreach ($items as $key => $item) {
|
||||
$msg = sprintf('上传文件失败: %s', $key);
|
||||
if (!$item) {
|
||||
return $this->jsonError(['msg' => $msg]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->jsonSuccess(['msg' => '上传文件成功']);
|
||||
|
153
app/Http/Admin/Services/Upload.php
Normal file
153
app/Http/Admin/Services/Upload.php
Normal file
@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Http\Admin\Services;
|
||||
|
||||
|
||||
use App\Services\MyStorage;
|
||||
|
||||
class Upload extends Service
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MyStorage
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->storage = new MyStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认用户头像
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultUserAvatar()
|
||||
{
|
||||
$filename = static_path('admin/img/default/user_avatar.png');
|
||||
|
||||
$key = '/img/default/user_avatar.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认课程封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultCourseCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/course_cover.png');
|
||||
|
||||
$key = '/img/default/course_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认套餐封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultPackageCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/package_cover.png');
|
||||
|
||||
$key = '/img/default/package_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认话题封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultTopicCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/topic_cover.png');
|
||||
|
||||
$key = '/img/default/topic_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认会员封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultVipCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/vip_cover.png');
|
||||
|
||||
$key = '/img/default/vip_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认专栏封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultArticleCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/article_cover.png');
|
||||
|
||||
$key = '/img/default/article_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认礼品封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultGiftCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/gift_cover.png');
|
||||
|
||||
$key = '/img/default/gift_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认轮播图片
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultSlideCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/slide_cover.png');
|
||||
|
||||
$key = '/img/default/slide_cover.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认分类图标
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultCategoryIcon()
|
||||
{
|
||||
$filename = static_path('admin/img/default/category_icon.png');
|
||||
|
||||
$key = '/img/default/category_icon.png';
|
||||
|
||||
return $this->storage->putFile($key, $filename);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -141,6 +141,10 @@
|
||||
<td>礼品封面</td>
|
||||
<td>public/static/admin/img/default/gift_cover.png</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>轮播封面</td>
|
||||
<td>public/static/admin/img/default/slide_cover.png</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
@ -11,6 +11,7 @@ use App\Library\Utils\FileInfo;
|
||||
use App\Models\Upload as UploadModel;
|
||||
use App\Repos\Upload as UploadRepo;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
class MyStorage extends Storage
|
||||
{
|
||||
@ -36,104 +37,6 @@ class MyStorage extends Storage
|
||||
return $this->putString($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认用户头像
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultUserAvatar()
|
||||
{
|
||||
$filename = static_path('admin/img/default/user_avatar.png');
|
||||
|
||||
$key = '/img/default/user_avatar.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认课程封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultCourseCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/course_cover.png');
|
||||
|
||||
$key = '/img/default/course_cover.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认套餐封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultPackageCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/package_cover.png');
|
||||
|
||||
$key = '/img/default/package_cover.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认话题封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultTopicCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/topic_cover.png');
|
||||
|
||||
$key = '/img/default/topic_cover.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认会员封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultVipCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/vip_cover.png');
|
||||
|
||||
$key = '/img/default/vip_cover.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传默认礼品封面
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultGiftCover()
|
||||
{
|
||||
$filename = static_path('admin/img/default/gift_cover.png');
|
||||
|
||||
$key = '/img/default/gift_cover.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传分类默认图标
|
||||
*
|
||||
* @return false|mixed|string
|
||||
*/
|
||||
public function uploadDefaultCategoryIcon()
|
||||
{
|
||||
$filename = static_path('admin/img/default/category_icon.png');
|
||||
|
||||
$key = '/img/default/category_icon.png';
|
||||
|
||||
return $this->putFile($key, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传封面图片
|
||||
*
|
||||
@ -218,6 +121,10 @@ class MyStorage extends Storage
|
||||
|
||||
$path = $this->putFile($keyName, $file->getTempName());
|
||||
|
||||
if (!$path) {
|
||||
throw new RuntimeException('Upload File Failed');
|
||||
}
|
||||
|
||||
$upload = new UploadModel();
|
||||
|
||||
$upload->name = $name;
|
||||
|
BIN
public/static/admin/img/default/article_cover.png
Normal file
BIN
public/static/admin/img/default/article_cover.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
Loading…
x
Reference in New Issue
Block a user