Skip to content

Commit 1b0539d

Browse files
committed
1.新增 文件上传服务
1 parent b4a6b6e commit 1b0539d

File tree

3 files changed

+171
-154
lines changed

3 files changed

+171
-154
lines changed

app/Extend/Upload.php

-152
This file was deleted.

app/Http/Controllers/Admin/IndexController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
use App\Jobs\Snotify;
1212
use App\Extend\Predis;
13-
use App\Extend\Upload;
1413
use App\Rules\AdminRule;
1514
use App\Model\SmsNotify;
1615
use App\Extend\Phpoffice;
1716
use App\Service\RoleService;
1817
use App\Extend\ServerMonitor;
1918
use App\Service\AdminService;
19+
use App\Service\UploadService;
2020

2121
class IndexController extends BaseController
2222
{
@@ -87,7 +87,7 @@ public function uploadFile()
8787
{
8888
$file = $this->requestFile['file'];
8989

90-
$result = Upload::file($file);
90+
$result = UploadService::file($file);
9191
if (!$result['status']) {
9292
jError($result['msg']);
9393
}

app/Service/UploadService.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2022/5/16
6+
* Time: 10:04
7+
*/
8+
9+
namespace App\Service;
10+
11+
use App\Extend\Oss;
12+
use App\Extend\Qniu;
13+
use Illuminate\Support\Facades\Storage;
14+
15+
class UploadService
16+
{
17+
18+
private static $imageSize = 2;
19+
private static $videoSize = 20;
20+
private static $imagePostfix = ["png", "jpg", "jpeg", "gif"];
21+
private static $videoPostfix = ["mp4", "avi", "3gp", "wmv", "mov"];
22+
23+
/**
24+
* form表单上传文件
25+
* @param $file
26+
* @param string $mode
27+
* @return mixed
28+
* @throws \Exception
29+
*/
30+
public static function file($file, $mode = 'storage')
31+
{
32+
# 验证后缀
33+
$postfix = strtolower($file->getClientOriginalExtension());
34+
if (!$postfix || ($postfix && !in_array($postfix, self::$imagePostfix) && !in_array($postfix, self::$videoPostfix))) {
35+
return error('不支持此格式文件上传');
36+
}
37+
38+
# 验证大小
39+
$size = $file->getSize() / (1024 * 1024);
40+
if (in_array($postfix, self::$imagePostfix)) {
41+
$type = 'image';
42+
if ($size > self::$imageSize) {
43+
return error('图片大小不能大于' . self::$imageSize . 'M');
44+
}
45+
}
46+
if (in_array($postfix, self::$videoPostfix)) {
47+
$type = 'video';
48+
if ($size > self::$videoSize) {
49+
return error('视频大小不能大于' . self::$imageSize . 'M');
50+
}
51+
}
52+
53+
# 要存储的文件名
54+
$name = date('His') . rand(1000, 9999) . '.' . $postfix;
55+
56+
switch ($mode)
57+
{
58+
case 'storage':
59+
# 存储至storage文件夹
60+
$date = date('Ymd');
61+
$path = 'upload/' . $type . '/' . $date . '/';
62+
$result = Storage::putFileAs($path, $file, $name);
63+
if ($result) {
64+
$url = '/' . $type . '/' .$date . '/' . $name;
65+
$data = ['url' => $url, 'full_url' => 'http://' . $_SERVER["HTTP_HOST"] . $url];
66+
return success($data);
67+
}
68+
return error();
69+
break;
70+
case 'qiniu':
71+
# 存储到七牛云
72+
$realPath = $file->getRealPath();
73+
$url = $type . '/' . date('Ymd') . '/' . $name;
74+
$result = Qniu::uploadFile($realPath,$url);
75+
if ($result['status']) {
76+
$data = ['url' => $result['data']['url'], 'full_url' => config('style.oss.gateway') . $result['data']['url']];
77+
return success($data);
78+
}
79+
return error($result['msg']);
80+
break;
81+
case 'oss':
82+
# 存储至阿里云OSS
83+
$realPath = $file->getRealPath();
84+
$url = $type . '/' . date('Ymd') . '/' . $name;
85+
$result = Oss::fileUpload($realPath,$url);
86+
if (!$result['status']) {
87+
return error($result['msg']);
88+
}
89+
return success($result['data']);
90+
break;
91+
default:
92+
return error();
93+
}
94+
return error();
95+
}
96+
97+
/**
98+
* base64字符串上传图片
99+
* @param $content
100+
* @param string $mode
101+
* @return mixed
102+
*/
103+
public static function base64Image($content, $mode = 'storage')
104+
{
105+
# 存储到storage文件夹
106+
preg_match('/^(data:\s*image\/(\w+);base64,)/',$content,$match);
107+
if (isset($match[2])) {
108+
# 验证后缀
109+
$postfix = strtolower($match[2]);
110+
if (!in_array($postfix, self::$imagePostfix)) {
111+
$str = implode(' | ',self::$imagePostfix);
112+
return error('只能上传 '. $str .' 格式的图片');
113+
}
114+
115+
# 验证大小
116+
$size = strlen(file_get_contents($content)) / (1024 * 1024);
117+
if ($size > self::$imageSize) {
118+
return error('图片大小不能大于' . self::$imageSize . 'M');
119+
}
120+
121+
switch ($mode)
122+
{
123+
case 'storage':
124+
# 存储至storage文件夹
125+
$name = date('His') . rand(1000, 9999) . '.' . $postfix;
126+
$date = date('Ymd');
127+
$path = 'image/' . $date . '/' . $name;
128+
$image = base64_decode(str_replace($match[1], '', $content));
129+
# disk设置参数upload,需要在config/filesystems.php文件中增加upload驱动
130+
$disk = 'upload';
131+
$result = Storage::disk($disk)->put($path,$image);
132+
if ($result) {
133+
$url = '/image/' . $date . '/' . $name;
134+
$data = ['url' => $url, 'full_url' => 'http://' . $_SERVER["HTTP_HOST"] . $url];
135+
return success($data);
136+
}
137+
return error();
138+
break;
139+
case 'qiniu':
140+
# 存储到七牛云
141+
$name = date('His') . rand(1000, 9999) . '.' . $postfix;
142+
$url = 'image/' . date('Ymd') . '/' . $name;
143+
$image = base64_decode(str_replace($match[1], '', $content));
144+
$result = Qniu::uploadContent($image, $url);
145+
if ($result['status']) {
146+
$data = ['url' => $result['data']['url'], 'full_url' => config('style.oss.storage') . $result['data']['url']];
147+
return success($data);
148+
}
149+
return error($result['msg']);
150+
break;
151+
case 'oss':
152+
# 存储至阿里云OSS
153+
$name = date('His') . rand(1000, 9999) . '.' . $postfix;
154+
$url = 'image/' . date('Ymd') . '/' . $name;
155+
$image = base64_decode(str_replace($match[1], '', $content));
156+
$result = Oss::contentUpload($image, $url);
157+
if (!$result['status']) {
158+
return error($result['msg']);
159+
}
160+
return success($result['data']);
161+
break;
162+
default:
163+
return error();
164+
}
165+
} else {
166+
return error();
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)