Files
cunkebao_v3/Server/application/command/SyncConfigMediaCommand.php
Manus AI 2f6187d2fa 部署:DB配置迁移幂等化与场景媒体OSS同步命令
新增 config:sync-media 与 sync_db_config_tencent,部署流程接入 2f 步骤;修复 workbench/内容库迁移 SQL 在生产库可重复执行。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-27 21:10:24 +08:00

82 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\command;
use app\common\service\MediaOssArchiveService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
/**
* 将场景目录等配置中的外链媒体同步到存客宝 OSSres.quwanzhi.com
*/
class SyncConfigMediaCommand extends Command
{
protected function configure()
{
$this->setName('config:sync-media')
->setDescription('同步 plan_scene 等配置表外链图片/视频到 OSS');
}
protected function execute(Input $input, Output $output)
{
$updated = 0;
$skipped = 0;
$failed = 0;
$rows = Db::name('plan_scene')
->where('image', '<>', '')
->whereNotNull('image')
->field('id,name,image')
->select();
foreach ($rows as $row) {
$url = trim((string)($row['image'] ?? ''));
if ($url === '' || MediaOssArchiveService::isOwnOssUrl($url)) {
$skipped++;
continue;
}
$result = MediaOssArchiveService::archiveRemoteUrl(
$url,
'plan_scene',
'cover',
(string)$row['id']
);
if (empty($result['success'])) {
$failed++;
$output->writeln(sprintf(
'FAIL scene #%d %s: %s',
$row['id'],
$row['name'] ?? '',
$result['error'] ?? 'unknown'
));
continue;
}
$newUrl = (string)($result['url'] ?? '');
if ($newUrl !== '' && $newUrl !== $url) {
Db::name('plan_scene')->where('id', $row['id'])->update([
'image' => $newUrl,
'updateTime' => time(),
]);
$updated++;
$output->writeln(sprintf('OK scene #%d -> %s', $row['id'], $newUrl));
} else {
$skipped++;
}
}
$output->writeln(sprintf(
'plan_scene 媒体同步完成:更新 %d跳过 %d失败 %d',
$updated,
$skipped,
$failed
));
return $failed > 0 ? 1 : 0;
}
}