Fix music directory path to match Docker mount point
This commit is contained in:
57
server/api/music/index.get.ts
Normal file
57
server/api/music/index.get.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import { join } from 'path'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Log incoming request for debugging proxy issues
|
||||
const headers = getHeaders(event)
|
||||
const realIP = headers['x-real-ip'] || headers['x-forwarded-for'] || 'unknown'
|
||||
console.log(`[MUSIC API] Music list request from ${realIP}`)
|
||||
|
||||
const musicDir = join(process.cwd(), 'public', 'music')
|
||||
|
||||
// Check if music directory exists
|
||||
try {
|
||||
await fs.access(musicDir)
|
||||
} catch {
|
||||
// Create music directory if it doesn't exist
|
||||
await fs.mkdir(musicDir, { recursive: true })
|
||||
return { tracks: [] }
|
||||
}
|
||||
|
||||
// Read music directory
|
||||
const files = await fs.readdir(musicDir)
|
||||
|
||||
// Filter audio files
|
||||
const audioExtensions = ['.mp3', '.wav', '.flac', '.m4a', '.ogg', '.aac']
|
||||
const musicFiles = files.filter(file =>
|
||||
audioExtensions.some(ext => file.toLowerCase().endsWith(ext))
|
||||
)
|
||||
|
||||
// Get file stats and create track objects
|
||||
const tracks = await Promise.all(
|
||||
musicFiles.map(async (file) => {
|
||||
const filePath = join(musicDir, file)
|
||||
const stats = await fs.stat(filePath)
|
||||
|
||||
return {
|
||||
name: file,
|
||||
size: stats.size,
|
||||
modified: stats.mtime,
|
||||
duration: null // We'll set this on the client side when the audio loads
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// Sort by name
|
||||
tracks.sort((a, b) => a.name.localeCompare(b.name))
|
||||
|
||||
return { tracks }
|
||||
} catch (error) {
|
||||
console.error('Error reading music directory:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Failed to load music files'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user