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
@@ -0,0 +1,38 @@
// @vitest-environment happy-dom
import { cleanup, render, screen } from '@testing-library/react'
import React from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { PdfViewer } from './pdf-viewer'
import type { AssetInfo } from '@shumai/dtos'
vi.mock('pdfjs-dist', () => ({
GlobalWorkerOptions: { workerSrc: '' },
getDocument: vi.fn(),
}))
describe('PdfViewer', () => {
afterEach(() => {
cleanup()
})
it('renders archived placeholder when native PDF is archived and pdfTranscode.url is absent', () => {
const archivedPdf: AssetInfo = {
id: 'pdf-1',
name: 'document.pdf',
type: 'file',
status: 'archived',
sizeByte: 1024,
fileCount: 0,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
media: {
original: { key: 'files/pdf-1/document.pdf' },
pdfTranscode: { key: 'files/pdf-1/document.pdf' },
},
} as AssetInfo
render(<PdfViewer file={archivedPdf} />)
expect(screen.getByTestId('pdf-viewer-archived-state')).toBeTruthy()
})
})
@@ -1,6 +1,8 @@
import { client } from '@/ui/api/client'
import DrawingCanvas from '@/ui/components/drawing-canvas'
import { m } from '@/ui/paraglide/messages.js'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import { Archive } from 'lucide-react'
import * as pdfjsLib from 'pdfjs-dist'
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react'
import { FileViewerProps, MediaController } from '../types'
@@ -166,6 +168,9 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
}, [])
const fileUrl = file.media?.pdfTranscode?.url
const isOriginalUnavailable =
file.status === 'archived' || file.status === 'archiving' || file.status === 'restoring'
const isNativePdfArchived = !fileUrl && isOriginalUnavailable
// Load PDF Document
useEffect(() => {
@@ -409,6 +414,16 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
<div className="absolute inset-0 flex items-center justify-center bg-black/40 z-20">
<div className="w-8 h-8 border-4 border-white/30 border-t-white rounded-full animate-spin" />
</div>
) : isNativePdfArchived ? (
<div
data-testid="pdf-viewer-archived-state"
className="absolute inset-0 flex flex-col items-center justify-center gap-3 p-6 text-center text-muted-foreground"
>
<Archive className="h-12 w-12 text-muted-foreground/70" />
<p className="text-sm font-medium text-foreground">
{m.pdf_archived_placeholder()}
</p>
</div>
) : error ? (
<div className="absolute inset-0 flex items-center justify-center text-gray-500 text-sm">
{error}
@@ -465,7 +480,7 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
onZoomOut={() => handleZoom(0.8)}
onFit={handleFit}
onDownload={handleDownload}
canDownload={!!file.media?.original?.key}
canDownload={!!file.media?.original?.key && !isOriginalUnavailable}
allowDownload={allowDownload}
/>
</div>