mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
feat(transcode): implement new image and video transcode strategies with UI detail enhancements
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { AssetInfo } from '@/dtos/asset'
|
||||
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||
import { getBestTranscode } from '@/ui/lib/media'
|
||||
import { Minus, Plus } from 'lucide-react'
|
||||
import { Minus, Plus, Copy, Download, Check } from 'lucide-react'
|
||||
import type { RefObject } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type Player from 'video.js/dist/types/player'
|
||||
@@ -52,6 +52,7 @@ export function FileViewer({
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
const imgW = file.media?.metadata?.originalWidth ?? 1920
|
||||
const imgH = file.media?.metadata?.originalHeight ?? 1080
|
||||
@@ -122,6 +123,58 @@ 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 handleCopy = async () => {
|
||||
const webpUrl = bestUrl
|
||||
if (!webpUrl) return
|
||||
try {
|
||||
const response = await fetch(webpUrl)
|
||||
const blob = await response.blob()
|
||||
|
||||
const img = new Image()
|
||||
img.crossOrigin = 'anonymous'
|
||||
img.src = URL.createObjectURL(blob)
|
||||
await new Promise((resolve, reject) => {
|
||||
img.onload = resolve
|
||||
img.onerror = reject
|
||||
})
|
||||
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = img.width
|
||||
canvas.height = img.height
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) throw new Error('Could not get canvas context')
|
||||
ctx.drawImage(img, 0, 0)
|
||||
|
||||
canvas.toBlob(async (pngBlob) => {
|
||||
if (!pngBlob) return
|
||||
try {
|
||||
await navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
'image/png': pngBlob,
|
||||
}),
|
||||
])
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 2000)
|
||||
} catch (err) {
|
||||
console.error('Failed to copy image to clipboard:', err)
|
||||
}
|
||||
}, 'image/png')
|
||||
} catch (err) {
|
||||
console.error('Failed to copy image:', err)
|
||||
}
|
||||
}
|
||||
|
||||
if (!bestUrl) {
|
||||
return (
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
@@ -160,7 +213,7 @@ export function FileViewer({
|
||||
</div>
|
||||
{/* Zoom Toolbar */}
|
||||
<div className="relative px-4 py-3 bg-gray-50 dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5">
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5 mr-auto">
|
||||
<button
|
||||
onClick={() => handleZoom(0.8)}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors"
|
||||
@@ -181,10 +234,28 @@ export function FileViewer({
|
||||
</div>
|
||||
<button
|
||||
onClick={handleFit}
|
||||
className="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"
|
||||
className="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 animate-in fade-in zoom-in-95 duration-200"
|
||||
>
|
||||
Fit
|
||||
</button>
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
disabled={!bestUrl}
|
||||
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="Copy optimized image to clipboard"
|
||||
>
|
||||
{copied ? <Check size={14} className="text-green-500" /> : <Copy size={14} />}
|
||||
{copied ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDownload}
|
||||
disabled={!file.media?.original?.downloadUrl}
|
||||
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"
|
||||
>
|
||||
<Download size={14} />
|
||||
Download
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -99,6 +99,9 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
if (!data.media?.metadata) {
|
||||
return null
|
||||
}
|
||||
|
||||
const previewResolutions = resolutions.filter((r) => !r.isRaw)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
@@ -239,7 +242,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuLabel>Quality</DropdownMenuLabel>
|
||||
{resolutions.map((res) => (
|
||||
{previewResolutions.map((res) => (
|
||||
<DropdownMenuItem
|
||||
key={res.resolution}
|
||||
onClick={() => changeResolution(res)}
|
||||
@@ -378,33 +381,29 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
resolution: t.isRaw ? 'Original' : `${t.height}p`,
|
||||
}))
|
||||
|
||||
const previewResolutions = resolutions.filter((r) => !r.isRaw)
|
||||
|
||||
// Logic to select best resolution based on screen size
|
||||
const getInitialResolution = (): DisplayTranscode => {
|
||||
if (typeof window === 'undefined') return originalRes
|
||||
if (previewResolutions.length === 0) return originalRes
|
||||
|
||||
if (typeof window === 'undefined') return previewResolutions[0]
|
||||
|
||||
// Use device pixel ratio for high DPI screens
|
||||
const screenWidth = window.innerWidth * (window.devicePixelRatio || 1)
|
||||
|
||||
// Strategy: Find smallest resolution that is >= screenWidth.
|
||||
// Filter out raw versions unless they are the only option.
|
||||
|
||||
const nonRaw = resolutions.filter((r) => !r.isRaw)
|
||||
const candidates = nonRaw.length > 0 ? nonRaw : resolutions
|
||||
|
||||
// 1. Sort by width ascending
|
||||
const sortedResolutions = [...candidates].sort((a, b) => {
|
||||
const sortedResolutions = [...previewResolutions].sort((a, b) => {
|
||||
const wA = a.width ?? 0
|
||||
const wB = b.width ?? 0
|
||||
if (wA !== wB) return wA - wB
|
||||
// If equal width, prefer non-raw (optimized)
|
||||
return (a.isRaw ? 1 : 0) - (b.isRaw ? 1 : 0)
|
||||
return wA - wB
|
||||
})
|
||||
|
||||
// 2. Find first one >= screenWidth
|
||||
const bestFit = sortedResolutions.find((r) => (r.width ?? 0) >= screenWidth)
|
||||
|
||||
// 3. If found, return it. If not (all are smaller), return the last one (largest).
|
||||
return bestFit || sortedResolutions[sortedResolutions.length - 1] || originalRes
|
||||
return bestFit || sortedResolutions[sortedResolutions.length - 1]
|
||||
}
|
||||
|
||||
const initialRes = getInitialResolution()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageTranscodeStrategy, VideoTranscodeStrategy } from '@/dtos/team'
|
||||
import { VideoTranscodeStrategy } from '@/dtos/team'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { AgentsSettings } from '@/ui/components/settings/AgentsSettings'
|
||||
import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings'
|
||||
@@ -81,16 +81,6 @@ function TeamSettingsPage() {
|
||||
})
|
||||
}
|
||||
|
||||
const handleImageStrategyChange = (value: ImageTranscodeStrategy) => {
|
||||
updateSettings({
|
||||
teamId,
|
||||
data: {
|
||||
key: 'transcode.imageStrategy',
|
||||
value: value,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
if (isSettingsLoading || isMeLoading) {
|
||||
return (
|
||||
<div className="flex h-screen items-center justify-center">
|
||||
@@ -103,13 +93,14 @@ function TeamSettingsPage() {
|
||||
return <div className="p-8 text-center text-red-500">Failed to load settings.</div>
|
||||
}
|
||||
|
||||
const currentVideoStrategy =
|
||||
let currentVideoStrategy =
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.single
|
||||
|
||||
const currentImageStrategy =
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(settings as any)?.transcode?.imageStrategy || ImageTranscodeStrategy.single
|
||||
(settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match
|
||||
if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') {
|
||||
currentVideoStrategy = VideoTranscodeStrategy.best_match
|
||||
} else if (currentVideoStrategy === 'full') {
|
||||
currentVideoStrategy = VideoTranscodeStrategy.all
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-full bg-slate-50 dark:bg-slate-950 font-sans selection:bg-blue-100 dark:selection:bg-blue-900 transition-colors duration-300">
|
||||
@@ -342,11 +333,13 @@ function TeamSettingsPage() {
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.single
|
||||
currentVideoStrategy === VideoTranscodeStrategy.best_match
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.single)}
|
||||
onClick={() =>
|
||||
handleVideoStrategyChange(VideoTranscodeStrategy.best_match)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Best match</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
@@ -357,72 +350,17 @@ function TeamSettingsPage() {
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.full
|
||||
currentVideoStrategy === VideoTranscodeStrategy.all
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.full)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.all)}
|
||||
>
|
||||
<div className="font-semibold">All resolutions</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Generates all supported resolutions up to the source quality.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.disable
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() =>
|
||||
handleVideoStrategyChange(VideoTranscodeStrategy.disable)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Disable</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Transcoding is disabled. Only system artifacts are generated.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Image Strategy */}
|
||||
<div className="space-y-3">
|
||||
<h3 className="text-lg font-medium">Image Strategy</h3>
|
||||
<div className="space-y-3">
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentImageStrategy === ImageTranscodeStrategy.single
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleImageStrategyChange(ImageTranscodeStrategy.single)}
|
||||
>
|
||||
<div className="font-semibold">Best match</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Generates a single optimal resolution matching the source quality.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentImageStrategy === ImageTranscodeStrategy.disable
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() =>
|
||||
handleImageStrategyChange(ImageTranscodeStrategy.disable)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Disable</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Transcoding is disabled. Only system thumbnails are generated.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
Reference in New Issue
Block a user