Files
appforge/server/src/routes/storage.ts
T

39 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Router } from 'express';
import multer from 'multer';
import { h } from '../lib/respond.js';
import { requireAuth, type AuthedRequest } from '../middleware/auth.js';
import { writeFile, deleteFile, listFiles, publicUrl } from '../lib/storage.js';
const r = Router();
const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 600 * 1024 * 1024 } });
// POST /storage/upload (multipart: bucket, path, file)
r.post('/upload', requireAuth, upload.single('file'), h(async (req: AuthedRequest) => {
const bucket = req.body.bucket as string;
const relPath = (req.body.path as string) || (req as any).file?.originalname;
const file = (req as any).file;
if (!file) throw new Error('No file provided');
if (!bucket) throw new Error('No bucket provided');
const result = writeFile(bucket, relPath, file.buffer);
return result;
}));
// DELETE /storage (body: bucket, path)
r.delete('/', requireAuth, h(async (req) => {
const { bucket, path: relPath } = req.body || {};
deleteFile(bucket, relPath);
return { message: 'File deleted successfully' };
}));
// GET /storage/list?bucket=&folder=
r.get('/list', requireAuth, h(async (req) => {
return listFiles(req.query.bucket as string, (req.query.folder as string) || '');
}));
// GET /storage/public-url?bucket=&path=
r.get('/public-url', h(async (req) => {
return { url: publicUrl(req.query.bucket as string, req.query.path as string) };
}));
export default r;