-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
195 lines (171 loc) · 5.52 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { cors } from 'hono/cors'
import { serveStatic } from "@hono/node-server/serve-static";
import AdmZip from 'adm-zip'
import { execSync } from 'child_process'
import fs from 'fs'
import path from 'path'
import os from 'os'
const app = new Hono()
app.use('/*', cors())
app.use("/", serveStatic({ root: "./public" }));
const uploadsDir = path.join(process.cwd(), 'uploads');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
async function fetchFileFromUrl(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return await response.arrayBuffer();
}
async function fetchFromGitHub(url, branch = 'main') {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-'));
try {
execSync(`git clone --depth 1 --branch ${branch} ${url} ${tempDir}`, { stdio: 'inherit' });
const zip = new AdmZip();
const addFilesToZip = (dir, zipPath = '') => {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
const filePath = path.join(dir, file.name);
if (file.isDirectory()) {
if (file.name !== '.git') {
addFilesToZip(filePath, path.join(zipPath, file.name));
}
} else {
zip.addLocalFile(filePath, zipPath);
}
}
};
addFilesToZip(tempDir);
return zip.toBuffer();
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
app.post('/upload', async (c) => {
try {
const formData = await c.req.formData()
const file = formData.get('zipFile')
const url = formData.get('url')
const branch = formData.get('branch') || 'main'
let buffer;
let filename;
if (file) {
buffer = await file.arrayBuffer()
filename = file.name;
} else if (url) {
if (url.includes('github.com')) {
buffer = await fetchFromGitHub(url, branch);
filename = url.split('/').pop() + '.zip';
} else {
buffer = await fetchFileFromUrl(url);
filename = url.split('/').pop();
}
} else {
return c.json({ error: 'No file or URL provided' }, 400)
}
const filePath = path.join(uploadsDir, filename);
fs.writeFileSync(filePath, Buffer.from(buffer));
const zip = new AdmZip(Buffer.from(buffer))
const zipEntries = zip.getEntries()
const fileStructure = buildFileStructure(zipEntries)
return c.json({ fileStructure, filename })
} catch (error) {
console.error('Error in /upload:', error)
return c.json({ error: 'Internal server error: ' + error.message }, 500)
}
})
app.get('/reopen/:filename', async (c) => {
try {
const filename = c.req.param('filename');
const filePath = path.join(uploadsDir, filename);
if (!fs.existsSync(filePath)) {
return c.json({ error: 'File not found' }, 404);
}
const zip = new AdmZip(filePath);
const zipEntries = zip.getEntries();
const fileStructure = buildFileStructure(zipEntries);
return c.json({ fileStructure, filename });
} catch (error) {
console.error('Error in /reopen:', error);
return c.json({ error: 'Internal server error: ' + error.message }, 500);
}
});
app.post('/extract', async (c) => {
try {
const formData = await c.req.formData()
const filesString = formData.get('files')
const filename = formData.get('filename')
if (!filename || !filesString) return c.json({ error: 'Missing filename or file list' }, 400)
const files = JSON.parse(filesString)
const filePath = path.join(uploadsDir, filename);
if (!fs.existsSync(filePath)) {
return c.json({ error: 'File not found' }, 404);
}
const zip = new AdmZip(filePath)
const extractedContent = files.map(file => {
const entry = zip.getEntry(file)
if (entry) {
let content = entry.getData().toString('utf8')
if (file.endsWith('.json')) {
try {
const jsonObj = JSON.parse(content)
content = JSON.stringify(jsonObj, null, 2)
} catch (e) {
console.error('Error parsing JSON:', e)
}
}
return `// ${file}\n${content}`
}
return `// ${file}\nFile not found in the ZIP archive.`
}).join('\n\n')
return c.json({ content: extractedContent })
} catch (error) {
console.error('Error in /extract:', error)
return c.json({ error: 'Internal server error: ' + error.message }, 500)
}
})
app.get('/uploads', (c) => {
const files = fs.readdirSync(uploadsDir);
return c.json(files);
});
app.delete('/upload/:filename', (c) => {
const filename = c.req.param('filename');
const filePath = path.join(uploadsDir, filename);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
return c.json({ message: 'File deleted successfully' });
} else {
return c.json({ error: 'File not found' }, 404);
}
});
function buildFileStructure(entries) {
const structure = {}
entries.forEach(entry => {
const path = entry.entryName.split('/')
let current = structure
path.forEach((part, index) => {
if (index === path.length - 1) {
current[part] = {
type: 'file',
path: entry.entryName
}
} else {
if (!current[part]) {
current[part] = {
type: 'directory',
children: {}
}
}
current = current[part].children
}
})
})
return structure
}
serve({
fetch: app.fetch,
port: 3001
})
console.log('Server is running on http://msn.so:3003')