222 lines
7.4 KiB
PHP
222 lines
7.4 KiB
PHP
<?php
|
||
|
||
namespace App\Classc;
|
||
|
||
use App\Models\File;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Auth;
|
||
use AnyUpload;
|
||
|
||
class UploadFile
|
||
{
|
||
public static function config($type = 'images')
|
||
{
|
||
$config = [
|
||
'fileType' => $type,
|
||
'nameMd5' => 'md5',
|
||
"maxSize" => 204800000, /* 上传大小限制,单位B */
|
||
"compressEnable" => true, /* 是否压缩图片,默认是true */
|
||
"urlPrefix" => "", /* 图片访问路径前缀 */
|
||
"pathFormat" => config('adconfig.upload_path') . "/images/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||
];
|
||
switch ($type) {
|
||
case 'image':
|
||
$config['allowFiles'] = [".png", ".jpg", ".jpeg", ".gif", ".bmp"];
|
||
break;
|
||
case 'zip':
|
||
$config['allowFiles'] = ['zip', ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2"];
|
||
$config['maxSize'] = 52428800;//50M
|
||
$config['pathFormat'] = config('adconfig.upload_path') . "/zips/{yyyy}{mm}{dd}/{time}{rand:6}"; /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||
break;
|
||
case
|
||
'file':
|
||
$config['allowFiles'] = [
|
||
".png", ".jpg", ".jpeg", ".gif", ".bmp",
|
||
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".wmv",
|
||
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
|
||
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
|
||
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml", ".psd", ".ai", ".cdr",
|
||
".cer", ".p12",".pem"
|
||
];
|
||
$config['pathFormat'] = config('adconfig.upload_path') . "/files/{yyyy}{mm}{dd}/{time}{rand:6}"; /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||
break;
|
||
case 'vedio':
|
||
$config['allowFiles'] = [".mp4"];
|
||
$config['pathFormat'] = config('adconfig.upload_path') . "/vedios/{yyyy}{mm}{dd}/{time}{rand:6}"; /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||
break;
|
||
case 'excel':
|
||
$config['allowFiles'] = [
|
||
".xls", ".xlsx"
|
||
];
|
||
$config['pathFormat'] = config('adconfig.upload_path') . "/excels/{yyyy}{mm}{dd}/{time}{rand:6}"; /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||
break;
|
||
default:
|
||
$config['allowFiles'] = [".png", ".jpg", ".jpeg", ".gif", ".bmp"];
|
||
break;
|
||
}
|
||
|
||
|
||
return $config;
|
||
}
|
||
|
||
public static function remote($filename = "file", $type = 'image', $is_oss = '', $uptype)
|
||
{
|
||
$config = self::config($type);
|
||
$up = AnyUpload::config($filename, $config, $uptype);
|
||
$result = AnyUpload::getFileInfo();
|
||
return $result;
|
||
}
|
||
|
||
public static function uploadBase64($filename = "file", $is_oss = '', $source = 'admin')
|
||
{
|
||
$config = self::config('image');
|
||
$up = AnyUpload::config($filename, $config, 'base64');
|
||
$result = $up->getFileInfo();
|
||
return $result;
|
||
|
||
}
|
||
|
||
public static function addFileDb($result, $type = 'admin')
|
||
{
|
||
//存如数据库
|
||
if ($type == 'user') {
|
||
$create_id = 0;
|
||
$create_type = 'user';
|
||
} else {
|
||
$create_id = 1;//admin('id');
|
||
$create_type = 'admin';
|
||
}
|
||
$data = [
|
||
'oss_type' => $result['oss_type'],
|
||
'path' => $result['path'],
|
||
'filename' => $result['filename'],
|
||
'size' => $result['size'],
|
||
'tmp' => $result['tmpname'],
|
||
'user_id' => $create_id,
|
||
'user_type' => $create_type,
|
||
'type' => $result['type']
|
||
];
|
||
return File::add($data);
|
||
}
|
||
|
||
public static function addOss($aburl, $url)
|
||
{
|
||
$r = Storage::put($url, file_get_contents($aburl));
|
||
return $r;
|
||
}
|
||
|
||
public static function upload($filename = "file", $type = 'image', $method = 'image', $thumbs = [], $is_oss = false)
|
||
{
|
||
|
||
$config = self::config($type);
|
||
//判断是否压缩图片,
|
||
if (count($thumbs) > 0) {
|
||
$config['thumbs'] = $thumbs;
|
||
}
|
||
//dd($config);
|
||
$up = AnyUpload::config($filename, $config, $method);
|
||
$result = AnyUpload::getFileInfo();
|
||
if ($result['success'] == 1) {
|
||
$result['oss_type'] = 'local';
|
||
$is_oss = $is_oss ? $is_oss : config('adconfig.is_oss');
|
||
//如果是OSS
|
||
if ($is_oss == 1) {
|
||
$result['oss_type'] = 'oss';
|
||
if (self::addOss($result['abpath'], $result['path'])) {
|
||
self::deleteLocalFile($result['path'], 0);//删除自己路径
|
||
$result['oss_url'] = Storage::url($result['path']);
|
||
}
|
||
|
||
}
|
||
self::addFileDb($result);//入库
|
||
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
public static function uploadUser($filename = "file", $type = 'image', $method = 'image', $thumbs = [], $is_oss = false)
|
||
{
|
||
|
||
$config = self::config($type);
|
||
//判断是否压缩图片,
|
||
if (count($thumbs) > 0) {
|
||
$config['thumbs'] = $thumbs;
|
||
}
|
||
|
||
$up = AnyUpload::config($filename, $config, $method);
|
||
$result = $up->getFileInfo();
|
||
if ($result['success'] == 1) {
|
||
$result['storage'] = 1;
|
||
if ($method != 'avatar') {
|
||
self::addFileDb($result, 'user');//入库
|
||
}
|
||
|
||
$is_oss = $is_oss ? $is_oss : config('website.is_oss');
|
||
//如果是OSS
|
||
if ($is_oss == 1) {
|
||
$result['storage'] = 2;
|
||
if (self::addOss($result['abpath'], $result['path'])) {
|
||
self::deleteLocalFile($result['path'], 0);//删除自己路径
|
||
$result['oss_url'] = Storage::url($result['path']);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
public static function deleteLocalFile($filepath, $del_db = 1)
|
||
{
|
||
if (is_array($filepath)) {
|
||
foreach ($filepath as $v) {
|
||
self::deleteLocalFile($v);
|
||
}
|
||
}
|
||
$ofilename = $filepath;
|
||
//附加前缀
|
||
$filepath = str_replace("\\", "/", public_path()) . $filepath;
|
||
if (is_dir($filepath)) {
|
||
return false;
|
||
} elseif (file_exists($filepath)) {
|
||
$r = unlink($filepath);
|
||
if ($r) {
|
||
if ($del_db) {
|
||
File::where('path', $ofilename)->delete();
|
||
}
|
||
|
||
return true;
|
||
|
||
}
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
public static function deleteOssFile($filepath)
|
||
{
|
||
$filepath = is_array($filepath) ? $filepath : [$filepath];
|
||
$r = Storage::delete($filepath);
|
||
if ($r) {
|
||
//从数据库里面删除
|
||
File::whereIn('path', $filepath)->delete();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public static function deleteFile($url = '', $is_oss = 0)
|
||
{
|
||
$is_oss = $is_oss ? $is_oss : config('adconfig.is_oss');
|
||
if ($is_oss) {
|
||
return self::deleteOssFile($url);
|
||
} else {
|
||
return self::deleteLocalFile($url);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
} |