[+] File server
This commit is contained in:
+2
-1
@@ -27,4 +27,5 @@ public/assets/fonts/cache
|
|||||||
.idea
|
.idea
|
||||||
static/audio
|
static/audio
|
||||||
temp_audio
|
temp_audio
|
||||||
.stfolder
|
.stfolder
|
||||||
|
storage/
|
||||||
@@ -6,6 +6,8 @@ services:
|
|||||||
dockerfile: deploy/Dockerfile.web
|
dockerfile: deploy/Dockerfile.web
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:3000:3000"
|
- "127.0.0.1:3000:3000"
|
||||||
|
volumes:
|
||||||
|
- ./data/storage:/app/storage
|
||||||
environment:
|
environment:
|
||||||
- ORIGIN=http://localhost:3000
|
- ORIGIN=http://localhost:3000
|
||||||
- MONGO_URL=mongodb://cat:meow@db:27017/amaoke?authSource=admin
|
- MONGO_URL=mongodb://cat:meow@db:27017/amaoke?authSource=admin
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import type { ObjectId } from 'mongodb'
|
|||||||
import '../ext'
|
import '../ext'
|
||||||
import { promises as fs } from 'fs'
|
import { promises as fs } from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { waitFor } from '../utils'
|
|
||||||
import { separateSong } from './separator'
|
import { separateSong } from './separator'
|
||||||
|
|
||||||
const CACHE_DIR = path.resolve('static/audio')
|
const CACHE_DIR = path.resolve('storage/audio')
|
||||||
|
export const getAudioFile = (songId: string, filename: string) => path.join(CACHE_DIR, songId, filename)
|
||||||
|
|
||||||
const neCookie = async () => (await dbs.serverProps
|
const neCookie = async () => (await dbs.serverProps
|
||||||
.findOne({ name: 'global_settings' }))?.netease_login_cookie
|
.findOne({ name: 'global_settings' }))?.netease_login_cookie
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { getAudioFile } from '$lib/server/songs';
|
||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
import path from 'path';
|
||||||
|
import type { RequestHandler } from './$types';
|
||||||
|
|
||||||
|
export const GET: RequestHandler = async ({ params }) => {
|
||||||
|
const { songId, file } = params;
|
||||||
|
const filePath = getAudioFile(songId, file);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(filePath);
|
||||||
|
} catch {
|
||||||
|
throw error(404, 'File not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const ext = path.extname(file).toLowerCase();
|
||||||
|
let contentType = 'application/octet-stream';
|
||||||
|
if (ext === '.mp3') contentType = 'audio/mpeg';
|
||||||
|
if (ext === '.opus') contentType = 'audio/ogg';
|
||||||
|
|
||||||
|
const buffer = await fs.readFile(filePath);
|
||||||
|
|
||||||
|
return new Response(buffer, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': contentType,
|
||||||
|
'Cache-Control': 'public, max-age=31536000'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user