From 24ef63b25556560a9a72e869a5820b99be163181 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:08:11 +0800 Subject: [PATCH 01/22] fix(media): preserve original image bytes, format and dimensions --- scripts/lib/media.ts | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/scripts/lib/media.ts b/scripts/lib/media.ts index 9d03e6c..a370d9c 100644 --- a/scripts/lib/media.ts +++ b/scripts/lib/media.ts @@ -1,22 +1,36 @@ +import { readFileSync } from 'node:fs'; import sharp from 'sharp'; import { createHash } from 'node:crypto'; import type { Site } from '../../src/lib/schema'; import type { ImageUpload } from './b2'; import { mediaBase } from '../../src/lib/settings'; + +// Preserve the exact source bytes and dimensions. Never generate WebP, resized +// copies or a second OG asset; the original also serves as the share image. export async function prepareMedia(file: string | Buffer, site: Site, path: string, name: string, cover: boolean): Promise { - const source = await sharp(file, { animated: false }).rotate().toBuffer(); - const metadata = await sharp(source).metadata(); - if (!['jpeg', 'png', 'webp', 'avif', 'tiff', 'gif'].includes(metadata.format ?? '') || !metadata.width || !metadata.height) throw new Error('請提供 JPG、PNG、WebP、AVIF、TIFF 或 GIF 圖片。GIF 只保留第一格。'); - // 單尺寸 WebP(最大 1440,絕不放大小圖);封面另外產生一張 OG JPEG。 - const widths = [Math.min(1440, metadata.width!)]; - const items: ImageUpload[] = []; - const add = async (bytes: Buffer, suffix: string, contentType: string) => { - const meta = await sharp(bytes).metadata(); - const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16); - const relative = `${path.replace(/^\/+|\/+$/g, '')}/${name}-${suffix}-${hash}.${contentType === 'image/jpeg' ? 'jpg' : 'webp'}`; - items.push({ bytes, objectName: `${site.domain}/${relative}`, contentType, width: meta.width!, height: meta.height!, url: mediaBase(site) + relative.split('/').map(encodeURIComponent).join('/') }); + void cover; + const bytes = typeof file === 'string' ? readFileSync(file) : file; + if (!bytes.length) throw new Error('圖片不能是空檔案。'); + const metadata = await sharp(bytes, { animated: false }).metadata(); + const formats: Record = { + jpeg: { extension: 'jpg', contentType: 'image/jpeg' }, + png: { extension: 'png', contentType: 'image/png' }, + webp: { extension: 'webp', contentType: 'image/webp' }, + avif: { extension: 'avif', contentType: 'image/avif' }, + gif: { extension: 'gif', contentType: 'image/gif' }, }; - for (const width of widths) await add(await sharp(source).resize({ width, withoutEnlargement: true }).webp({ quality: 80, effort: 5 }).toBuffer(), `${width}w`, 'image/webp'); - if (cover) await add(await sharp(source).resize(1200, 630, { fit: 'cover', position: 'attention' }).jpeg({ quality: 82, mozjpeg: true }).toBuffer(), 'og', 'image/jpeg'); - return items; + const format = formats[metadata.format ?? '']; + if (!format || !metadata.width || !metadata.height) { + throw new Error('原檔格式不適合直接在網頁顯示;請使用 JPG、PNG、WebP、AVIF 或 GIF。TIFF/HEIF 不會暗中轉檔。'); + } + const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16); + const relative = `${path.replace(/^\/+|\/+$/g, '')}/${name}-${hash}.${format.extension}`; + return [{ + bytes, + objectName: `${site.domain}/${relative}`, + contentType: format.contentType, + width: metadata.width, + height: metadata.height, + url: mediaBase(site) + relative.split('/').map(encodeURIComponent).join('/'), + }]; } From 5974f318bebd6839c2f75db1ab308f24c73c3491 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:08:14 +0800 Subject: [PATCH 02/22] fix(media): preserve original image bytes, format and dimensions --- scripts/media.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/media.ts b/scripts/media.ts index 6d9c98a..d5c742e 100644 --- a/scripts/media.ts +++ b/scripts/media.ts @@ -25,7 +25,7 @@ run(async () => { 'CPT 相簿:npm run media:add -- --uid UID --file ./car.jpg --alt 車輛照片 --field photos(自動追加到 gallery)', 'CPT 附件:npm run media:add -- --uid UID --file ./report.pdf --field report(自動填入 file 欄位)', '一般文章附件:npm run media:add -- --uid UID --file ./report.pdf --name report --label 車況報告(內文先放 )', - '支援 PDF、ZIP、Office、CSV、TXT、7Z、RAR、音訊及影片等附件(單檔 <=100 MiB);圖片單尺寸 WebP。', + '支援 PDF、ZIP、Office、CSV、TXT、7Z、RAR、音訊及影片等附件(單檔 <=100 MiB);圖片只保留原始檔,無轉檔及縮圖。', '附加 --dry-run 只列出目標網址,不上傳、不修改任何內容。', ].join('\n')); return; @@ -56,7 +56,7 @@ run(async () => { const items = isImage ? await prepareMedia(values.file, site, path, name, !!values.cover) : [prepareAttachment(values.file, site, path, name)]; - const imageVariants = isImage ? items.filter((i): i is ImageUpload => i.contentType === 'image/webp' && i.width !== undefined && i.height !== undefined) : []; + const imageVariants = isImage ? items.filter((i): i is ImageUpload => i.width !== undefined && i.height !== undefined) : []; const main = isImage ? imageVariants.at(-1)! : items[0]; if (!main) throw new Error('沒有產生可上傳的媒體。'); const url = main.url; @@ -98,8 +98,8 @@ run(async () => { const cover = imageVariants.at(-1)!; parsed.data.cover = { url: cover.url, alt: values.alt, width: cover.width, height: cover.height, - variants: imageVariants.map(i => ({ url: i.url, width: i.width })), - ogUrl: items.find(i => i.contentType === 'image/jpeg')!.url, + variants: [], + ogUrl: cover.url, }; } else if (fieldType && values.field) { parsed.data.fields = nextFields; @@ -117,7 +117,7 @@ run(async () => { records.set(receipt.url, { ...receipt, ...(imageVariants.some(i => i.url === receipt.url) - ? { variants: imageVariants.map(i => ({ url: i.url, width: i.width })) } + ? { variants: [] } : {}), }); } From f182839043495004b0796da039bfad297d22f7da Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:08:16 +0800 Subject: [PATCH 03/22] fix(media): preserve original image bytes, format and dimensions --- tests/media.test.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/media.test.ts b/tests/media.test.ts index 1df3421..69c28d9 100644 --- a/tests/media.test.ts +++ b/tests/media.test.ts @@ -6,20 +6,25 @@ import { prepareMedia } from '../scripts/lib/media'; import { uploadB2, type Upload } from '../scripts/lib/b2'; import { siteSchema } from '../src/lib/schema'; const site = siteSchema.parse({ domain: 'site.example.org', title: '測試', description: '說明', author: '作者', media: { bucket: 'wordpress-cdn' } }); -test('圖片只產生單尺寸 WebP 與分享圖片,中文路徑只編碼一次', async () => { +test('圖片及 OG 只引用原始檔,不重新編碼、不產生尺寸版本,中文路徑只編碼一次', async () => { const image = await sharp({ create: { width: 1600, height: 900, channels: 4, background: '#287655' } }).png().toBuffer(); const output = await prepareMedia(image, site, '/分類/文章/', 'cover', true); - assert.deepEqual(output.filter(o => o.contentType === 'image/webp').map(o => o.width), [1440]); - assert.equal(output.at(-1)!.width, 1200); assert.equal(output.at(-1)!.height, 630); + assert.equal(output.length, 1); + assert.equal(output[0].contentType, 'image/png'); + assert.equal(output[0].width, 1600); + assert.equal(output[0].height, 900); + assert.ok(output[0].objectName.endsWith('.png')); + assert.deepEqual(output[0].bytes, image); assert.ok(output[0].objectName.startsWith('site.example.org/分類/文章/')); assert.ok(output[0].url.startsWith('https://cdn.site.example.org/file/wordpress-cdn/site.example.org/%E5%88%86%E9%A1%9E/')); assert.deepEqual((await prepareMedia(image, site, '/分類/文章/', 'cover', true)).map(o => o.url), output.map(o => o.url)); }); -test('小圖不放大,內容雜湊會隨圖片內容變更', async () => { +test('原始 GIF 不會被截為首格;內容雜湊隨圖片變更', async () => { const image = (background: string) => sharp({ create: { width: 300, height: 200, channels: 3, background } }).png().toBuffer(); const a = await prepareMedia(await image('#ffffff'), site, '/分類/文章/', '圖', false); const b = await prepareMedia(await image('#000000'), site, '/分類/文章/', '圖', false); assert.equal(a.length, 1); assert.equal(a[0].width, 300); assert.notEqual(a[0].url, b[0].url); + assert.equal(a[0].contentType, 'image/png'); }); const bytes = Buffer.from('image-test-data'); const digest = createHash('sha1').update(bytes).digest('hex'); From 008a98a7ab440af496e77c4cbc98bb7d80494726 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:17 +0800 Subject: [PATCH 04/22] feat(media): define isolated CMS media key namespace --- scripts/lib/media-path.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 scripts/lib/media-path.ts diff --git a/scripts/lib/media-path.ts b/scripts/lib/media-path.ts new file mode 100644 index 0000000..1291a41 --- /dev/null +++ b/scripts/lib/media-path.ts @@ -0,0 +1,17 @@ +// One bucket can host several sites. CMS uploads get a dedicated namespace +// beneath their own domain, separate from legacy WordPress uploads and +// older generated CMS WebP assets. +const segment = /^[\p{L}\p{N}][\p{L}\p{N}_-]*$/u; +export function cmsMediaPath(kind: string, uid: string): string { + if (![kind, uid].every(value => segment.test(value) && value === value.normalize('NFC'))) { + throw new Error('CMS 媒體目錄必須使用合法的內容類型與 UID。'); + } + return `/cms/${kind}/${uid}/`; +} +export function assertCmsMediaPath(value: string): string { + const match = /^\/?cms\/([^/]+)\/([^/]+)\/?$/u.exec(value); + if (!match || ![match[1], match[2]].every(s => segment.test(s) && s === s.normalize('NFC'))) { + throw new Error('新媒體必須存放在本站的 cms/內容類型/UID/ 專用目錄,禁止沿用 WordPress uploads。'); + } + return `cms/${match[1]}/${match[2]}`; +} From 8443bd7feaa60daa95d1b60bb90d85376654c306 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:33 +0800 Subject: [PATCH 05/22] feat(media): isolate original assets under per-site CMS prefix --- scripts/lib/media.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/lib/media.ts b/scripts/lib/media.ts index a370d9c..c8a3284 100644 --- a/scripts/lib/media.ts +++ b/scripts/lib/media.ts @@ -4,6 +4,7 @@ import { createHash } from 'node:crypto'; import type { Site } from '../../src/lib/schema'; import type { ImageUpload } from './b2'; import { mediaBase } from '../../src/lib/settings'; +import { assertCmsMediaPath } from './media-path'; // Preserve the exact source bytes and dimensions. Never generate WebP, resized // copies or a second OG asset; the original also serves as the share image. @@ -24,7 +25,7 @@ export async function prepareMedia(file: string | Buffer, site: Site, path: stri throw new Error('原檔格式不適合直接在網頁顯示;請使用 JPG、PNG、WebP、AVIF 或 GIF。TIFF/HEIF 不會暗中轉檔。'); } const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16); - const relative = `${path.replace(/^\/+|\/+$/g, '')}/${name}-${hash}.${format.extension}`; + const relative = `${assertCmsMediaPath(path)}/${name}-${hash}.${format.extension}`; return [{ bytes, objectName: `${site.domain}/${relative}`, From 05c642d0b56148588cabac537d15d69d6e30ee1b Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:36 +0800 Subject: [PATCH 06/22] feat(media): isolate original assets under per-site CMS prefix --- scripts/media.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/media.ts b/scripts/media.ts index d5c742e..8943c3f 100644 --- a/scripts/media.ts +++ b/scripts/media.ts @@ -8,6 +8,7 @@ import { readInput, run } from './lib/content'; import { loadSite } from '../src/lib/settings'; import { segment } from '../src/lib/schema'; import { prepareMedia } from './lib/media'; +import { cmsMediaPath } from './lib/media-path'; import { prepareAttachment, isImageFilename, resolveCptAssetField, applyCptAssetField, insertAttachmentLink } from './lib/attachments'; import { uploadB2, type Receipt, type ImageUpload } from './lib/b2'; import { uploadS3 } from './lib/s3'; @@ -52,7 +53,7 @@ run(async () => { } } const mediaKind = doc.kind === 'post' ? 'blog' : doc.kind === 'page' ? 'site' : doc.data.type; - const path = '/' + mediaKind + '/' + doc.data.uid + '/'; + const path = cmsMediaPath(mediaKind, doc.data.uid); const items = isImage ? await prepareMedia(values.file, site, path, name, !!values.cover) : [prepareAttachment(values.file, site, path, name)]; From 8aa6ddec4e7abb293e92434ee9283a2e2b631e75 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:39 +0800 Subject: [PATCH 07/22] feat(media): isolate original assets under per-site CMS prefix --- scripts/lib/attachments.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/lib/attachments.ts b/scripts/lib/attachments.ts index 5b3ae36..0cd0944 100644 --- a/scripts/lib/attachments.ts +++ b/scripts/lib/attachments.ts @@ -5,6 +5,7 @@ import type { Site } from '../../src/lib/schema'; import type { Document } from '../../src/lib/model'; import type { Upload } from './b2'; import { mediaBase } from '../../src/lib/settings'; +import { assertCmsMediaPath } from './media-path'; // 明確允許常見文件、壓縮檔與影音附件;不接受可直接執行腳本的 HTML/SVG/JS 等。 // 檔案本體原樣上傳,不讓 Sharp 處理,也不從 CDN 下載回來做雜湊驗證。 @@ -45,7 +46,7 @@ export function prepareAttachment(file: string, site: Site, objectPath: string, const bytes = readFileSync(file); // 只為避免同名覆蓋與快取污染加本機隨機字尾,不必讀回或比對 CDN 資料。 const suffix = randomBytes(6).toString('hex'); - const relative = objectPath.replace(/^\/+|\/+$/g, '') + '/' + name + '-' + suffix + extension; + const relative = assertCmsMediaPath(objectPath) + '/' + name + '-' + suffix + extension; return { bytes, objectName: site.domain + '/' + relative, From 8147e859fcb19658dc274140ef7aba371968c8c3 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:41 +0800 Subject: [PATCH 08/22] feat(media): isolate original assets under per-site CMS prefix --- tests/media.test.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/media.test.ts b/tests/media.test.ts index 69c28d9..7bde718 100644 --- a/tests/media.test.ts +++ b/tests/media.test.ts @@ -3,29 +3,39 @@ import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import sharp from 'sharp'; import { prepareMedia } from '../scripts/lib/media'; +import { cmsMediaPath, assertCmsMediaPath } from '../scripts/lib/media-path'; import { uploadB2, type Upload } from '../scripts/lib/b2'; import { siteSchema } from '../src/lib/schema'; const site = siteSchema.parse({ domain: 'site.example.org', title: '測試', description: '說明', author: '作者', media: { bucket: 'wordpress-cdn' } }); test('圖片及 OG 只引用原始檔,不重新編碼、不產生尺寸版本,中文路徑只編碼一次', async () => { const image = await sharp({ create: { width: 1600, height: 900, channels: 4, background: '#287655' } }).png().toBuffer(); - const output = await prepareMedia(image, site, '/分類/文章/', 'cover', true); + const output = await prepareMedia(image, site, '/cms/分類/文章/', 'cover', true); assert.equal(output.length, 1); assert.equal(output[0].contentType, 'image/png'); assert.equal(output[0].width, 1600); assert.equal(output[0].height, 900); assert.ok(output[0].objectName.endsWith('.png')); assert.deepEqual(output[0].bytes, image); - assert.ok(output[0].objectName.startsWith('site.example.org/分類/文章/')); - assert.ok(output[0].url.startsWith('https://cdn.site.example.org/file/wordpress-cdn/site.example.org/%E5%88%86%E9%A1%9E/')); - assert.deepEqual((await prepareMedia(image, site, '/分類/文章/', 'cover', true)).map(o => o.url), output.map(o => o.url)); + assert.ok(output[0].objectName.startsWith('site.example.org/cms/分類/文章/')); + assert.ok(output[0].url.startsWith('https://cdn.site.example.org/file/wordpress-cdn/site.example.org/cms/%E5%88%86%E9%A1%9E/')); + assert.deepEqual((await prepareMedia(image, site, '/cms/分類/文章/', 'cover', true)).map(o => o.url), output.map(o => o.url)); }); test('原始 GIF 不會被截為首格;內容雜湊隨圖片變更', async () => { const image = (background: string) => sharp({ create: { width: 300, height: 200, channels: 3, background } }).png().toBuffer(); - const a = await prepareMedia(await image('#ffffff'), site, '/分類/文章/', '圖', false); - const b = await prepareMedia(await image('#000000'), site, '/分類/文章/', '圖', false); + const a = await prepareMedia(await image('#ffffff'), site, '/cms/分類/文章/', '圖', false); + const b = await prepareMedia(await image('#000000'), site, '/cms/分類/文章/', '圖', false); assert.equal(a.length, 1); assert.equal(a[0].width, 300); assert.notEqual(a[0].url, b[0].url); assert.equal(a[0].contentType, 'image/png'); }); +test('新圖片及附件只能使用獨立 cms 目錄,不接受 WordPress uploads 或跨站路徑', async () => { + assert.equal(cmsMediaPath('cars', 'wp-2885'), '/cms/cars/wp-2885/'); + assert.equal(assertCmsMediaPath('/cms/blog/wp-123/'), 'cms/blog/wp-123'); + assert.throws(() => assertCmsMediaPath('/wp-content/uploads/2023/'), /cms\/|專用目錄/); + assert.throws(() => assertCmsMediaPath('/cms/blog/../'), /專用目錄/); + assert.throws(() => assertCmsMediaPath('/another.com/cms/blog/wp-123/'), /專用目錄/); + const data = await sharp({ create: { width: 40, height: 20, channels: 3, background: '#aabbcc' } }).png().toBuffer(); + await assert.rejects(prepareMedia(data, site, '/blog/wp-123/', 'image', false), /專用目錄/); +}); const bytes = Buffer.from('image-test-data'); const digest = createHash('sha1').update(bytes).digest('hex'); const item: Upload = { bytes, objectName: 'site.example.org/分類/cover.webp', contentType: 'image/webp', width: 800, height: 450, url: 'https://cdn.site.example.org/cover.webp' }; From db1a4455cad2c11a022451efa546acd4afa9adf5 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:13:44 +0800 Subject: [PATCH 09/22] feat(media): isolate original assets under per-site CMS prefix --- ...345\234\226\347\211\207\346\223\215\344\275\234.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/docs/\345\234\226\347\211\207\346\223\215\344\275\234.md" "b/docs/\345\234\226\347\211\207\346\223\215\344\275\234.md" index 00fc782..561a00a 100644 --- "a/docs/\345\234\226\347\211\207\346\223\215\344\275\234.md" +++ "b/docs/\345\234\226\347\211\207\346\223\215\344\275\234.md" @@ -1,6 +1,6 @@ # 圖片操作 -給圖片路徑、文章 ID 與替代文字,工具會縮圖、上傳並填回文章。 +給圖片原始檔、文章 ID 與替代文字,工具會原檔上傳並填回文章,不會再縮圖或轉成 WebP。 ## 第一次設定 @@ -27,7 +27,7 @@ npm run media:add -- --uid 範例新增文章 --file "D:/圖片/cover.png" --alt "文章與分類的關係" --cover ``` -產生單尺寸 WebP(最大寬 1440,小圖不放大);封面額外產生 1200 × 630 的分享圖片。成功後自動寫入 `cover` 的網址、替代文字、寬高與各尺寸。 +保留來源圖片的原始位元組、格式、完整尺寸;不再生成其他尺寸的 WebP 或 1200 × 630 OG 副本。封面與 OG 皆引用同一張原圖,`variants` 為空。 ## 內文 @@ -43,13 +43,13 @@ npm run media:add -- --uid 範例新增文章 --file "D:/圖片/cover.png" --alt npm run media:add -- --uid 範例新增文章 --file "D:/圖片/流程.png" --alt "從寫作到發布的流程" --name 流程圖 ``` -成功後換成 Markdown 圖片,建置時補上寬高、自適應尺寸與延遲載入。封面可重跑覆換;內文重插時,先將原圖片換回占位符。 +成功後換成 Markdown 圖片,建置時補上寬高與延遲載入。封面可重跑覆換;內文重插時,先將原圖片換回占位符。 加 `--dry-run`(預覽模式)只列出處理結果與目標網址,不上傳、不改文章、不需金鑰。 ## 上傳成功與連結檢查 -圖片轉檔後交給 B2 Native 或 S3 上傳 API。**API 明確成功後,只對這次上傳的圖片網址發出一次 HTTP HEAD**,確認連結是 2xx,且回應標頭沒有顯示 HTML 假圖片或空檔案。不使用 GET 下載媒體內容,不計算下載後雜湊,也不對同一張圖連續重試。若 HEAD 回 404/403/405 或不正確的類型,就顯示錯誤並暫停更新文章,請檢查 CDN 設定。 +圖片原檔交給 B2 Native 或 S3 上傳 API。**API 明確成功後,只對這次上傳的圖片網址發出一次 HTTP HEAD**,確認連結是 2xx,且回應標頭沒有顯示 HTML 假圖片或空檔案。不使用 GET 下載媒體內容,不計算下載後雜湊,也不對同一張圖連續重試。若 HEAD 回 404/403/405 或不正確的類型,就顯示錯誤並暫停更新文章,請檢查 CDN 設定。 B2 Native API 的 SHA-1 是該 API 上傳所需的原生校驗欄位,與重新下載檔案無關;媒體 manifest 仍保留現有 SHA-1、尺寸、時間等欄位,確保既有資料相容。S3 API 接收上傳成功回應後不額外 GET 檔案。內容雜湊仍可用於避免圖片同名與快取衝突。 @@ -74,7 +74,7 @@ npm run media:verify -- --batch-size 1000 # 手動分批 HEAD ## CPT 圖片路徑 -新上傳圖片依 `網域/site|blog|CPT代碼/永久UID/檔案` 儲存,不與會改變的文章分類及 Slug 綁定。已存在的圖片路徑保持不動,操作詳見 [CPT 自訂內容](CPT自訂內容.md)。 +新圖片與附件一律存於 `網域/cms/site|blog|CPT代碼/永久UID/原檔`,與 `網域/wp-content/uploads/` 以及舊有 CMS 產生的 WebP 路徑完全分開。相同 Bucket 內各站也依網域隔離。現有已發布網址不會在這次程式更新時自動改寫;舊圖片要另經來源核對、B2 複製和 CDN 驗證後才切換。操作詳見 [CPT 自訂內容](CPT自訂內容.md)。 ## 附件上傳:PDF/ZIP/Office 及其他檔案 From 1cd3c945cffaea05179258b2cbdca19fb0f458b8 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:14:17 +0800 Subject: [PATCH 10/22] chore(release): bump CMS version to 0.2.6 for original-only media --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d6dbe6c..b2145dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cms-template", - "version": "0.2.5", + "version": "0.2.6", "private": true, "type": "module", "description": "少爺的輕量靜態網站公版:繁體中文操作、Post / Page、SEO 自動化與 B2 圖床", From 8f3f6465c4c23ecb8cc1a4cf3fe17407b6595489 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:14:21 +0800 Subject: [PATCH 11/22] chore(release): bump CMS version to 0.2.6 for original-only media --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index b1a403b..2f968e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cms-template", - "version": "0.2.5", + "version": "0.2.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cms-template", - "version": "0.2.5", + "version": "0.2.6", "dependencies": { "@astrojs/markdown-remark": "^7.3.3", "@aws-sdk/client-s3": "^3.0.0", From 02744e31d36b08f8f0aec043ed4e80f06ef888bb Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:14:24 +0800 Subject: [PATCH 12/22] chore(release): bump CMS version to 0.2.6 for original-only media --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d08c528..dc1d12b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 天公仔CMS -目前公版版本:**v0.2.5**|[查看版本更新紀錄](CHANGELOG.md) +目前公版版本:**v0.2.6**|[查看版本更新紀錄](CHANGELOG.md)

