mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 00:19:53 +00:00
feat(file-browser): show video duration on file card preview
Display the video duration (mm:ss) in the bottom-right corner of the video file card preview area with white text and a transparent background. Duration is sourced from media metadata and exposed via the preview DTO. The overlay is gated behind a showDuration prop so it only appears on file cards, not folder card child previews.
This commit is contained in:
@@ -1280,6 +1280,61 @@ describe('AssetService', () => {
|
||||
expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true)
|
||||
})
|
||||
|
||||
describe('Preview', () => {
|
||||
it('toPreviewInfo includes video duration from media metadata', async () => {
|
||||
const { project, user } = await setupBasicAssets()
|
||||
|
||||
const video = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'clip.mp4',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sizeByte: 123456,
|
||||
mediaType: 'video/mp4',
|
||||
status: 'processed',
|
||||
media: {
|
||||
poster: { key: 'poster-key' },
|
||||
metadata: { duration: 262 },
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any,
|
||||
},
|
||||
})
|
||||
|
||||
const info = await assetService.getAsset({ assetId: video.id })
|
||||
|
||||
expect(info.preview).toBeDefined()
|
||||
expect(info.preview?.mediaType).toBe('video/mp4')
|
||||
expect(info.preview?.duration).toBe(262)
|
||||
})
|
||||
|
||||
it('toPreviewInfo omits duration for images', async () => {
|
||||
const { project, user } = await setupBasicAssets()
|
||||
|
||||
const image = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'pic.png',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sizeByte: 5000,
|
||||
mediaType: 'image/png',
|
||||
status: 'processed',
|
||||
media: {
|
||||
thumbnail: { key: 'thumb-key' },
|
||||
metadata: { originalWidth: 100, originalHeight: 80 },
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any,
|
||||
},
|
||||
})
|
||||
|
||||
const info = await assetService.getAsset({ assetId: image.id })
|
||||
|
||||
expect(info.preview).toBeDefined()
|
||||
expect(info.preview?.duration).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Symlinks', () => {
|
||||
it('toAssetInfo resolves symlink target metadata', async () => {
|
||||
const { project, user } = await setupBasicAssets()
|
||||
|
||||
@@ -1913,6 +1913,7 @@ export class AssetService {
|
||||
originalHeight: asset.media.metadata?.originalHeight,
|
||||
originalWidth: asset.media.metadata?.originalWidth,
|
||||
spriteUrl,
|
||||
duration: asset.media.metadata?.duration,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ export const previewInfoSchema = z.object({
|
||||
originalHeight: z.number().optional(),
|
||||
originalWidth: z.number().optional(),
|
||||
spriteUrl: z.string().optional(),
|
||||
duration: z.number().optional(),
|
||||
})
|
||||
export type PreviewInfo = z.infer<typeof previewInfoSchema>
|
||||
|
||||
|
||||
@@ -276,7 +276,7 @@ export function FileCard({
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<FilePreview item={displayItem} />
|
||||
<FilePreview item={displayItem} showDuration />
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { File, Folder } from 'lucide-react'
|
||||
import { SpriteScrubber } from '../sprite-scrubber'
|
||||
import { formatTime } from '../viewers/utils'
|
||||
|
||||
type FilePreviewItem = {
|
||||
type?: string | null
|
||||
@@ -9,39 +10,56 @@ type FilePreviewItem = {
|
||||
thumbnailUrl?: string
|
||||
originalWidth?: number
|
||||
originalHeight?: number
|
||||
duration?: number
|
||||
} | null
|
||||
}
|
||||
|
||||
interface FilePreviewProps {
|
||||
item: FilePreviewItem
|
||||
showDuration?: boolean
|
||||
}
|
||||
|
||||
export const FilePreview = ({ item }: FilePreviewProps) => {
|
||||
export const FilePreview = ({ item, showDuration = false }: FilePreviewProps) => {
|
||||
const isVideo = item.preview?.mediaType?.startsWith('video/')
|
||||
const duration = item.preview?.duration
|
||||
const durationOverlay =
|
||||
showDuration && isVideo && typeof duration === 'number' && duration > 0 ? (
|
||||
<span className="pointer-events-none absolute bottom-1 right-1 text-xs font-medium tabular-nums text-white">
|
||||
{formatTime(duration)}
|
||||
</span>
|
||||
) : null
|
||||
|
||||
const isVideoWithSprite =
|
||||
item.preview?.mediaType?.startsWith('video/') &&
|
||||
item.preview.spriteUrl &&
|
||||
isVideo &&
|
||||
item.preview?.spriteUrl &&
|
||||
item.preview.thumbnailUrl &&
|
||||
item.preview.originalWidth &&
|
||||
item.preview.originalHeight
|
||||
|
||||
if (isVideoWithSprite && item.preview) {
|
||||
return (
|
||||
<SpriteScrubber
|
||||
spriteUrl={item.preview.spriteUrl!}
|
||||
thumbnailUrl={item.preview.thumbnailUrl!}
|
||||
videoWidth={item.preview.originalWidth!}
|
||||
videoHeight={item.preview.originalHeight!}
|
||||
/>
|
||||
<>
|
||||
<SpriteScrubber
|
||||
spriteUrl={item.preview.spriteUrl!}
|
||||
thumbnailUrl={item.preview.thumbnailUrl!}
|
||||
videoWidth={item.preview.originalWidth!}
|
||||
videoHeight={item.preview.originalHeight!}
|
||||
/>
|
||||
{durationOverlay}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (item.preview?.thumbnailUrl) {
|
||||
return (
|
||||
<img
|
||||
src={item.preview.thumbnailUrl}
|
||||
alt="Preview"
|
||||
className="w-full h-full object-contain bg-black"
|
||||
/>
|
||||
<>
|
||||
<img
|
||||
src={item.preview.thumbnailUrl}
|
||||
alt="Preview"
|
||||
className="w-full h-full object-contain bg-black"
|
||||
/>
|
||||
{durationOverlay}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user