feat(storage): add archive storage support for assets

This commit is contained in:
yiling
2026-11-02 13:19:10 +08:00
parent 49a2d7c380
commit 9097ec12bb
100 changed files with 6180 additions and 170 deletions
@@ -244,6 +244,60 @@ describe('AssetReadService', () => {
expect(s3Service.getObject).not.toHaveBeenCalled()
expect(outcome.status === 'ready' && outcome.result.text?.content).toBe('')
})
it('rejects reading raw text or native PDF pages when the original is archived, while allowing proxy.pdf', async () => {
const archivedText = await createFile('notes.md', {
mediaType: 'text/markdown',
key: 'raw/notes.md',
status: 'archived',
})
await expect(
assetReadService.read(
user(owner),
{ assetId: archivedText.id, docConfig: { mode: 'text' } },
now,
),
).rejects.toMatchObject({
status: 400,
message: expect.stringContaining('is archived in cold storage'),
})
const archivedNativePdf = await createFile('report.pdf', {
mediaType: 'application/pdf',
key: 'raw/report.pdf',
status: 'archived',
media: {
proxyType: 'pdf',
pdfTranscode: { key: 'raw/report.pdf', numPages: 3 },
} as unknown as PrismaJson.MediaInfo,
})
await expect(
assetReadService.read(
user(owner),
{ assetId: archivedNativePdf.id, docConfig: { mode: 'pages', startPage: 1, endPage: 1 } },
now,
),
).rejects.toMatchObject({
status: 400,
message: expect.stringContaining('is archived in cold storage'),
})
const archivedOfficeDoc = await createFile('deck.pptx', {
mediaType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
key: 'raw/deck.pptx',
status: 'archived',
media: {
proxyType: 'pdf',
pdfTranscode: { key: 'transcoded/deck/proxy.pdf', numPages: 3 },
} as unknown as PrismaJson.MediaInfo,
})
const officeOutcome = await assetReadService.read(
user(owner),
{ assetId: archivedOfficeDoc.id, docConfig: { mode: 'pages', startPage: 1, endPage: 1 } },
now,
)
expect(officeOutcome.status).toBe('pending')
})
})
describe('render tasks', () => {
@@ -55,6 +55,11 @@ function toReadable(asset: AssetRecord): ReadableAsset {
filenameLower: asset.name?.toLowerCase() || '',
storageKey: asset.storageKey?.key ?? null,
media: asset.media as PrismaJson.MediaInfo | null,
isArchived:
asset.status === 'archiving' ||
asset.status === 'archived' ||
asset.status === 'restoring' ||
Boolean(asset.storageKey?.isArchived),
}
}
@@ -107,9 +107,23 @@ export const documentReader: AssetReader<'document'> = {
`Asset "${asset.name}" is a binary PDF/document and cannot be read as raw text. Please call read_asset with docConfig: { mode: "pages", startPage: 1, endPage: ... } to view its visual pages.`,
)
}
if (asset.isArchived) {
throw badRequest(
`Asset "${asset.name}" is archived in cold storage and cannot be read as raw text. Restore the asset first, or use docConfig: { mode: "pages", startPage: 1, endPage: ... } if it has a generated PDF proxy.`,
)
}
return
}
if (
asset.isArchived &&
(!asset.media?.pdfTranscode?.key || asset.media.pdfTranscode.key === asset.storageKey)
) {
throw badRequest(
`Asset "${asset.name}" is a native PDF whose original file is archived in cold storage. Restore the asset first to render its pages.`,
)
}
if (startPage === null || endPage === null) {
throw badRequest('startPage and endPage are required when docConfig mode is "pages".')
}
@@ -1,6 +1,6 @@
import { WorkflowTaskType } from '@shumai/db'
import { getFileMimeType } from '@shumai/core/src/utils/file-mime'
import type { AssetReader } from '../types'
import { badRequest, type AssetReader } from '../types'
/** Images (including PSD): the web proxy, or a copy with the annotation drawn on top. */
export const imageReader: AssetReader<'image'> = {
@@ -16,7 +16,13 @@ export const imageReader: AssetReader<'image'> = {
validate() {},
async plan(asset, _options, { annotation }) {
const key = asset.media?.imageTranscodes?.[0]?.key || asset.storageKey
const proxyKey = asset.media?.imageTranscodes?.[0]?.key
if (!proxyKey && asset.isArchived) {
throw badRequest(
`Asset "${asset.name}" is archived in cold storage and has no web image proxy. Restore the asset first.`,
)
}
const key = proxyKey || asset.storageKey
if (!key) {
throw new Error('No media content found for this asset.')
}
+2
View File
@@ -15,6 +15,8 @@ export interface ReadableAsset {
filenameLower: string
storageKey: string | null
media: PrismaJson.MediaInfo | null
/** True when the original raw file is archived, archiving, or restoring. */
isArchived?: boolean
}
export interface ReadAnnotation {