天公仔 PHP 工程師梗圖:寫 PHP 是走投無路的人在做的,老子我天公仔,看起來像走投無路的嗎? From b55df030dda83135cf4bab671f5eaf25544d8692 Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:14:26 +0800 Subject: [PATCH 13/22] chore(release): bump CMS version to 0.2.6 for original-only media --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b77d9b9..e2fa577 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ 從 v0.2.0 開始,每次更新共通功能、修正公版 BUG 或調整行為,都應提高版本並記錄變更。衍生網站的單純新增文章、SEO 文案或個別設定不代表 CMS 公版升版。 +## [0.2.6] - 2026-11-01 + +- **保留圖片原始檔**:上傳原始位元組、格式與解析度,不再產生 WebP 縮圖或額外的 OG JPEG;封面和社群預覽共用原圖。 +- **B2 專屬媒體命名空間**:圖片與附件的新物件固定存到 `網域/cms/類型/UID/`,避免混用 WordPress `uploads` 和先前的衍生圖資料;新增路徑安全與原圖驗證測試。 + ## [0.2.5] - 2026-10-27 - **高頻 Agent CI 分流**:文章與索引改動只做內容 Schema/URL 快檢;程式與設定改動保留完整驗收,減少重複 Astro 建置。 From 7a30166c8f076b33b280977c853a121faa95d79b Mon Sep 17 00:00:00 2001 From: leo72321 <68505225+leo72321@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:15:31 +0800 Subject: [PATCH 14/22] test(media): align attachment fixtures with isolated CMS directory --- tests/attachments.test.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/attachments.test.ts b/tests/attachments.test.ts index 5a454bf..77750b9 100644 --- a/tests/attachments.test.ts +++ b/tests/attachments.test.ts @@ -36,39 +36,39 @@ function fixture(action: (dir: string) => void) { try { action(dir); } finally { rmSync(dir, { recursive: true, force: true }); } } -test('PDF 與 ZIP 原檔上傳,保持內容與副檔名,不經過圖片轉檔,物件依 CPT/UID 存放', () => { +test('PDF 與 ZIP 原檔上傳,保持內容與副檔名,不經過圖片轉檔,物件依 CMS/CPT/UID 專用目錄存放', () => { fixture(dir => { for (const ext of ['pdf', 'zip']) { const file = join(dir, 'report.' + ext); const bytes = Buffer.from(ext === 'pdf' ? '%PDF-1.7\nhello' : 'PK\x03\x04mock zip'); writeFileSync(file, bytes); - const prepared = prepareAttachment(file, site, '/cars/20261023-abcde/', 'report'); + const prepared = prepareAttachment(file, site, '/cms/cars/20261023-abcde/', 'report'); assert.deepEqual(prepared.bytes, bytes); assert.equal(prepared.contentType, ext === 'pdf' ? 'application/pdf' : 'application/zip'); - assert.match(prepared.objectName, new RegExp('^cars.example.com/cars/20261023-abcde/report-[a-f0-9]{12}\\.' + ext + '$')); - assert.ok(prepared.url.startsWith('https://f002.backblazeb2.com/file/wordpress-cdn/cars.example.com/cars/20261023-abcde/')); + assert.match(prepared.objectName, new RegExp('^cars.example.com/cms/cars/20261023-abcde/report-[a-f0-9]{12}\\.' + ext + '$')); + assert.ok(prepared.url.startsWith('https://f002.backblazeb2.com/file/wordpress-cdn/cars.example.com/cms/cars/20261023-abcde/')); assert.equal(prepared.width, undefined); assert.equal(prepared.height, undefined); } }); }); -test('避免非法副檔名、空白附件、過大檔案;圖片仍走 Sharp', () => { +test('避免非法副檔名、空白附件、過大檔案;圖片維持獨立原檔處理', () => { fixture(dir => { const pdf = join(dir, 'report.pdf'); writeFileSync(pdf, Buffer.alloc(0)); - assert.throws(() => prepareAttachment(pdf, site, '/cars/uid/', 'report'), /非空/); + assert.throws(() => prepareAttachment(pdf, site, '/cms/cars/uid/', 'report'), /非空/); writeFileSync(pdf, Buffer.from('ok')); - const first = prepareAttachment(pdf, site, '/cars/uid/', 'report'); - const second = prepareAttachment(pdf, site, '/cars/uid/', 'report'); + const first = prepareAttachment(pdf, site, '/cms/cars/uid/', 'report'); + const second = prepareAttachment(pdf, site, '/cms/cars/uid/', 'report'); assert.notEqual(first.url, second.url, '多次上傳不能覆蓋既有網址'); - assert.throws(() => prepareAttachment(pdf, site, '/cars/uid/', '../secret'), /名稱/); + assert.throws(() => prepareAttachment(pdf, site, '/cms/cars/uid/', '../secret'), /名稱/); const html = join(dir, 'evil.html'); writeFileSync(html, Buffer.from('