162 lines
4.0 KiB
PHP
162 lines
4.0 KiB
PHP
<?php
|
|
|
|
use Carbon\Carbon;
|
|
use App\Events\FileUploadEvent;
|
|
use Dipper\Foundation\Support\Time;
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
if (! function_exists('cdn')) {
|
|
/**
|
|
* 生成CDN链接, 可带时间戳防盗链
|
|
*
|
|
* @param string $disk 磁盘名称
|
|
* @param string $uri 路径, e.g. common/test/a.jpg/640
|
|
* @param bool $deadline 授权过期时间, 默认true
|
|
*
|
|
* @return string
|
|
*/
|
|
function cdn($disk, $uri, $deadline = true, $size = 640)
|
|
{
|
|
static $disks;
|
|
|
|
if (!$disks[$disk]) {
|
|
$disks[$disk] = app('filesystem')->disk($disk);
|
|
}
|
|
|
|
$disk = $disks[$disk];
|
|
|
|
if ($deadline === true) {
|
|
$deadline = Carbon::today()->addDay(3)->addHour(1)->timestamp;
|
|
} elseif ($deadline !== false) {
|
|
$deadline = Time::parse($deadline)->timestamp;
|
|
}
|
|
|
|
if ($deadline) {
|
|
$symbol = strpos($uri, '?') ? '&' : '?';
|
|
$uri = trim($uri, '/') . $symbol . 'timestamp=' . $deadline;
|
|
}
|
|
|
|
return $disk->url($uri);
|
|
}
|
|
}
|
|
|
|
if (! function_exists('account_avatar')) {
|
|
/**
|
|
* 单次获取账户头像
|
|
*
|
|
* @param int $accountId
|
|
* @param int $deadline
|
|
* @param bool $size
|
|
* @return string
|
|
*/
|
|
function account_avatar($accountId, $deadline = true, $size = 64)
|
|
{
|
|
$path = cdn('data', \App\Domains\Account\Services\AccountService::generateAvatarPath($accountId), $deadline, $size);
|
|
return $path;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('save_cover')) {
|
|
/**
|
|
* 保存封面
|
|
*
|
|
* @param string $type
|
|
* @param int $id
|
|
* @param UploadedFile $file
|
|
* @return void
|
|
*/
|
|
function save_cover($type, $id, $file)
|
|
{
|
|
if ($file instanceof UploadedFile && !$file->isValid()) {
|
|
throw new \App\Exceptions\InvalidArgumentException('文件不正确或已损坏');
|
|
}
|
|
|
|
if (!in_array($file->guessExtension(), ['jpg', 'jpeg', 'gif', 'png', 'bmp'])) {
|
|
throw new \App\Exceptions\InvalidArgumentException('只支持jpg,jpeg,gif,png,bmp文件格式');
|
|
}
|
|
|
|
$filename = "/$type/$id.jpg";
|
|
|
|
Event::fire(new FileUploadEvent($file, [
|
|
'filename' => $filename,
|
|
'type' => $type,
|
|
'typeid' => $id,
|
|
'cover' => true,
|
|
], $disk = 'data'));
|
|
|
|
$url = cdn('data', $filename, time());
|
|
|
|
return $url;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('get_cover')) {
|
|
/**
|
|
* 保存封面
|
|
*
|
|
* @param string $type
|
|
* @param int $id
|
|
* @param UploadedFile $file
|
|
* @return void
|
|
*/
|
|
function get_cover($type, $id, $deadline = true, $size = '640')
|
|
{
|
|
$filename = "/$type/$id.jpg";
|
|
$path = cdn('data', $filename, $deadline, $size);
|
|
return $path;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('config_path')) {
|
|
/**
|
|
* Get the configuration path.
|
|
*
|
|
* @param string $path
|
|
* @return string
|
|
*/
|
|
function config_path($path = '')
|
|
{
|
|
$path = 'config' . DIRECTORY_SEPARATOR . ($path ? DIRECTORY_SEPARATOR.$path : $path);
|
|
|
|
return base_path($path);
|
|
}
|
|
}
|
|
|
|
if (! function_exists('array_merge_sum')) {
|
|
/**
|
|
* 合并数组(相同键名相加)
|
|
*
|
|
* @param array $arr1
|
|
* @param array $arr2
|
|
* @return void
|
|
*/
|
|
function array_merge_sum(...$arrays)
|
|
{
|
|
$array = [];
|
|
|
|
foreach ($arrays as $item) {
|
|
foreach ($item as $key => $value) {
|
|
$array[$key] = isset($array[$key]) ? $array[$key] + $value : $value;
|
|
}
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('human_filesize')) {
|
|
/**
|
|
* 文件大小可读性
|
|
*
|
|
* @param array $arr1
|
|
* @param array $arr2
|
|
* @return void
|
|
*/
|
|
function human_filesize($bytes, $decimals = 2)
|
|
{
|
|
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
|
|
}
|
|
}
|