65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
||
namespace app\service;
|
||
|
||
use think\file\UploadedFile;
|
||
|
||
class File
|
||
{
|
||
//上传文件移动到上传文件夹
|
||
public static function move(UploadedFile $file)
|
||
{
|
||
$upload_path = 'storage/uploads/' . date('Ymd');
|
||
$path = app()->getRootPath() . $upload_path;
|
||
$filename = uniqid() . '.' . $file->extension();
|
||
$upload_filename = '/' . $upload_path . '/' . $filename;
|
||
return [$file->move($path, $filename), $file, $upload_filename];
|
||
}
|
||
|
||
/**
|
||
* 文件访问路径转换为完整的url
|
||
* @param string|null $fileUrl
|
||
* @param bool $ossAnalysis 是否进行OSS解析
|
||
* @return string
|
||
* @todo 若启用OOS存储,需根据业务配置调整$fileDomain
|
||
*
|
||
*/
|
||
public static function convertCompleteFileUrl(?string $fileUrl, bool $ossAnalysis=true): string
|
||
{
|
||
if (empty($fileUrl)) {
|
||
return '';
|
||
}
|
||
if ($ossAnalysis) {
|
||
$fileDomain = self::getFileDomain();
|
||
} else {
|
||
$fileDomain = request()->domain();
|
||
}
|
||
$prefix = substr($fileUrl, 0, 4);
|
||
if (!($prefix == 'http')) {
|
||
$fileUrl = $fileDomain.'/'.ltrim($fileUrl, '/');
|
||
}
|
||
|
||
return $fileUrl;
|
||
}
|
||
|
||
/**
|
||
* 文件访问域名前缀
|
||
*
|
||
* @return string
|
||
*/
|
||
public static function getFileDomain(): string
|
||
{
|
||
$confBase = ExtraConfig::base();
|
||
$confOss = ExtraConfig::aliOss();
|
||
$isOss = $confBase['oss'] ?? 'false';
|
||
$ossDomain = $confOss['customDomain'] ?? '';
|
||
|
||
// 默认为当前域名
|
||
$fileDomain = request()->domain();
|
||
if ($isOss == 'true' && !empty($ossDomain)) {
|
||
$fileDomain = $ossDomain;
|
||
}
|
||
|
||
$fileDomain = trim($fileDomain);
|
||
return rtrim($fileDomain, '/');
|
||
}
|
||
} |