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

145 lines
4.7 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()
{
$list = [
['name' => '用户', 'list' => UserInfo::get(), 'attributes' => ['avatar']],
['name' => '文章', 'list' => Article::get(), 'attributes' => ['cover', 'content']],
['name' => '广告', 'list' => Ad::get(), 'attributes' => ['image']],
['name' => '门店', 'list' => Store::get(), 'attributes' => ['image']],
['name' => '商品SPU', 'list' => ProductSpu::get(), 'attributes' => ['cover', 'images', 'media', 'description']],
['name' => '商品SKU', 'list' => ProductSku::get(), 'attributes' => ['cover', 'images', 'media', 'description']],
['name' => '商品分类', 'list' => ProductCategory::get(), 'attributes' => ['icon']],
['name' => '商品特点', 'list' => ProductFeature::get(), 'attributes' => ['icon']],
['name' => '购买须知', 'list' => ProductBuynote::get(), 'attributes' => ['content']],
['name' => '分享背景', 'list' => ShareBg::get(), 'attributes' => ['image']],
['name' => '抽奖活动', 'list' => DrawActivity::get(), 'attributes' => ['desc', 'bg_image']],
['name' => '抽奖奖品', 'list' => DrawPrize::get(), 'attributes' => ['icon']],
];
foreach($list as $item) {
$collection = $item['list'];
$this->info($item['name'] . ': ' . $collection->count());
$bar = $this->output->createProgressBar($collection->count());
$bar->start();
foreach($collection as $model) {
$this->updateModel($model, $item['attributes']);
$bar->advance();
}
$bar->finish();
$this->line('');
}
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 = 'zcs-test';
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;
}
}