2026-04-30 21:18:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Phase: upload — OSS 上传
|
|
|
|
|
|
*
|
2026-05-02 00:14:40 +08:00
|
|
|
|
* 将图片(含首尾帧)和视频上传到 OSS,回写 url / videoUrl
|
2026-04-30 21:18:31 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const path = require('path')
|
2026-05-03 16:11:08 +08:00
|
|
|
|
const fs = require('fs')
|
2026-04-30 21:18:31 +08:00
|
|
|
|
const { saveManifest, log, getManifestDir } = require('./pipeline-utils')
|
|
|
|
|
|
|
2026-05-03 16:11:08 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 自动修复 item.file:如果当前 file 不存在,从 candidates 中匹配实际存在的文件。
|
|
|
|
|
|
* 用户手动换图时可能删除了非选中候选,导致 file 指向不存在的文件。
|
|
|
|
|
|
*/
|
|
|
|
|
|
function autoFixFile(item, dir) {
|
|
|
|
|
|
const currentPath = path.resolve(dir, item.file)
|
|
|
|
|
|
if (fs.existsSync(currentPath)) return false
|
|
|
|
|
|
|
|
|
|
|
|
const cands = item.candidates || []
|
|
|
|
|
|
const existing = cands.filter(c => fs.existsSync(path.resolve(dir, c)))
|
|
|
|
|
|
|
|
|
|
|
|
if (existing.length === 0) {
|
|
|
|
|
|
log('upload', ` ⚠ ${item.file} 不存在,candidates 中也没有文件`)
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const oldFile = item.file
|
|
|
|
|
|
const candMatch = oldFile.match(/_cand(\d+)\./)
|
|
|
|
|
|
const targetCand = candMatch ? `_cand${candMatch[1]}.` : null
|
|
|
|
|
|
const matched = targetCand
|
|
|
|
|
|
? (existing.find(c => c.includes(targetCand)) || existing[0])
|
|
|
|
|
|
: existing[0]
|
|
|
|
|
|
|
|
|
|
|
|
item.file = matched
|
|
|
|
|
|
log('upload', ` 🔧 ${path.basename(oldFile)} → ${path.basename(matched)} (${existing.length}/${cands.length} 候选存在)`)
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-30 21:18:31 +08:00
|
|
|
|
async function phaseUpload(manifest, manifestPath) {
|
2026-05-03 16:11:08 +08:00
|
|
|
|
const dir = getManifestDir(manifestPath)
|
|
|
|
|
|
const { uploadFile } = require('../oss-upload')
|
|
|
|
|
|
|
|
|
|
|
|
// 自动修复:用户手动换图后 file 可能指向已删除的候选
|
|
|
|
|
|
let fixedCount = 0
|
|
|
|
|
|
for (const item of manifest.items) {
|
|
|
|
|
|
if (item.status === 'done' && item.file && !item.url) {
|
|
|
|
|
|
if (autoFixFile(item, dir)) fixedCount++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (fixedCount > 0) {
|
|
|
|
|
|
saveManifest(manifestPath, manifest)
|
|
|
|
|
|
log('upload', `已自动修复 ${fixedCount} 个 file 路径`)
|
|
|
|
|
|
}
|
2026-04-30 21:18:31 +08:00
|
|
|
|
|
2026-05-03 16:11:08 +08:00
|
|
|
|
// 图片(含首尾帧 first frame)
|
|
|
|
|
|
const imageItems = manifest.items.filter(it =>
|
|
|
|
|
|
it.status === 'done' && it.file && !it.url
|
|
|
|
|
|
)
|
|
|
|
|
|
// 视频
|
|
|
|
|
|
const videoItems = manifest.items.filter(it =>
|
|
|
|
|
|
it.status === 'done' && it.video && !it.videoUrl
|
|
|
|
|
|
)
|
2026-04-30 21:18:31 +08:00
|
|
|
|
|
2026-05-03 16:11:08 +08:00
|
|
|
|
if (imageItems.length === 0 && videoItems.length === 0) {
|
|
|
|
|
|
log('upload', '无待上传文件,跳过')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-04-30 21:18:31 +08:00
|
|
|
|
|
2026-05-03 16:11:08 +08:00
|
|
|
|
// 上传图片
|
|
|
|
|
|
if (imageItems.length > 0) {
|
|
|
|
|
|
log('upload', `图片: ${imageItems.length} 个`)
|
|
|
|
|
|
for (let i = 0; i < imageItems.length; i++) {
|
|
|
|
|
|
const item = imageItems[i]
|
|
|
|
|
|
const filePath = path.resolve(dir, item.file)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { url } = await uploadFile(filePath)
|
|
|
|
|
|
item.url = url
|
|
|
|
|
|
log('upload', ` [${i + 1}/${imageItems.length}] ${item.file} → OK`)
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
item.error = `上传失败: ${err.message}`
|
|
|
|
|
|
log('upload', ` [${i + 1}/${imageItems.length}] 失败: ${err.message}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 首尾帧模式:上传 lastFrame
|
|
|
|
|
|
if (item.url && item.lastFrame && !item.lastFrameUrl) {
|
|
|
|
|
|
const lastPath = path.resolve(dir, item.lastFrame)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { url } = await uploadFile(lastPath)
|
|
|
|
|
|
item.lastFrameUrl = url
|
|
|
|
|
|
log('upload', ` [${i + 1}/${imageItems.length}] lastFrame → OK`)
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
log('upload', ` [${i + 1}/${imageItems.length}] lastFrame 上传失败: ${err.message}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
saveManifest(manifestPath, manifest)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-02 00:14:40 +08:00
|
|
|
|
|
2026-05-03 16:11:08 +08:00
|
|
|
|
// 上传视频
|
|
|
|
|
|
if (videoItems.length > 0) {
|
|
|
|
|
|
log('upload', `视频: ${videoItems.length} 个`)
|
|
|
|
|
|
for (let i = 0; i < videoItems.length; i++) {
|
|
|
|
|
|
const item = videoItems[i]
|
|
|
|
|
|
const videoPath = path.resolve(dir, item.video)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { url } = await uploadFile(videoPath)
|
|
|
|
|
|
item.videoUrl = url
|
|
|
|
|
|
log('upload', ` [${i + 1}/${videoItems.length}] ${item.video} → OK`)
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
log('upload', ` [${i + 1}/${videoItems.length}] 失败: ${err.message}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
saveManifest(manifestPath, manifest)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-30 21:18:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { phaseUpload }
|