php版蜻蜓精简版系统-修复本地上传功能,本地上传视频存储功能,之前一直使用阿里云和腾讯云,本地出错

php版蜻蜓精简版系统-修复本地上传功能,本地上传视频存储功能,之前一直使用阿里云和腾讯云,本地出错

 

 

修改以下几部分,并且必须安装ffmpg插件,修改部分是

 

 

 app/Services/UploadFilesService.php

 

 @@ -173,13 +173,13 @@ class UploadFilesService {
173173if ($type == 'video') {
174174// 本地环境生成预览图
175175$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);
183183$video = $ffmpeg->open($filePath);
184184$frame = $video->frame(TimeCode::fromSeconds(1));
185185$frame->save($img);
 

 

 

 

 

 

 

 app/Services/UploadFilesService.php

 

 @@ -12,6 +12,7 @@ use Exception;
1212use App\Models\Config;
1313use Illuminate\Http\Request;
1414use Illuminate\Support\Facades\File;
 15use Illuminate\Support\Facades\Storage;
1516use Qiniu\Auth;
1617use Qiniu\Storage\UploadManager;
1718use OSS\OssClient;
 @@ -165,8 +166,8 @@ class UploadFilesService {
165166$path = 'uploads/' . date('Ymd') . '/';
166167File::makeDirectory($path,$mode = 0777,true,true);
167168// 使用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;
170171if ($type == 'video') {
171172return [
172173'img' => '',
 

 

 

 

 app/Services/UploadFilesService.php

 

 @@ -163,11 +163,10 @@ class UploadFilesService {
163163 
164164public function local_upload($filename, $filePath, $type = 'img')
165165{
166 $path = 'uploads/' . date('Ymd') . '/';
167 File::makeDirectory($path,$mode = 0777,true,true);
 166$filename = date('Ymd') . '/' . $filename;
168167// 使用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;
171170if ($type == 'video') {
172171return [
173172'img' => '',
 

 

 

 

 app/Services/UploadFilesService.php

 @@ -165,20 +165,24 @@ class UploadFilesService {
165165{
166166$filename = date('Ymd') . '/' . $filename;
167167// 使用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));
 169if($bool){
 170$url = 'uploads/' . $filename;
 171if ($type == 'video') {
 172return [
 173'img' => '',
 174'img_url' => '',
 175'video_url' => $url,
 176'url' => dealUrl($url),
 177];
 178} else {
 179return [
 180'img_url' => $url,
 181'url' => dealUrl($url)
 182];
 183}
 184}else{
 185throw new Exception('上传失败');
182186}
183187}
184188} 直接替换本文件 ,使用最终文件。
 

<?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('上传失败');

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优雅草·央千澈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值