update: drag and drop + compression des textures
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { join } from 'path'
|
||||
import { existsSync } from 'fs'
|
||||
import { mkdir, writeFile, readFile, unlink, rm } from 'fs/promises'
|
||||
import { TMP_DIR } from '@/lib/constants'
|
||||
import { compressWithBlender } from '@/lib/blender'
|
||||
import { compressTextureBuffer } from '@/lib/texture-compression'
|
||||
import type { ParsedFile } from '@/lib/types'
|
||||
|
||||
interface PushFile {
|
||||
path: string
|
||||
contentBase64: string
|
||||
}
|
||||
|
||||
interface PrepareGitAssetsParams {
|
||||
folderName: string
|
||||
destination: string
|
||||
parsedFiles: ParsedFile[]
|
||||
}
|
||||
|
||||
interface PrepareGitAssetsResult {
|
||||
filesToPush: PushFile[]
|
||||
modelFilename: string
|
||||
textureNames: string[]
|
||||
compressed: boolean
|
||||
compressionError?: string
|
||||
}
|
||||
|
||||
export async function prepareGitAssets({
|
||||
folderName,
|
||||
destination,
|
||||
parsedFiles,
|
||||
}: PrepareGitAssetsParams): Promise<PrepareGitAssetsResult> {
|
||||
const filesToPush: PushFile[] = []
|
||||
const textureNames: string[] = []
|
||||
let modelFilename = ''
|
||||
let compressed = false
|
||||
let compressionError: string | undefined
|
||||
|
||||
for (const pf of parsedFiles) {
|
||||
let content = pf.buffer
|
||||
|
||||
if (pf.isModel) {
|
||||
modelFilename = pf.filename
|
||||
|
||||
const tmpFolder = join(TMP_DIR, folderName)
|
||||
await mkdir(tmpFolder, { recursive: true })
|
||||
const tmpFilePath = join(tmpFolder, pf.filename)
|
||||
await writeFile(tmpFilePath, pf.buffer)
|
||||
|
||||
const stem = pf.filename.replace(/\.[^.]+$/, '')
|
||||
const compressedPath = join(tmpFolder, `${stem}_compressed.glb`)
|
||||
|
||||
try {
|
||||
const result = await compressWithBlender(tmpFilePath, compressedPath)
|
||||
|
||||
if (result.success && existsSync(compressedPath)) {
|
||||
content = await readFile(compressedPath)
|
||||
compressed = true
|
||||
await unlink(compressedPath).catch(() => {})
|
||||
} else {
|
||||
compressionError = result.error
|
||||
}
|
||||
} finally {
|
||||
await unlink(tmpFilePath).catch(() => {})
|
||||
await rm(tmpFolder, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
} else {
|
||||
textureNames.push(pf.filename)
|
||||
|
||||
const textureResult = await compressTextureBuffer(pf.filename, pf.buffer)
|
||||
content = textureResult.buffer
|
||||
|
||||
if (textureResult.error && !compressionError) {
|
||||
compressionError = textureResult.error
|
||||
}
|
||||
}
|
||||
|
||||
filesToPush.push({
|
||||
path: `public/models/${destination}/${folderName}/${pf.filename}`,
|
||||
contentBase64: content.toString('base64'),
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
filesToPush,
|
||||
modelFilename,
|
||||
textureNames,
|
||||
compressed,
|
||||
compressionError,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { extname } from 'path'
|
||||
import sharp from 'sharp'
|
||||
|
||||
interface TextureCompressionResult {
|
||||
buffer: Buffer
|
||||
compressed: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
export async function compressTextureBuffer(
|
||||
filename: string,
|
||||
buffer: Buffer,
|
||||
): Promise<TextureCompressionResult> {
|
||||
const ext = extname(filename).toLowerCase()
|
||||
|
||||
try {
|
||||
if (ext === '.jpg' || ext === '.jpeg') {
|
||||
return {
|
||||
buffer: await sharp(buffer).jpeg({ quality: 82, mozjpeg: true }).toBuffer(),
|
||||
compressed: true,
|
||||
}
|
||||
}
|
||||
|
||||
if (ext === '.png') {
|
||||
return {
|
||||
buffer: await sharp(buffer).png({ compressionLevel: 9, adaptiveFiltering: true }).toBuffer(),
|
||||
compressed: true,
|
||||
}
|
||||
}
|
||||
|
||||
if (ext === '.webp') {
|
||||
return {
|
||||
buffer: await sharp(buffer).webp({ quality: 82 }).toBuffer(),
|
||||
compressed: true,
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return {
|
||||
buffer,
|
||||
compressed: false,
|
||||
error: `Compression texture echouee pour ${filename}: ${message}`,
|
||||
}
|
||||
}
|
||||
|
||||
return { buffer, compressed: false }
|
||||
}
|
||||
+5
-35
@@ -56,9 +56,11 @@ export async function checkFolderDiffs(
|
||||
secret: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<CheckResult> {
|
||||
const params = new URLSearchParams({ folderName: folder.folderName, destination })
|
||||
const res = await fetch(`/api/upload/check?${params}`, {
|
||||
const formData = buildUploadFormData(folder, destination)
|
||||
const res = await fetch('/api/upload/check', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
body: formData,
|
||||
signal,
|
||||
})
|
||||
|
||||
@@ -73,39 +75,7 @@ export async function checkFolderDiffs(
|
||||
return { exists: false, diffs: [] }
|
||||
}
|
||||
|
||||
const remoteFiles: { name: string; size: number }[] = data.files || []
|
||||
const remoteMap = new Map(remoteFiles.map((f) => [f.name.toLowerCase(), f.size]))
|
||||
|
||||
const diffs: FileDiff[] = []
|
||||
const localNames = new Set<string>()
|
||||
|
||||
// Model: skip size comparison (compression changes the size).
|
||||
const modelKey = folder.modelFile.name.toLowerCase()
|
||||
localNames.add(modelKey)
|
||||
if (!remoteMap.has(modelKey)) {
|
||||
diffs.push({ name: folder.modelFile.name, status: 'new' })
|
||||
}
|
||||
|
||||
// Textures: compare by size
|
||||
for (const tex of folder.textures) {
|
||||
const key = tex.name.toLowerCase()
|
||||
localNames.add(key)
|
||||
const remoteSize = remoteMap.get(key)
|
||||
if (remoteSize === undefined) {
|
||||
diffs.push({ name: tex.name, status: 'new' })
|
||||
} else if (remoteSize !== tex.file.size) {
|
||||
diffs.push({ name: tex.name, status: 'changed' })
|
||||
}
|
||||
}
|
||||
|
||||
// Deleted
|
||||
for (const [name] of remoteMap) {
|
||||
if (!localNames.has(name)) {
|
||||
diffs.push({ name, status: 'deleted' })
|
||||
}
|
||||
}
|
||||
|
||||
return { exists: true, diffs }
|
||||
return { exists: true, diffs: (data.diffs || []) as FileDiff[] }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user