mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +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)
|
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', () => {
|
describe('Symlinks', () => {
|
||||||
it('toAssetInfo resolves symlink target metadata', async () => {
|
it('toAssetInfo resolves symlink target metadata', async () => {
|
||||||
const { project, user } = await setupBasicAssets()
|
const { project, user } = await setupBasicAssets()
|
||||||
|
|||||||
@@ -1913,6 +1913,7 @@ export class AssetService {
|
|||||||
originalHeight: asset.media.metadata?.originalHeight,
|
originalHeight: asset.media.metadata?.originalHeight,
|
||||||
originalWidth: asset.media.metadata?.originalWidth,
|
originalWidth: asset.media.metadata?.originalWidth,
|
||||||
spriteUrl,
|
spriteUrl,
|
||||||
|
duration: asset.media.metadata?.duration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export const previewInfoSchema = z.object({
|
|||||||
originalHeight: z.number().optional(),
|
originalHeight: z.number().optional(),
|
||||||
originalWidth: z.number().optional(),
|
originalWidth: z.number().optional(),
|
||||||
spriteUrl: z.string().optional(),
|
spriteUrl: z.string().optional(),
|
||||||
|
duration: z.number().optional(),
|
||||||
})
|
})
|
||||||
export type PreviewInfo = z.infer<typeof previewInfoSchema>
|
export type PreviewInfo = z.infer<typeof previewInfoSchema>
|
||||||
|
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ export function FileCard({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<FilePreview item={displayItem} />
|
<FilePreview item={displayItem} showDuration />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { File, Folder } from 'lucide-react'
|
import { File, Folder } from 'lucide-react'
|
||||||
import { SpriteScrubber } from '../sprite-scrubber'
|
import { SpriteScrubber } from '../sprite-scrubber'
|
||||||
|
import { formatTime } from '../viewers/utils'
|
||||||
|
|
||||||
type FilePreviewItem = {
|
type FilePreviewItem = {
|
||||||
type?: string | null
|
type?: string | null
|
||||||
@@ -9,39 +10,56 @@ type FilePreviewItem = {
|
|||||||
thumbnailUrl?: string
|
thumbnailUrl?: string
|
||||||
originalWidth?: number
|
originalWidth?: number
|
||||||
originalHeight?: number
|
originalHeight?: number
|
||||||
|
duration?: number
|
||||||
} | null
|
} | null
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FilePreviewProps {
|
interface FilePreviewProps {
|
||||||
item: FilePreviewItem
|
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 =
|
const isVideoWithSprite =
|
||||||
item.preview?.mediaType?.startsWith('video/') &&
|
isVideo &&
|
||||||
item.preview.spriteUrl &&
|
item.preview?.spriteUrl &&
|
||||||
item.preview.thumbnailUrl &&
|
item.preview.thumbnailUrl &&
|
||||||
item.preview.originalWidth &&
|
item.preview.originalWidth &&
|
||||||
item.preview.originalHeight
|
item.preview.originalHeight
|
||||||
|
|
||||||
if (isVideoWithSprite && item.preview) {
|
if (isVideoWithSprite && item.preview) {
|
||||||
return (
|
return (
|
||||||
<SpriteScrubber
|
<>
|
||||||
spriteUrl={item.preview.spriteUrl!}
|
<SpriteScrubber
|
||||||
thumbnailUrl={item.preview.thumbnailUrl!}
|
spriteUrl={item.preview.spriteUrl!}
|
||||||
videoWidth={item.preview.originalWidth!}
|
thumbnailUrl={item.preview.thumbnailUrl!}
|
||||||
videoHeight={item.preview.originalHeight!}
|
videoWidth={item.preview.originalWidth!}
|
||||||
/>
|
videoHeight={item.preview.originalHeight!}
|
||||||
|
/>
|
||||||
|
{durationOverlay}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.preview?.thumbnailUrl) {
|
if (item.preview?.thumbnailUrl) {
|
||||||
return (
|
return (
|
||||||
<img
|
<>
|
||||||
src={item.preview.thumbnailUrl}
|
<img
|
||||||
alt="Preview"
|
src={item.preview.thumbnailUrl}
|
||||||
className="w-full h-full object-contain bg-black"
|
alt="Preview"
|
||||||
/>
|
className="w-full h-full object-contain bg-black"
|
||||||
|
/>
|
||||||
|
{durationOverlay}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user