Move repository content images to S3-backed media references

This commit is contained in:
2026-10-29 23:03:11 +08:00
parent 906c6985cb
commit 19bba57561
630 changed files with 5666 additions and 391 deletions
+2 -1
View File
@@ -1,7 +1,8 @@
import {managedMedia} from './managed-media.mjs';
/** Package the 27 audited layers; no new Wiki document routes or copied DB. */
import {readFile,writeFile} from 'node:fs/promises';
const root=new URL('../',import.meta.url),read=p=>readFile(new URL(p,root)),raw=JSON.parse(await read('content/layout-reference/manifest.json')),ids=JSON.parse(await read('content/layout-reference/post-ids.json'));
const image=async p=>'data:image/png;base64,'+(await read('content/layout-reference/assets/'+p.replace(/^asset:/,''))).toString('base64');
const image=async p=>'/api/media/'+managedMedia['content/layout-reference/assets/'+p.replace(/^asset:/,'')].id;
const layouts=[];
for(const m of raw){const elements=[];for(const e of m.elements)elements.push({...e,raster:await image(e.asset),...(e.mask?{shape:await image(e.mask)}:{})});layouts.push({id:m.id,name:m.name,width:m.width,height:m.height,tutorialUrl:'/explore/'+ids[m.id],sourceSha256:m.source.sha256,source:await image(m.reference),background:await image(m.background),elements});}
await writeFile(new URL('apps/studio/src/content/templates/layouts.json',root),JSON.stringify(layouts));
+5 -2
View File
@@ -1,3 +1,4 @@
import {readRepositoryMedia} from './managed-media.mjs';
import {createRequire} from 'node:module';
import {readFile} from 'node:fs/promises';
import {fileURLToPath} from 'node:url';
@@ -9,9 +10,11 @@ const layers=[];let shapes='';
for(const[i,l]of layouts.entries()){
const x=52+i%6*166,y=200+Math.floor(i/6)*232;
shapes+=`<rect x="${x}" y="${y}" width="146" height="222" fill="#faf9f3" stroke="#cbd1ca"/><text x="${x+73}" y="${y+214}" text-anchor="middle" font-size="16" letter-spacing="2">${l.id.toUpperCase()}</text>`;
const bytes=await sharp(path.join(root,'content/layout-reference/assets',l.id,'source.png')).resize({width:130,height:190,fit:'inside'}).png().toBuffer();
const bytes=await sharp(await readRepositoryMedia(`content/layout-reference/assets/${l.id}/source.png`)).resize({width:130,height:190,fit:'inside'}).png().toBuffer();
const meta=await sharp(bytes).metadata();layers.push({input:bytes,left:Math.round(x+(146-meta.width)/2),top:y+7});
}
const svg=`<svg xmlns="http://www.w3.org/2000/svg" width="1080" height="1440"><rect width="1080" height="1440" fill="#eeeee8"/><g fill="#263734" font-family="Arial,PingFang SC,sans-serif"><text x="52" y="150" font-family="Georgia,serif" font-size="136">27</text><text x="230" y="100" font-size="49">原图对应排版</text><text x="232" y="145" font-size="23" letter-spacing="3">一张设计 · 一份构件清单</text><path d="M52 175H1028" stroke="#9ca9a2"/>${shapes}<text x="570" y="1190" font-size="28">构件分层</text><text x="572" y="1237" font-size="23" fill="#2c64d8">T 文字 113</text><text x="780" y="1237" font-size="23" fill="#c8672f">I 图位 51</text><text x="572" y="1276" font-size="23" fill="#08875d">D 装饰 53</text><text x="780" y="1276" font-size="23">共 217 组</text><text x="570" y="1320" font-size="18">原裁片 / 坐标 / 代码 / 验收</text><text x="52" y="1404" font-size="20" letter-spacing="2">WIKI · TEXT + IMAGE</text><text x="1028" y="1404" text-anchor="end" font-size="20">REF-01 — REF-27</text></g></svg>`;
await sharp(Buffer.from(svg)).composite(layers).png().toFile(path.join(root,'content/layout-reference/assets/router-cover.png'));
if(!process.env.MEDIA_OUTPUT_FILE)throw Error('Set MEDIA_OUTPUT_FILE to an external authoring output');
const output=path.resolve(process.env.MEDIA_OUTPUT_FILE);if(output===root||output.startsWith(root+path.sep))throw Error('Output must be outside repository');
await sharp(Buffer.from(svg)).composite(layers).png().toFile(output);
console.log('Created Router cover from all 27 actual source crops.');
+27
View File
@@ -0,0 +1,27 @@
/** Read versioned content from S3-backed media API or an external authoring cache. */
import {readFile,mkdir,writeFile} from 'node:fs/promises';
import {createHash} from 'node:crypto';
import {fileURLToPath} from 'node:url';
import path from 'node:path';
export const repositoryRoot=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
export const managedMedia=JSON.parse(await readFile(path.join(repositoryRoot,'apps/waterfall-wiki/src/content/managed-media.json'),'utf8'));
export async function readRepositoryMedia(name){
const asset=managedMedia[name];if(!asset)throw Error('Unregistered media: '+name);
let bytes;
if(process.env.MEDIA_SOURCE_ROOT)bytes=await readFile(path.join(process.env.MEDIA_SOURCE_ROOT,name));
else{
const base=process.env.MEDIA_BASE_URL||process.env.WIKI_BASE||'http://8.211.154.134:18131';
const headers=process.env.PROMPT_TOKEN?{Authorization:'Bearer '+process.env.PROMPT_TOKEN}:{};
const response=await fetch(new URL('/api/media/'+asset.id,base),{headers,redirect:'error',signal:AbortSignal.timeout(60000)});
if(!response.ok)throw Error(`Media ${name}: HTTP ${response.status}; supply MEDIA_SOURCE_ROOT or authenticated MEDIA_BASE_URL`);
bytes=Buffer.from(await response.arrayBuffer());
}
if(bytes.length!==asset.bytes||createHash('sha256').update(bytes).digest('hex')!==asset.sha256)throw Error('Media checksum mismatch: '+name);
return bytes;
}
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){
const destination=process.argv[2];if(!destination)throw Error('Usage: node scripts/managed-media.mjs /absolute/external-cache');
const target=path.resolve(destination);if(target===repositoryRoot||target.startsWith(repositoryRoot+path.sep))throw Error('Cache must be outside repository');
for(const name of Object.keys(managedMedia)){const bytes=await readRepositoryMedia(name);const file=path.join(target,name);await mkdir(path.dirname(file),{recursive:true});await writeFile(file,bytes);}
console.log(`Restored ${Object.keys(managedMedia).length} verified media paths to external cache`);
}
+2 -1
View File
@@ -1,3 +1,4 @@
import {readRepositoryMedia} from './managed-media.mjs';
/** Local authoring client. Publishes text/images exclusively through existing generic Wiki APIs. */
import {readFile,writeFile,mkdir} from 'node:fs/promises';
import {createHash} from 'node:crypto';
@@ -21,7 +22,7 @@ const assetNames=new Set(['router-cover.png','source.png','numbered-source.png']
for(const l of raw){for(const field of ['reference','annotated','background'])assetNames.add(l[field].slice('asset:'.length));for(const kind of ['text','image','decoration'])assetNames.add(`${l.id}/${kind}-map.png`);for(const e of l.elements)for(const field of ['asset','mask','underlay'])if(e[field])assetNames.add(e[field].slice('asset:'.length))}
const byFile={},pending=[];
for(const name of [...assetNames].sort()){
const bytes=await readFile(path.join(dataRoot,'assets',name)),hash=createHash('sha256').update(bytes).digest('hex');byFile[name]={bytes,hash};
const bytes=await readRepositoryMedia('content/layout-reference/assets/'+name),hash=createHash('sha256').update(bytes).digest('hex');byFile[name]={bytes,hash};
if(!state.assets[hash]&&!pending.some(a=>a.hash===hash))pending.push({name,bytes,hash});
}
if(!dryRun){
+2 -1
View File
@@ -6,7 +6,8 @@ import {createHash} from 'node:crypto';
import annotations from '../contentasset:annotations.json';
import {textMergeTarget,unionBoxes} from '../contentasset:group-text';
import ocr from '../contentasset:ocr-evidence.json';
const root=path.resolve(import.meta.dirname,'..'),target=path.join(root,'contentasset:assets');
const root=path.resolve(import.meta.dirname,'..'),target=path.resolve(process.env.MEDIA_AUTHORING_ROOT||'/tmp/prompt-media-authoring');
if(target===root||target.startsWith(root+path.sep))throw Error('MEDIA_AUTHORING_ROOT must be outside repository');
type Box=[number,number,number,number];
type Element={id:string;kind:'text'|'image'|'decoration';role:string;box:Box;text:string|null;direction:string;reading:'verified'|'unreadable'|'not-text';asset:string;mask?:string;underlay?:string;pixels:number};
function components(mask:Uint8Array,w:number,h:number){