fixed excel stuff
This commit is contained in:
@@ -3,6 +3,46 @@ import { db } from '$lib/server/db/index.js';
|
||||
import { audioFile } from '$lib/server/db/schema.js';
|
||||
import { eq, isNull } from 'drizzle-orm';
|
||||
import { uploadToS3, deleteFromS3, generateAudioS3Key } from '$lib/server/s3.js';
|
||||
import { exec } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
async function getAudioDuration(buffer) {
|
||||
try {
|
||||
// Create temporary file
|
||||
const tempDir = os.tmpdir();
|
||||
const tempFilePath = path.join(tempDir, `audio_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`);
|
||||
|
||||
// Write buffer to temp file
|
||||
await fs.writeFile(tempFilePath, buffer);
|
||||
|
||||
try {
|
||||
// Use ffprobe to get duration
|
||||
const { stdout } = await execAsync(`ffprobe -v quiet -show_entries format=duration -of csv=p=0 "${tempFilePath}"`);
|
||||
const duration = parseFloat(stdout.trim());
|
||||
|
||||
// Clean up temp file
|
||||
await fs.unlink(tempFilePath);
|
||||
|
||||
return isNaN(duration) ? null : duration;
|
||||
} catch (error) {
|
||||
// Clean up temp file even if ffprobe fails
|
||||
try {
|
||||
await fs.unlink(tempFilePath);
|
||||
} catch {}
|
||||
|
||||
console.error('Error extracting audio duration:', error);
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating temp file for duration extraction:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function load() {
|
||||
const audioFiles = await db.select({
|
||||
@@ -43,16 +83,19 @@ export const actions = {
|
||||
const s3Key = generateAudioS3Key(id, file.name);
|
||||
|
||||
try {
|
||||
// Extract duration before uploading
|
||||
const duration = await getAudioDuration(buffer);
|
||||
|
||||
// Upload to S3 first
|
||||
await uploadToS3(s3Key, buffer, file.type);
|
||||
|
||||
// Then save metadata to database (without blob data)
|
||||
// Then save metadata to database with calculated duration
|
||||
await db.insert(audioFile).values({
|
||||
id,
|
||||
filename: file.name,
|
||||
contentType: file.type,
|
||||
s3Key,
|
||||
duration: null, // Will be updated by client-side after upload
|
||||
duration,
|
||||
fileSize: file.size,
|
||||
createdAt: new Date()
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user