// 自媒体
'spider' => [
'asset' => [ // 资源
'image' => [ // 图片
'hostInfo' => 'http://localhost/channel-pub-api/storage/spider', // HOME URL
'baseUrl' => '/images', // BASE URL
'basePath' => 'E:/wwwroot/channel-pub-api/storage/spider/images', // BASE PATH
],
'video' => [ // 视频
'hostInfo' => 'http://localhost/channel-pub-api/storage/spider', // HOME URL
'baseUrl' => '/videos', // BASE URL
'basePath' => 'E:/wwwroot/channel-pub-api/storage/spider/videos', // BASE PATH
],
],
],
// 渠道发布接口
'channelPubApi' => [
'asset' => [ // 资源
'image' => [ // 图片
'basePath' => 'E:/wwwroot/channel-pub-api/storage/channel-pub-api/images', // BASE PATH
],
'video' => [ // 视频
'basePath' => 'E:/wwwroot/channel-pub-api/storage/channel-pub-api/videos', // BASE PATH
],
],
],
<pre class="wp-block-syntaxhighlighter-code">
<?php
/**
* Created by PhpStorm.
* User: WangQiang
* Date: 2018/09/20
* Time: 14:04
*/
namespace common\services;
use Yii;
use yii\base\Model;
use common\logics\ChannelAppSource;
use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;
use yii\helpers\FileHelper;
use yii\web\NotFoundHttpException;
use yii\web\UnprocessableEntityHttpException;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
/**
* 资源服务
*
* @author Qiang Wang <shuijingwanwq@163.com>
* @since 1.0
*/
class AssetService extends Service
{
/**
* 复制来源的资源文件至渠道发布的资源目录,返回相对路径
* @param string $source 来源
* 格式如下:spider
* @param array $assets 来源的资源文件的绝对URL
* 格式如下:
* [
* [
* 'type' => 'image',
* 'absolute_url' => 'http://localhost/channel-pub-api/storage/spider/images/1.png',
* ],
* [
* 'type' => 'video',
* 'absolute_url' => 'http://localhost/channel-pub-api/storage/spider/videos/7月份北上广深等十大城市租金环比上涨 看东方 20180820 高清_高清.mp4',
* ],
* ]
*
* @return array $channelPubApiAssetAbsolutePaths 渠道发布的资源文件的相对路径
* 格式如下:
* [
* [
* 'type' => 'image',
* 'relative_path' => '/2018/09/20/1537439889.2333.1441541478.png',
* ],
* [
* 'type' => 'video',
* 'relative_path' => '/2018/09/20/1537439889.2403.62871268.mp4',
* ],
* ]
*
* @throws UnprocessableEntityHttpException 如果来源的资源文件不是以来源的 HOME URL + BASE URL 开头,将抛出 422 HTTP 异常
* @throws NotFoundHttpException 如果来源的资源文件不存在,将抛出 404 HTTP 异常
* @throws ServerErrorHttpException 如果创建目录失败,将抛出 500 HTTP 异常
* @throws ServerErrorHttpException 如果复制来源的资源文件至渠道发布的资源目录失败,将抛出 500 HTTP 异常
* @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes)
*/
public static function copyAssets($source, $assets)
{
// 不是以来源的 HOME URL + BASE URL 开头的来源的资源文件
$notAbsoluteUrlStartKeys = [];
// 不存在的来源的资源文件
$notExistsKeys = [];
// 渠道发布的资源文件的相对路径
$channelPubApiAssetAbsolutePaths = [];
foreach ($assets as $key => $asset) {
// 检查来源的资源文件的绝对URL是否以来源的 HOME URL + BASE URL 开头
if (!StringHelper::startsWith($asset['absolute_url'], Yii::$app->params[$source]['asset'][$asset['type']]['hostInfo'] . Yii::$app->params[$source]['asset'][$asset['type']]['baseUrl'])) {
$notAbsoluteUrlStartKeys[] = $key;
} else {
// 获取来源的资源文件的绝对路径
$sourceAssetAbsolutePath = Yii::$app->params[$source]['asset'][$asset['type']]['basePath'] . str_replace(Yii::$app->params[$source]['asset'][$asset['type']]['hostInfo'] . Yii::$app->params[$source]['asset'][$asset['type']]['baseUrl'], '', $asset['absolute_url']);
// 检查来源的资源文件是否存在
if (!file_exists($sourceAssetAbsolutePath)) {
$notExistsKeys[] = $key;
}
}
}
if (!empty($notAbsoluteUrlStartKeys)) {
$notAbsoluteUrlStartKeys = implode(",", $notAbsoluteUrlStartKeys);
throw new UnprocessableEntityHttpException(Yii::t('common/error', Yii::t('common/error', Yii::t('common/error', '35003'), ['not_absolute_url_start_keys' => $notAbsoluteUrlStartKeys])), 35003);
}
if (!empty($notExistsKeys)) {
$notExistsKeys = implode(",", $notExistsKeys);
throw new NotFoundHttpException(Yii::t('common/error', Yii::t('common/error', Yii::t('common/error', '35004'), ['not_exists_keys' => $notExistsKeys])), 35004);
}
foreach ($assets as $key => $asset) {
// 获取来源的资源文件的绝对路径
$sourceAssetAbsolutePath = Yii::$app->params[$source]['asset'][$asset['type']]['basePath'] . str_replace(Yii::$app->params[$source]['asset'][$asset['type']]['hostInfo'] . Yii::$app->params[$source]['asset'][$asset['type']]['baseUrl'], '', $asset['absolute_url']);
// 获取来源的资源文件的路径信息
$pathInfo = pathinfo($sourceAssetAbsolutePath);
// 渠道发布的资源文件的相对路径
$directory = date('Y/m/d');
$channelPubApiAssetRelativePath = '/' . $directory . '/' . microtime(true) . '.' . mt_rand() . '.' . $pathInfo['extension'];
// 渠道发布的资源文件的绝对路径
$channelPubApiAssetAbsolutePath = Yii::$app->params['channelPubApi']['asset'][$asset['type']]['basePath'] . $channelPubApiAssetRelativePath;
// 创建目录
if (!FileHelper::createDirectory(Yii::$app->params['channelPubApi']['asset'][$asset['type']]['basePath'] . '/' . $directory)) {
throw new ServerErrorHttpException(Yii::t('common/error', Yii::t('common/error', Yii::t('common/error', '35005'), ['directory' => Yii::$app->params['channelPubApi']['asset'][$asset['type']]['basePath'] . '/' . $directory])), 35005);
}
// 复制来源的资源文件至渠道发布的资源目录
if (!copy($sourceAssetAbsolutePath, $channelPubApiAssetAbsolutePath)) {
throw new ServerErrorHttpException(Yii::t('common/error', Yii::t('common/error', Yii::t('common/error', '35006'), ['source_asset_absolute_path' => $sourceAssetAbsolutePath])), 35006);
}
$channelPubApiAssetAbsolutePaths[$key] = [
'type' => $asset['type'],
'relative_path' => $channelPubApiAssetRelativePath,
];
}
return $channelPubApiAssetAbsolutePaths;
}
}
</pre>
$assets = [
[
'type' => 'image',
'absolute_url' => 'http://localhost/channel-pub-api/storage/spider/images/1.png',
],
[
'type' => 'video',
'absolute_url' => 'http://localhost/channel-pub-api/storage/spider/videos/7月份北上广深等十大城市租金环比上涨 看东方 20180820 高清_高清.mp4',
],
];
$assetServiceResult = AssetService::copyAssets('spider', $assets);
print_r($assetServiceResult);
exit;
Array
(
[0] => Array
(
[type] => image
[relative_path] => /2018/09/21/1537495742.6172.473015079.png
)
[1] => Array
(
[type] => video
[relative_path] => /2018/09/21/1537495742.6251.651458618.mp4
)
)


PHP / Laravel / Yii2 老项目维护与长期技术支持
如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。
适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护
可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查
如需咨询,请联系我,并注明:PHP 维护咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

发表回复