This commit is contained in:
尖叫 2023-11-29 15:28:26 +08:00
parent 87f691d2e8
commit 7a545ea88f
4 changed files with 211 additions and 161 deletions

View File

@ -165,23 +165,13 @@ $this->params['breadcrumbs'][] = $this->title;
$json_de = json_decode($model->goodsHub->video_url, true);
$temp_arr = [];
foreach ($json_de as $key => $val) {
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1'){
$val_model = filesize('D:/phpstudy_pro/WWW/cxgj/web' . $val);
}else{
$val_model = filesize($_SERVER["DOCUMENT_ROOT"] . $val);
}
$explode = explode('/', $val);
$end = end($explode);
//
$temp_arr[] = [
'name' => $end,
'url' => $val,
'size_cn' => $val_model,
];
}
$model->goodsHub->video_url = json_encode($temp_arr, JSON_UNESCAPED_UNICODE);
}

View File

@ -103,18 +103,11 @@ $this->params['breadcrumbs'][] = $this->title;
$temp_arr = [];
foreach ($json_de as $key => $val) {
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
$val_model = filesize('D:/phpstudy_pro/WWW/cxgj/web' . $val);
} else {
$val_model = filesize($_SERVER["DOCUMENT_ROOT"] . $val);
}
$explode = explode('/', $val);
$end = end($explode);
//
$temp_arr[] = [
'name' => $end,
'url' => $val,
'size_cn' => $val_model,
];
}
$model->cover_pic = json_encode($temp_arr, JSON_UNESCAPED_UNICODE);

View File

