refactor(storage): use original filenames and fix download UX

- Store uploaded files as files/{ulid}/{original-name} instead of files/{ulid}/raw
- Store transcoded videos as {stem}-{resolution}.mp4 (e.g. foo-720p.mp4)
- Store transcoded images as {stem}-{width}p.webp (e.g. foo-1920p.webp)
- Add download?: boolean param to presign - only sets Content-Disposition: attachment when true
- Local storage handler only sets Content-Disposition when ?download=1 query param is present
- Add POST /api/files/download-url endpoint for key-based single file downloads
  - Uses ResourceType.Asset for permission check
  - Validates key belongs to the asset's storage directory
- Fix existing download-links API to pass download: true to presign
- Fix breadcrumb navbar download: video shows resolution submenu, others download directly
- Fix video player download: calls download-url API, shows correct file type label
- Remove filename query param hack from presign URLs
- Add sanitizeFilename() and stemFromKey() utility helpers
- Add key field to VideoTranscode DTO interface
This commit is contained in:
yiling
2026-07-02 17:39:23 +08:00
parent 114cdca4ee
commit b7367bb129
22 changed files with 562 additions and 84 deletions
+97 -3
View File
@@ -1,8 +1,10 @@
import { Badge } from '@/ui/components/ui/badge'
import { client } from '@/ui/api/client'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuSub,
@@ -44,6 +46,15 @@ interface BreadcrumbNavProps {
shareId?: string
onFolderClick?: (folderId: string) => void
fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{
id: string
version: number
@@ -69,6 +80,8 @@ export function BreadcrumbNav({
isPublic = false,
onFolderClick,
fileId,
shareId,
downloadInfo,
versions,
}: BreadcrumbNavProps) {
const navigate = useNavigate()
@@ -83,6 +96,33 @@ export function BreadcrumbNav({
})
}
const handleDownload = async (key: string) => {
if (!fileId) return
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: fileId },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
const hasVideoTranscodes =
downloadInfo?.videoTranscodes && downloadInfo.videoTranscodes.filter((t) => !t.isRaw).length > 0
const breadcrumbs: { name: string; path?: string; id?: string; isMuted?: boolean }[] = isPublic
? [
{
@@ -155,9 +195,63 @@ export function BreadcrumbNav({
<ChevronDown className="h-4 w-4" />
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56">
<DropdownMenuItem onClick={() => console.log('Download')}>
Download
</DropdownMenuItem>
{hasVideoTranscodes ? (
<DropdownMenuSub>
<DropdownMenuSubTrigger className="flex items-center gap-2">
<span>{m.download()}</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className="w-48">
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes
?.filter((t) => !t.isRaw)
.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
return (
<DropdownMenuItem
key={resolution}
onClick={() => handleDownload(t.key)}
className="flex items-center justify-between"
>
<span>{resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span>
</DropdownMenuItem>
)
})}
{downloadInfo?.originalKey && (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleDownload(downloadInfo.originalKey!)}
className="flex items-center justify-between"
>
<span>Original</span>
<span className="text-xs text-muted-foreground">
{currentAsset.name?.split('.').pop()?.toUpperCase() || 'RAW'}
</span>
</DropdownMenuItem>
</>
)}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
) : (
<DropdownMenuItem
onClick={() =>
downloadInfo?.originalKey && handleDownload(downloadInfo.originalKey)
}
disabled={!downloadInfo?.originalKey}
>
{m.download()}
</DropdownMenuItem>
)}
{canEdit && (
<DropdownMenuItem onClick={() => console.log('Rename')}>
Rename
+29 -11
View File
@@ -1,4 +1,5 @@
import { useScreenSize } from '@/ui/hooks/useScreenSize'
import { client } from '@/ui/api/client'
import { getBestTranscode } from '@/ui/lib/media'
import type { AssetInfo } from '@shumai/dtos'
import { Check, Copy, Download, Minus, Plus } from 'lucide-react'
@@ -19,6 +20,7 @@ type FileViewerProps = {
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
startTime?: number
shareId?: string
children?: ReactNode
}
@@ -30,6 +32,7 @@ export function FileViewer({
onTimeUpdate,
annotations,
startTime,
shareId,
children,
}: FileViewerProps) {
const { width: screenWidth } = useScreenSize()
@@ -133,15 +136,29 @@ export function FileViewer({
}
}
const handleDownload = () => {
const url = file.media?.original?.downloadUrl
if (!url) return
const link = document.createElement('a')
link.href = url
link.download = file.name || 'image'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
const handleDownload = async () => {
const key = file.media?.original?.key
if (!key || !file.id) return
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId: file.id },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: file.id },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
const handleCopy = async () => {
@@ -210,7 +227,7 @@ export function FileViewer({
<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">
<button
onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl}
disabled={!file.media?.original?.key}
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"
>
@@ -291,7 +308,7 @@ export function FileViewer({
</button>
<button
onClick={handleDownload}
disabled={!file.media?.original?.downloadUrl}
disabled={!file.media?.original?.key}
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 image"
>
@@ -311,6 +328,7 @@ export function FileViewer({
onTimeUpdate={onTimeUpdate}
annotations={displayAnnotations}
startTime={startTime}
shareId={shareId}
>
{children}
</VideoPlayer>
@@ -350,6 +350,17 @@ export function PublicShareManager({
isPublic: true,
shareId,
fileId: viewingFileId || undefined,
downloadInfo: viewingFileData
? {
originalKey: viewingFileData.media?.original?.key,
videoTranscodes: viewingFileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
}
: undefined,
onFolderClick: handleBreadcrumbClick,
isRightSidebarCollapsed,
onRightSidebarToggle: () => setIsRightSidebarCollapsed((prev) => !prev),
@@ -458,6 +469,7 @@ export function PublicShareManager({
onTimeUpdate={setCurrentTime}
annotations={annotations}
startTime={startTime}
shareId={shareId}
/>
)}
</div>
+2
View File
@@ -19,6 +19,7 @@ export function TopNav() {
currentAsset,
isRootFolder,
fileId,
downloadInfo,
versions,
isPublic,
shareId,
@@ -87,6 +88,7 @@ export function TopNav() {
isPublic={isPublic}
shareId={shareId}
fileId={fileId}
downloadInfo={downloadInfo}
versions={versions}
onFolderClick={onFolderClick}
/>
@@ -1,4 +1,5 @@
import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
import { client } from '@/ui/api/client'
import { cn } from '@/ui/lib/utils'
import {
Check,
@@ -58,6 +59,7 @@ interface VideoPlayerProps {
onTimeUpdate?: (time: number) => void
annotations?: Annotation[]
startTime?: number
shareId?: string
children?: React.ReactNode
}
@@ -76,7 +78,7 @@ interface ControlBarProps {
handleVolumeChange: (newVolume: number) => void
changePlaybackRate: (rate: number) => void
changeResolution: (res: DisplayTranscode) => void
handleDownload: (url: string, resolution: string) => void
handleDownload: (key: string) => void
toggleFullScreen: () => void
onZoomChange: (zoom: number) => void
onZoomReset: () => void
@@ -344,11 +346,13 @@ const ControlBar: React.FC<ControlBarProps> = ({
{resolutions.map((res) => (
<DropdownMenuItem
key={res.resolution}
onClick={() => handleDownload(res.url ?? '', res.resolution)}
onClick={() => handleDownload(res.key ?? '')}
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground"
>
<span>{res.resolution}</span>
<span className="text-xs text-muted-foreground">MP4</span>
<span className="text-xs text-muted-foreground">
{res.isRaw ? data.name?.split('.').pop()?.toUpperCase() || 'RAW' : 'MP4'}
</span>
</DropdownMenuItem>
))}
</DropdownMenuContent>
@@ -376,6 +380,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
onTimeUpdate,
annotations,
startTime,
shareId,
children,
}) => {
const localPlayerRef = useRef<Player | null>(null)
@@ -456,12 +461,16 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
}
// Initial resolution setup
const originalRes = {
const originalRes: DisplayTranscode = {
id: 'original',
url: data.media.original.downloadUrl,
key: data.media.original.key ?? '',
width: data.media.metadata.originalWidth ?? 0,
height: data.media.metadata.originalHeight ?? 0,
size: 0,
isRaw: true,
resolution: 'Original',
} as DisplayTranscode
}
const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => {
const longSide = Math.max(t.width, t.height)
@@ -837,13 +846,27 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
}
}
const handleDownload = (url: string, resolution: string) => {
const link = document.createElement('a')
link.href = url
link.download = `${data.name}-${resolution}.mp4`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
const handleDownload = async (key: string) => {
try {
const res = shareId
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
param: { shareId, fileId: data.id! },
json: { key },
})
: await client.api.files['download-url'].$post({
json: { key, assetId: data.id! },
})
if (!res.ok) return
const { url } = await res.json()
const link = document.createElement('a')
link.href = url
link.download = ''
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} catch {
// silently fail
}
}
// Listen for fullscreen change events (ESC key)
@@ -148,6 +148,15 @@ function FileViewPage() {
},
isRootFolder: false,
fileId,
downloadInfo: {
originalKey: fileData.media?.original?.key,
videoTranscodes: fileData.media?.videoTranscodes?.map((t) => ({
key: t.key,
width: t.width,
height: t.height,
isRaw: t.isRaw,
})),
},
versions: versionsDataList,
onFolderClick: (id: string) => {
navigate({
+9
View File
@@ -13,6 +13,15 @@ export interface TopNavProjectState {
}
isRootFolder: boolean
fileId?: string
downloadInfo?: {
originalKey?: string
videoTranscodes?: Array<{
key: string
width: number
height: number
isRaw?: boolean
}>
}
versions?: Array<{
id: string
version: number