mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 11:24:43 +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()
|
||||
|
||||
Reference in New Issue
Block a user