@ -12,12 +12,15 @@
namespace app\modules\file\controllers;
use app\components\Oss;
use Yii;
use app\modules\file\components\Uploader;
use app\modules\file\components\UploadHelper;
use app\models\file\UploadFile;
use app\models\Model;
set_time_limit(0);
use Gumlet\ImageResize;
use Gumlet\ImageResizeException;
@ -301,11 +304,11 @@ class UploadController extends \app\controllers\Controller
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") {
//判断实际发送的请求是否安全
$result = "";
} else {
if(Yii::$app->request->isPost && !empty($_FILES)){
if (Yii::$app->request->isPost && !empty($_FILES)) {
//上传
$currentFileInfo = $this->uploadFile($action);
$result = json_encode($currentFileInfo);
@ -317,7 +320,7 @@ class UploadController extends \app\controllers\Controller
/* 列出图片 */
case 'listimage':
if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") {
$result = "";
} else {
//返回结果
@ -326,7 +329,7 @@ class UploadController extends \app\controllers\Controller
break;
/* 列出文件 */
case 'listfile':
if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") {
$result = "";
} else {
//返回结果
@ -336,7 +339,7 @@ class UploadController extends \app\controllers\Controller
/* 抓取远程文件 */
case 'catchimage':
if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS"){
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "OPTIONS") {
$result = "";
} else {
//返回结果
@ -346,7 +349,7 @@ class UploadController extends \app\controllers\Controller
default:
$result = json_encode(array(
'state'=> '请求地址出错'
'state' => '请求地址出错'
));
break;
}
@ -357,7 +360,7 @@ class UploadController extends \app\controllers\Controller
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode(array(
'state'=> 'callback参数不合法'
'state' => 'callback参数不合法'
));
}
} else {
@ -365,64 +368,124 @@ class UploadController extends \app\controllers\Controller
}
}
//上传图片
public function actionImage($rfmt = 'default')
// //上传图片
// public function actionImage($rfmt = 'default')
// {
// if(!\Yii::$app->request->isPost){
// $data = [
// 'code' => 1,
// 'msg' => "Invalid Request"
// ];
// return $this->responseHandler($data);
// }
// $action = "uploadimage";
// $fileInfo = $this->uploadFile($action);
// if($rfmt == 'default'){
// if($fileInfo["state"] == "SUCCESS"){
// $data = [
// 'code' => 0,
// 'msg' => 'success',
// 'data' => [
// 'url' => $fileInfo['url'],
// 'extension' => $fileInfo["type"],
// 'size' => $fileInfo["size"],
// 'type' => $this->getFileType(trim($fileInfo['type'],"."))
// ]
// ];
// } else {
// $data = [
// 'code' => 1,
// 'msg' => $fileInfo["state"]
// ];
// }
// } else if($rfmt == 'wangeditor'){
// if($fileInfo["state"] == "SUCCESS"){
// $data = [
// 'errno' => 0,
// 'message' => 'success',
// 'data' => [
// 'url' => $fileInfo['url'],
// 'alt' => '',
// 'href' => '',
// 'extension' => $fileInfo["type"],
// 'size' => $fileInfo["size"],
// 'type' => $this->getFileType(trim($fileInfo['type'],"."))
// ]
// ];
// } else {
// $data = [
// 'errno' => 1,
// 'message' => $fileInfo["state"]
// ];
// }
// }
// return $this->responseHandler($data);
// }
//上传图片到OSS
public function actionImage()
{
if(!\Yii::$app->request->isPost){
if (!\Yii::$app->request->isPost) {
$data = [
'code' => 1,
'msg' => "Invalid Request"
];
return $this->responseHandler($data);
}
$action = "uploadimage";
$fileInfo = $this->uploadFile($action);
if($rfmt == 'default'){
if($fileInfo["state"] == "SUCCESS"){
if (empty($_FILES['file']['name'])) {
$data = [
'code' => 1,
'msg' => "请选择要上传的文件"
];
return $this->responseHandler($data);
}
// 上传文件的目录
$upload_dir = "/web/uploads/1/";
if (!is_dir($upload_dir)) {
mkdir(\Yii::$app->basePath . '/web/upload/1', 0777, true);
}
$file = md5($_FILES['file']['name']);
// 上传文件的全名
$upload_file = \Yii::$app->basePath . '/web/upload/1/' . $file . '.png';
// 将上传的文件从临时目录移动到指定目录
if (!move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
$data = [
'code' => 1,
'msg' => "文件上传失败"
];
} else {
$oss = new Oss();
$file = '/web/upload/1/' . $file . '.png';
//上传文件目录
$oss_data = $oss->upload($upload_file, $file);
if ($oss == false) {
$data = [
'code' => 1,
'msg' => "文件上传失败"
];
} else {
$data = [
'code' => 0,
'msg' => 'success',
'data' => [
'url' => $fileInfo['url'],
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'],"."))
'url' => $oss_data,
]
];
} else {
$data = [
'code' => 1,
'msg' => $fileInfo["state"]
];
}
} else if($rfmt == 'wangeditor'){
if($fileInfo["state"] == "SUCCESS"){
$data = [
'errno' => 0,
'message' => 'success',
'data' => [
'url' => $fileInfo['url'],
'alt' => '',
'href' => '',
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'],"."))
]
];
} else {
$data = [
'errno' => 1,
'message' => $fileInfo["state"]
];
}
}
return $this->responseHandler($data);
}
//上传文件
public function actionFile($rfmt = 'default')
{
if(!\Yii::$app->request->isPost){
if (!\Yii::$app->request->isPost) {
$data = [
'code' => 1,
'msg' => "Invalid Request"
@ -431,8 +494,8 @@ class UploadController extends \app\controllers\Controller
}
$action = "uploadfile";
$fileInfo = $this->uploadFile($action);
if($rfmt == 'default'){
if($fileInfo["state"] == "SUCCESS"){
if ($rfmt == 'default') {
if ($fileInfo["state"] == "SUCCESS") {
$data = [
'code' => 0,
'msg' => 'success',
@ -440,7 +503,7 @@ class UploadController extends \app\controllers\Controller
'url' => $fileInfo['url'],
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'],"."))
'type' => $this->getFileType(trim($fileInfo['type'], "."))
]
];
} else {
@ -449,8 +512,8 @@ class UploadController extends \app\controllers\Controller
'msg' => $fileInfo["state"]
];
}
} else if($rfmt == 'wangeditor'){
if($fileInfo["state"] == "SUCCESS"){
} else if ($rfmt == 'wangeditor') {
if ($fileInfo["state"] == "SUCCESS") {
$data = [
'errno' => 0,
'message' => 'success',
@ -460,7 +523,7 @@ class UploadController extends \app\controllers\Controller
'href' => '',
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'],"."))
'type' => $this->getFileType(trim($fileInfo['type'], "."))
]
];
} else {
@ -476,7 +539,7 @@ class UploadController extends \app\controllers\Controller
//上传视频
public function actionVideoFile()
{
if(!\Yii::$app->request->isPost){
if (!\Yii::$app->request->isPost) {
$data = [
'code' => 1,
'msg' => "Invalid Request"
@ -484,7 +547,7 @@ class UploadController extends \app\controllers\Controller
}
$action = "uploadvideo";
$fileInfo = $this->uploadFile($action);
if($fileInfo["state"] == "SUCCESS"){
if ($fileInfo["state"] == "SUCCESS") {
$data = [
'code' => 0,
'msg' => 'success',
@ -492,7 +555,7 @@ class UploadController extends \app\controllers\Controller
'url' => $fileInfo['url'],
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'],"."))
'type' => $this->getFileType(trim($fileInfo['type'], "."))
]
];
} else {
@ -509,7 +572,8 @@ class UploadController extends \app\controllers\Controller
* @TODO 扩展上传至第三方存储
* 文件上传
*/
private function uploadFile($action){
private function uploadFile($action)
{
//获取上传配置
$uploadConfig = $this->getUploadConfig($action);
$path = $uploadConfig["path"];
@ -575,12 +639,12 @@ class UploadController extends \app\controllers\Controller
/* 返回数据 */
$fileInfo = $up->getFileInfo();
if($fileInfo["state"] == "SUCCESS"){
if ($fileInfo["state"] == "SUCCESS") {
$this->imageCompress($fileInfo);
$model = new UploadFile();
$model->user_id = Model::getUserId();
$model->file_url = $fileInfo["url"];
$model->extension = trim($fileInfo["type"],".");
$model->extension = trim($fileInfo["type"], ".");
$model->type = $this->getFileType($model->extension);
$model->size = $fileInfo["size"];
$model->group_id = \Yii::$app->request->get("group_id");
@ -592,18 +656,18 @@ class UploadController extends \app\controllers\Controller
private function imageCompress(&$fileInfo)
{
$filetype = $this->getFileType(trim($fileInfo["type"],"."));
if($filetype != "image")
$filetype = $this->getFileType(trim($fileInfo["type"], "."));
if ($filetype != "image")
return;
$imageCompressConfig = $this->getImageCompressConfig();
$filename = trim($fileInfo["url"],'/');
if($imageCompressConfig['imageCompressEnable'] && file_exists($filename)){
try{
$filename = trim($fileInfo["url"], '/');
if ($imageCompressConfig['imageCompressEnable'] && file_exists($filename)) {
try {
$gumletImagic = new ImageResize($filename);
$gumletImagic->resizeToLongSide($imageCompressConfig['imageCompressBorder']);
$gumletImagic->save($filename);
clearstatcache();
} catch (ImageResizeException $ex){
} catch (ImageResizeException $ex) {
}
}
$fileInfo["size"] = filesize($filename);
@ -618,14 +682,14 @@ class UploadController extends \app\controllers\Controller
{
$config = [];
$imageCompressEnable = \Yii::$app->request->get('imageCompressEnable');
if($imageCompressEnable !== null){
if ($imageCompressEnable !== null) {
$config['imageCompressEnable'] = $imageCompressEnable == 1 ? true : false;
} else {
$config['imageCompressEnable'] = $this->uploadConfig['uploadFile']['imageCompressEnable'];
}
$imageCompressBorder = \Yii::$app->request->get('imageCompressBorder');
if($imageCompressBorder !== null && is_numeric($imageCompressBorder)){
if ($imageCompressBorder !== null && is_numeric($imageCompressBorder)) {
$imageCompressBorder = (int)$imageCompressBorder;
$config['imageCompressBorder'] = $imageCompressBorder;
} else {
@ -651,7 +715,8 @@ class UploadController extends \app\controllers\Controller
/**
* 获取已经上传的文件列表
*/
private function getFileList($action){
private function getFileList($action)
{
//获取上传配置
$uploadConfig = $this->getUploadConfig($action);
$_path = $uploadConfig["path"];
@ -661,7 +726,7 @@ class UploadController extends \app\controllers\Controller
switch ($action) {
/* 列出文件 */
case 'listfile':
$_config['fileManagerListPath'] = $_path .$_config['fileManagerListPath'];
$_config['fileManagerListPath'] = $_path . $_config['fileManagerListPath'];
$allowFiles = $_config['fileManagerAllowFiles'];
$listSize = $_config['fileManagerListSize'];
$path = $_config['fileManagerListPath'];
@ -682,7 +747,7 @@ class UploadController extends \app\controllers\Controller
$end = $start + $size;
/* 获取文件列表 */
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
$files = $this->getfiles($path, $allowFiles);
if (!count($files)) {
return json_encode(array(
@ -695,7 +760,7 @@ class UploadController extends \app\controllers\Controller
/* 获取指定范围的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
@ -723,7 +788,7 @@ class UploadController extends \app\controllers\Controller
private function getfiles($path, $allowFiles, &$files = array())
{
if (!is_dir($path)) return null;
if(substr($path, strlen($path) - 1) != '/') $path .= '/';
if (substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
@ -731,10 +796,10 @@ class UploadController extends \app\controllers\Controller
if (is_dir($path2)) {
$this->getfiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = array(
'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime'=> filemtime($path2)
'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime' => filemtime($path2)
);
}
}
@ -746,7 +811,8 @@ class UploadController extends \app\controllers\Controller
/**
* 获取上传路径前缀,及配置信息
*/
private function getUploadConfig($action){
private function getUploadConfig($action)
{
$path = UploadHelper::getUploadPath();
$config = $this->uploadConfig['uploadFile'];
@ -760,7 +826,8 @@ class UploadController extends \app\controllers\Controller
/**
* 抓取远程图片
*/
private function crawlerImage($action) {
private function crawlerImage($action)
{
set_time_limit(0);
//获取上传配置
$uploadConfig = $this->getUploadConfig($action);
@ -798,29 +865,29 @@ class UploadController extends \app\controllers\Controller
/* 返回抓取数据 */
return json_encode(array(
'state'=> count($list) ? 'SUCCESS': 'ERROR',
'list'=> $list
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
));
}
public function actionVideo()
{
set_time_limit (0);
set_time_limit(0);
$params = \Yii::$app->request->post();
$tempDir = './temp/upload/video/chunks/'.date('Ymd',time()) . '/' . md5($params['name']);
if(!file_exists($tempDir))
$tempDir = './temp/upload/video/chunks/' . date('Ymd', time()) . '/' . md5($params['name']);
if (!file_exists($tempDir))
$this->mk_dir($tempDir);
if(!isset($params['chunk'])){
if (!isset($params['chunk'])) {
$params['chunk'] = 0;
}
if(!isset($params['chunks'])){
if (!isset($params['chunks'])) {
$params['chunks'] = 1;
}
//获取上传配置
$action = 'uploadvideo';
$uploadConfig = $this->getUploadConfig($action);
$config = array(
"pathFormat" => $tempDir . '/' .$params['chunk'],
"pathFormat" => $tempDir . '/' . $params['chunk'],
"maxSize" => $uploadConfig['config']['videoMaxSize'],
"allowFiles" => $uploadConfig['config']['videoAllowFiles']
);
@ -830,17 +897,17 @@ class UploadController extends \app\controllers\Controller
$fileInfo = $up->getFileInfo();
$chunk = strval($params['chunk']);
$chunkCount = strval($params['chunks']);
if($chunk + 1 == $chunkCount){
if ($chunk + 1 == $chunkCount) {
//合并
$targetDir = './upload/video/'.date('Ymd',time()). '/' . md5($params['name']);
if(!file_exists($targetDir))
$targetDir = './upload/video/' . date('Ymd', time()) . '/' . md5($params['name']);
if (!file_exists($targetDir))
$this->mk_dir($targetDir);
$finalName = $targetDir . $fileInfo['type'];
if(file_exists($finalName))
if (file_exists($finalName))
unlink($finalName);
$file = fopen($finalName, 'wb');
for($index = 0; $index < $chunkCount; $index++){
$tmpFile = $tempDir . '/'. $index . $fileInfo['type'];
for ($index = 0; $index < $chunkCount; $index++) {
$tmpFile = $tempDir . '/' . $index . $fileInfo['type'];
$chunkFile = fopen($tmpFile, 'rb');
$content = fread($chunkFile, filesize($tmpFile));
fclose($chunkFile);
@ -859,8 +926,8 @@ class UploadController extends \app\controllers\Controller
return $fileInfo;
}
private function mk_dir($dir,$mode = 0777)
private function mk_dir($dir, $mode = 0777)
{
return is_dir($dir) or $this->mk_dir(dirname($dir),$mode) and mkdir($dir,$mode);
return is_dir($dir) or $this->mk_dir(dirname($dir), $mode) and mkdir($dir, $mode);
}
}

View File

@ -109,7 +109,7 @@ $this->registerJsFile($layui_js_file, ['depends' => 'app\modules\admin\assets\Ap
<tr>
<th>名称</th>
<th>文件</th>
<th>大小</th>
<!-- <th>大小</th>-->
<th>状态</th>
<th style="width:188px">操作</th>
</tr>
@ -118,7 +118,7 @@ $this->registerJsFile($layui_js_file, ['depends' => 'app\modules\admin\assets\Ap
<tr v-for="(item,index) in filelist">
<td>{{item.name}}</td>
<td>{{item.url}}</td>
<td>{{item.size_cn}}</td>
<!-- <td>{{item.size_cn}}</td>-->
<td>
<template v-if="item.status == 0">
<span class="layui-badge layui-bg-gray">待上传</span>