6
0
Fork 0
jiqu-library-server/app/Console/Commands/OssCopy.php

165 lines
4.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Ad;
use App\Models\Article;
use App\Models\DrawActivity;
use App\Models\DrawPrize;
use App\Models\ProductBuynote;
use App\Models\ProductCategory;
use App\Models\ProductFeature;
use App\Models\ProductSpu;
use App\Models\ShareBg;
use App\Models\Store\ProductSku;
use App\Models\Store\Store;
use App\Models\UserInfo;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use OSS\OssClient;
class OssCopy extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'oss:copy';
/**
* The console command description.
*
* @var string
*/
protected $description = '将OSS的资源转存到另外的OSS';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
// 用户头像
foreach(UserInfo::get() as $item) {
$this->updateModel($item, ['avatar']);
}
// 文章
foreach(Article::get() as $item) {
$this->updateModel($item, ['cover', 'content']);
}
// 广告
foreach(Ad::get() as $item) {
$this->updateModel($item, ['image']);
}
// 门店
foreach(Store::get() as $item) {
$this->updateModel($item, ['image']);
}
// 商品
foreach(ProductSpu::get() as $item) {
$this->updateModel($item, ['cover', 'images', 'media', 'description']);
}
foreach(ProductSku::get() as $item) {
$this->updateModel($item, ['cover', 'images', 'media', 'description']);
}
// 商品分类
foreach(ProductCategory::get() as $item) {
$this->updateModel($item, ['icon']);
}
// 商品特点
foreach(ProductFeature::get() as $item) {
$this->updateModel($item, ['icon']);
}
// 购买须知
foreach(ProductBuynote::get() as $item) {
$this->updateModel($item, ['content']);
}
// 分享背景
foreach(ShareBg::get() as $item) {
$this->updateModel($item, ['image']);
}
// 抽奖活动
foreach(DrawActivity::get() as $item) {
$this->updateModel($item, ['desc', 'bg_image']);
}
// 抽奖奖品
foreach(DrawPrize::get() as $item) {
$this->updateModel($item, ['icon']);
}
return 0;
}
protected function updateModel(Model $model, array $attributes = [])
{
$data = [];
foreach($attributes as $key) {
$item = $model->$key;
if (!$item) {
continue;
}
// 数组
if (is_array($item)) {
$values = [];
foreach($item as $value) {
array_push($values, $this->copy($value));
}
$data[$key] = $values;
}
// 富文本
else if (preg_match('/^\<.+\>$/i', $item)) {
$data[$key] = $this->parseEditorImage($item);
} else {
$data[$key] = $this->copy($item);
}
}
if (count($data) > 0) {
$model->forceFill($data)->save();
}
}
protected function copy($url)
{
if ($url) {
$config = config('filesystems.disks.aliyun');
$client = new OssClient(data_get($config, 'access_id'), data_get($config, 'access_key'), data_get($config, 'endpoint'));
$baseUrl = 'https://zcs-test.oss-cn-chengdu.aliyuncs.com/';
$newUrl = 'https://jiqu-library.oss-cn-chengdu.aliyuncs.com/';
$oldBucket = data_get($config, 'bucket');
if (Str::startsWith($url, $baseUrl)) {
$path = substr($url, strlen($baseUrl));
$bucket = 'jiqu-library';
$client->copyObject($oldBucket, $path, $bucket, $path);
return $newUrl . $path;
}
}
return $url;
}
protected function parseEditorImage($content)
{
$list = [];
preg_match_all("/src=\"(.+)\"/iU", $content, $list);
$replaces = [];
foreach($list[1] as $item) {
array_push($replaces, ['search' => $item, 'value' => $this->copy($item)]);
}
$str = $content;
foreach($replaces as $item) {
$str = str_replace($item['search'], $item['value'], $str);
}
return $str;
}
}