From a066f5a0b743b5a7422944e12eeffc7765ec7733 Mon Sep 17 00:00:00 2001 From: Yaqub Mahmoud Date: Tue, 28 Jan 2025 16:30:08 +0900 Subject: [PATCH] Update api.copy-files.ts --- app/routes/api.copy-files.ts | 80 +++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/app/routes/api.copy-files.ts b/app/routes/api.copy-files.ts index 1f3f0a9..523b559 100644 --- a/app/routes/api.copy-files.ts +++ b/app/routes/api.copy-files.ts @@ -1,36 +1,70 @@ -// api/copy-files.ts -import express from 'express'; +// server.ts +import http from 'http'; +import { parse as parseUrl } from 'url'; import fs from 'fs/promises'; import path from 'path'; -const router = express.Router(); +interface FileContent { + type: string; + content: string; +} -router.post('/copy-files', async (req, res) => { - try { - const { files, targetDirectory } = req.body; +interface CopyFilesRequest { + files: Record; + targetDirectory: string; +} - // ensure target directory exists - await fs.mkdir(targetDirectory, { recursive: true }); +const server = http.createServer(async (req, res) => { + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); - // copy each file - for (const [filePath, dirent] of Object.entries(files)) { - if (dirent?.type === 'file' && dirent.content) { - const fullPath = path.join(targetDirectory, filePath.startsWith('/') ? filePath.slice(1) : filePath); - const dirPath = path.dirname(fullPath); + if (req.method === 'OPTIONS') { + res.writeHead(204); + res.end(); - // ensure directory exists - await fs.mkdir(dirPath, { recursive: true }); + return; + } - // write file - await fs.writeFile(fullPath, dirent.content); + const parsedUrl = parseUrl(req.url || '', true); + + if (parsedUrl.pathname === '/copy-files' && req.method === 'POST') { + try { + let body = ''; + + for await (const chunk of req) { + body += chunk; } - } - res.json({ success: true }); - } catch (error) { - console.error('Error copying files:', error); - res.status(500).json({ error: 'Failed to copy files' }); + const { files, targetDirectory } = JSON.parse(body) as CopyFilesRequest; + + await fs.mkdir(targetDirectory, { recursive: true }); + + for (const [filePath, dirent] of Object.entries(files)) { + if (dirent?.type === 'file' && dirent.content) { + const fullPath = path.join(targetDirectory, filePath.startsWith('/') ? filePath.slice(1) : filePath); + const dirPath = path.dirname(fullPath); + + await fs.mkdir(dirPath, { recursive: true }); + + await fs.writeFile(fullPath, dirent.content); + } + } + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ success: true })); + } catch (error) { + console.error('Error copying files:', error); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Failed to copy files' })); + } + } else { + res.writeHead(404, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: 'Not found' })); } }); -export default router; +const PORT = process.env.PORT || 3001; +server.listen(PORT, () => { + console.log(`Server running at http://localhost:${PORT}`); +});