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
@@ -77,8 +77,14 @@ export const DefaultViewer = React.forwardRef<MediaController, FileViewerProps>(
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
{allowDownload && (
<button
data-testid="default-viewer-download-button"
onClick={handleDownload}
disabled={!file.media?.original?.key}
disabled={
!file.media?.original?.key ||
file.status === 'archived' ||
file.status === 'archiving' ||
file.status === 'restoring'
}
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
title="Download original file"
>
@@ -213,7 +213,12 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
onZoomOut={() => handleZoom(0.8)}
onFit={handleFit}
onDownload={handleDownload}
canDownload={!!file.media?.original?.key}
canDownload={
!!file.media?.original?.key &&
file.status !== 'archived' &&
file.status !== 'archiving' &&
file.status !== 'restoring'
}
onCopy={handleCopy}
copied={copied}
canCopy={!!bestUrl}
@@ -514,7 +514,14 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
zoom={zoom}
isFullScreen={isFullScreen}
allowDownload={allowDownload}
onDownload={data.media?.original?.key ? handleDownload : undefined}
onDownload={
data.media?.original?.key &&
data.status !== 'archived' &&
data.status !== 'archiving' &&
data.status !== 'restoring'
? handleDownload
: undefined
}
togglePlay={togglePlay}
onSeekDegree={seekToDegree}
onZoomChange={handleZoomChange}
@@ -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>
@@ -425,9 +425,20 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
(isAudio ? (
<button
onClick={() => handleDownload(data.media?.original?.key || '')}
className="transition-colors hover:text-primary"
title={m.download()}
disabled={!data.media?.original?.key}
className="transition-colors hover:text-primary disabled:opacity-50"
title={
data.status === 'archived' ||
data.status === 'archiving' ||
data.status === 'restoring'
? m.original_archived_tooltip()
: m.download()
}
disabled={
!data.media?.original?.key ||
data.status === 'archived' ||
data.status === 'archiving' ||
data.status === 'restoring'
}
>
<Download className="h-5 w-5" />
</button>
@@ -468,7 +479,24 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleDownload(data.media?.original?.key ?? '')}
disabled={
data.status === 'archived' ||
data.status === 'archiving' ||
data.status === 'restoring'
}
title={
data.status === 'archived' ||
data.status === 'archiving' ||
data.status === 'restoring'
? m.original_archived_tooltip()
: undefined
}
onClick={() =>
data.status !== 'archived' &&
data.status !== 'archiving' &&
data.status !== 'restoring' &&
handleDownload(data.media?.original?.key ?? '')
}
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground"
>
<span>{m.original()}</span>