-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
138 lines (110 loc) · 3.93 KB
/
server.js
File metadata and controls
138 lines (110 loc) · 3.93 KB
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
require('dotenv').config({ path: "../.env" });
const path = require('path');
const express = require("express");
const serveIndex = require("serve-index");
const cors = require('cors');
const multer = require('multer');
const FILES = path.join(__dirname, "../files");
const upload = multer();
const flexatarApiUrl = "https://api.flexatar-sdk.com"
const flexatarApiSecret = process.env.FLEXATAR_API_SECRET;
async function requestUploadLink() {
const response = await fetch(flexatarApiUrl + "/b2b/createflexatar", {
method: "POST",
headers: {
'Authorization': 'Bearer ' + flexatarApiSecret,
"Content-Type": "application/json",
}
})
if (response.ok) {
const flexatarCreationInfo = await response.json()
console.log(flexatarCreationInfo)
return flexatarCreationInfo
} else {
return { error: true,status:response.status }
}
}
async function requestDownloadLink(id,whatToDownload) {
const response = await fetch(flexatarApiUrl + `/b2b/createflexatar/${whatToDownload}/` + id, {
method: "POST",
headers: {
'Authorization': 'Bearer ' + flexatarApiSecret,
"Content-Type": "application/json",
}
})
if (response.ok) {
const flexatarCreationInfo = await response.json()
console.log(flexatarCreationInfo)
return flexatarCreationInfo.link
} else {
return { error: true }
}
}
async function uploadToS3(link, file) {
const formData = new FormData();
// Add all S3 required fields
for (const [k, v] of Object.entries(link.fields)) {
formData.append(k, v);
}
// Attach the file (from req.file or req.files in Express)
formData.append('file', new Blob([file.buffer]), file.originalname);
// Send POST request to S3
const res = await fetch(link.url, {
method: 'POST',
body: formData
});
if (!res.ok) {
const text = await res.text();
throw new Error(`S3 upload failed: ${res.status} ${text}`);
}
return {
success: true,
};
}
const app = express();
const PORT = 8082;
const ROOT = path.join(__dirname, "dist");
app.use(express.json());
app.use(cors());
app.post('/upload', upload.single('file'), async (req, res) => {
try {
const ftarCreationInfo = await requestUploadLink()
if (ftarCreationInfo.error){
res.status(ftarCreationInfo.status).json({ error: "unavailable" });
return
}
console.log("ftarCreationInfo",ftarCreationInfo)
await uploadToS3(ftarCreationInfo.link, req.file);
delete ftarCreationInfo.link
// console.log("req.file",req.file)
res.json(ftarCreationInfo);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/download/:what/:id', async (req, res) => {
try {
const fileId = req.params.id;
const whatToDownload = req.params.what;
const downlladLink = await requestDownloadLink(fileId,whatToDownload)
console.log(downlladLink)
// Fetch from remote resource
const response = await fetch(downlladLink);
if (!response.ok) {
return res.status(response.status).send(`Remote fetch failed: ${response.statusText}`);
}
// Copy headers (optional, if you want to preserve content-type, etc.)
res.set('Content-Type', response.headers.get('content-type') || 'application/octet-stream');
// Stream the body back to the client
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
res.send(buffer);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.use("/", express.static(ROOT));
app.use("/", serveIndex(ROOT));
app.use("/files",express.static(FILES));
app.use("/files", serveIndex(FILES));
app.listen(PORT, () => console.log('Server running on port ' + PORT));