php版蜻蜓精简版系统-修复本地上传功能,本地上传视频存储功能,之前一直使用阿里云和腾讯云,本地出错
修改以下几部分,并且必须安装ffmpg插件,修改部分是
[size=1em] app/Services/UploadFilesService.php
| @@ -173,13 +173,13 @@ class UploadFilesService { | 173 | 173 | if ($type == 'video') { | 174 | 174 | // 本地环境生成预览图 | 175 | 175 | $img = 'uploads/' . genRequestSn() . '.jpg'; | 176 | | $path = [ | 177 | | 'ffmpeg.binaries' => '/usr/local/bin/avconv', | 178 | | 'ffmpeg.binaries' => '/usr/local/bin/ffmpeg', | 179 | | 'ffprobe.binaries' => '/usr/local/bin/avprobe', | 180 | | 'ffprobe.binaries' => '/usr/local/bin/ffprobe', | 181 | | ]; | 182 | | $ffmpeg = FFMpeg::create($path); | | 176 | $logger = null; | | 177 | $ffmpeg = FFMpeg::create(array( | | 178 | 'ffmpeg.binaries' => '/usr/ffmpeg/bin/ffmpeg', | | 179 | 'ffprobe.binaries' => '/usr/ffmpeg/bin/ffprobe', | | 180 | 'timeout' => 3600, // The timeout for the underlying process | | 181 | 'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use | | 182 | ), $logger); | 183 | 183 | $video = $ffmpeg->open($filePath); | 184 | 184 | $frame = $video->frame(TimeCode::fromSeconds(1)); | 185 | 185 | $frame->save($img); |
|
[size=1em] app/Services/UploadFilesService.php
| @@ -12,6 +12,7 @@ use Exception; | 12 | 12 | use App\Models\Config; | 13 | 13 | use Illuminate\Http\Request; | 14 | 14 | use Illuminate\Support\Facades\File; | | 15 | use Illuminate\Support\Facades\Storage; | 15 | 16 | use Qiniu\Auth; | 16 | 17 | use Qiniu\Storage\UploadManager; | 17 | 18 | use OSS\OssClient; |
| @@ -165,8 +166,8 @@ class UploadFilesService { | 165 | 166 | $path = 'uploads/' . date('Ymd') . '/'; | 166 | 167 | File::makeDirectory($path,$mode = 0777,true,true); | 167 | 168 | // 使用uploads本地存款控件(目录) | 168 | | $img = app('image')->make($filePath)->resize(800, 800)->save($path . $filename); | 169 | | $url = $img->basePath(); | | 169 | $bool = Storage::disk($path)->put($filename, file_get_contents($filePath)); | | 170 | $url = $path . $filename; | 170 | 171 | if ($type == 'video') { | 171 | 172 | return [ | 172 | 173 | 'img' => '', |
|
[size=1em] app/Services/UploadFilesService.php
| @@ -163,11 +163,10 @@ class UploadFilesService { | 163 | 163 | | 164 | | public function local_upload($filename, $filePath, $type = 'img') | 165 | 165 | { | 166 | | $path = 'uploads/' . date('Ymd') . '/'; | 167 | | File::makeDirectory($path,$mode = 0777,true,true); | | 166 | $filename = date('Ymd') . '/' . $filename; | 168 | 167 | // 使用uploads本地存款控件(目录) | 169 | | $bool = Storage::disk($path)->put($filename, file_get_contents($filePath)); | 170 | | $url = $path . $filename; | | 168 | $bool = Storage::disk('uploads')->put($filename, file_get_contents($filePath)); | | 169 | $url = 'uploads/' . $filename; | 171 | 170 | if ($type == 'video') { | 172 | 171 | return [ | 173 | 172 | 'img' => '', |
|
[size=1em] app/Services/UploadFilesService.php
| @@ -165,20 +165,24 @@ class UploadFilesService { | 165 | 165 | { | 166 | 166 | $filename = date('Ymd') . '/' . $filename; | 167 | 167 | // 使用uploads本地存款控件(目录) | 168 | | $bool = Storage::disk('uploads')->put($filename, file_get_contents($filePath)); | 169 | | $url = 'uploads/' . $filename; | 170 | | if ($type == 'video') { | 171 | | return [ | 172 | | 'img' => '', | 173 | | 'img_url' => '', | 174 | | 'video_url' => $url, | 175 | | 'url' => dealUrl($url), | 176 | | ]; | 177 | | } else { | 178 | | return [ | 179 | | 'img_url' => $url, | 180 | | 'url' => dealUrl($url) | 181 | | ]; | | 168 | $bool = Storage::disk('local')->put($filename, file_get_contents($filePath)); | | 169 | if($bool){ | | 170 | $url = 'uploads/' . $filename; | | 171 | if ($type == 'video') { | | 172 | return [ | | 173 | 'img' => '', | | 174 | 'img_url' => '', | | 175 | 'video_url' => $url, | | 176 | 'url' => dealUrl($url), | | 177 | ]; | | 178 | } else { | | 179 | return [ | | | 'img_url' => $url, | | 181 | 'url' => dealUrl($url) | | 182 | ]; | | 183 | } | | 184 | }else{ | | 185 | throw new Exception('上传失败'); | 182 | 186 | } | 183 | 187 | } | 184 | 188 | }直接替换本文件 ,使用最终文件。<?php
/**
* 文件上传服务
* @date 2020-01-01
* @author kiro
* @version 1.0
*/
namespace App\Services;
use Exception;
use App\Models\Config;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFMpeg;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use OSS\OssClient;
use OSS\Core\OssException;
class UploadFilesService {
private $config = [];
public function __construct()
{
$this->config = Config::getConfig('upload');
if (empty($this->config)){
throw new Exception('上传配置错误');
}
}
public function up(Request $request)
{
if (!$request->hasFile('file')) {
throw new Exception('无法获取上传文件');
}
$file = $request->file('file');
if (!$file->isValid()) {
throw new Exception('文件未通过验证');
}
// 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名
$fileExtension = strtolower($file->getClientOriginalExtension());
if(! in_array($fileExtension, explode('|', $this->config['upload_file_ext']))) {
throw new Exception('不支持' .$fileExtension. '文件格式');
}
// 3.判断大小是否符合 2M
$filePath = $file->getRealPath();
if (filesize($filePath) >= $this->config['upload_max_size'] * 1024 * 1000) {
throw new Exception('文件大小超过限制');
}
$filename = genRequestSn() . '.' . $fileExtension;
// 文件原名
$originaName = $file->getClientOriginalName();
if ($this->config['upload_service'] == 'qiniu') {
$data = $this->qiniu_upload($filename, $filePath);
} elseif ($this->config['upload_service'] == 'aliyun') {
$data = $this->aliyun_upload($filename, $filePath);
} else {
$data = $this->local_upload($filename, $filePath);
}
$data['name'] = $originaName;
return $data;
}
public function upVideo(Request $request)
{
if (!$request->hasFile('file')) {
throw new Exception('无法获取上传文件');
}
$file = $request->file('file');
if (!$file->isValid()) {
throw new Exception('文件未通过验证');
}
// 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名
$fileExtension = strtolower($file->getClientOriginalExtension());
if(! in_array($fileExtension, explode('|', $this->config['upload_video_file_ext']))) {
throw new Exception('不支持' .$fileExtension. '文件格式');
}
// 3.判断大小是否符合 2M
$filePath = $file->getRealPath();
if (filesize($filePath) >= $this->config['upload_video_max_size'] * 1024 * 1000) {
throw new Exception('文件大小超过限制');
}
$filename = genRequestSn() . '.' . $fileExtension;
// 文件原名
$originaName = $file->getClientOriginalName();
if ($this->config['upload_service'] == 'qiniu') {
$data = $this->qiniu_upload($filename, $filePath, 'video');
} elseif ($this->config['upload_service'] == 'aliyun') {
$data = $this->aliyun_upload($filename, $filePath, 'video');
} else {
$data = $this->local_upload($filename, $filePath, 'video');
}
$data['name'] = $originaName;
return $data;
}
public function qiniu_upload($filename, $filePath, $type = 'img')
{
$accessKey = $this->config['upload_qiniu_accessKey'];
$secretKey = $this->config['upload_qiniu_secretKey'];
$bucket = $this->config['upload_qiniu_bucket'];
if (empty($accessKey) || empty($secretKey) || empty($bucket)) {
throw new Exception('七牛云配置有误');
}
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
$filename = 'qiniu_' . $filename;
$result = $uploadMgr->putFile($token, $filename, $filePath);
unlink($filePath);
$url = isset($result[0]['key']) ? $result[0]['key']:'';
if ($type == 'video') {
$img = $url . $this->config['upload_qiniu_video_thumb'];
return [
'img' => $img,
'img_url' => dealUrl($img),
'video_url' => $url,
'url' => dealUrl($url),
];
} else {
return [
'img_url' => $url . '?imageView2/1/w/800/h/800',
'url' => dealUrl($url)
];
}
}
public function aliyun_upload($filename, $filePath, $type = 'img')
{
$accessKey = $this->config['upload_aliyun_accessKey'];
$secretKey = $this->config['upload_aliyun_secretKey'];
$bucket = $this->config['upload_aliyun_bucket'];
$endpoint = $this->config['upload_aliyun_endPoint'];
if (empty($accessKey) || empty($secretKey) || empty($bucket)) {
throw new Exception('阿里云配置有误');
}
try{
$filename = 'aliyun_' . $filename;
$ossClient = new OssClient($accessKey, $secretKey, $endpoint);
$ossClient->uploadFile($bucket, $filename, $filePath);
} catch(OssException $e) {
throw new Exception($e->getMessage());
}
unlink($filePath);
$url = $filename;
if ($type == 'video') {
$img = $url . $this->config['upload_aliyun_video_thumb'];
return [
'img' => $img,
'img_url' => dealUrl($img),
'video_url' => $url,
'url' => dealUrl($url),
];
} else {
return [
'img_url' => $url,
'url' => dealUrl($url)
];
}
}
public function local_upload($filename, $filePath, $type = 'img')
{
$filename = date('Ymd') . '/' . $filename;
// 使用uploads本地存款控件(目录)
$bool = Storage::disk('local')->put($filename, file_get_contents($filePath));
if($bool){
$url = 'uploads/' . $filename;
if ($type == 'video') {
// 本地环境生成预览图
$img = 'uploads/' . genRequestSn() . '.jpg';
$logger = null;
$ffmpeg = FFMpeg::create(array(
'ffmpeg.binaries' => '/usr/ffmpeg/bin/ffmpeg',
'ffprobe.binaries' => '/usr/ffmpeg/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
), $logger);
$video = $ffmpeg->open($filePath);
$frame = $video->frame(TimeCode::fromSeconds(1));
$frame->save($img);
return [
'img' => $img,
'img_url' => dealUrl($img),
'video_url' => $url,
'url' => dealUrl($url),
];
} else {
return [
'img_url' => $url,
'url' => dealUrl($url)
];
}
}else{
throw new Exception('上传失败');
}
}
}
|
|