This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
168 lines (115 loc) · 4.27 KB
/
index.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
require('dotenv').config()
const { Deta } = require('deta')
const upload = require("express-fileupload");
const express = require('express')
var uniqid = require('uniqid');
const app = express()
const fs = require('fs')
app.use(upload());
const deta = Deta()
const images = deta.Base('images_data')
const drive = deta.Drive('images')
const allowed_filetypes = ["image/apng", "image/avif", "image/gif", "image/jpeg", "image/png", "image/webp"]
app.get('/sharex', async (req, res) => {
let host = req.get('host');
let client_config = {
"Version": "14.1.0",
"Name": `${host}`,
"DestinationType": "ImageUploader",
"RequestMethod": "POST",
"RequestURL": `${req.secure ? 'https://' : 'http://'}${host}/upload`,
"Headers": {
"key": ""
},
"Body": "MultipartFormData",
"FileFormName": "image",
"URL": "{json:url}"
}
res.writeHead(200, {
'Content-Disposition': `attachment; filename="${host}.sxcu"`,
'Content-Type': `text/json`,
})
const download = Buffer.from(JSON.stringify(client_config))
res.end(download)
})
app.get('/:id', async (req, res) => {
let id = req.params.id;
id = id.split('.')[0]
let imgData = await images.get(id)
if(!imgData) return res.send({ error: true, msg: "Not Found" })
let template = fs.readFileSync('./template.html', 'utf-8')
console.log(imgData)
let date = new Date(parseInt(imgData.timestamp))
template = template
.replace(/{FILE_NAME}/g, imgData.name)
.replace(/{URL}/g, imgData.preview_url)
.replace(/{DL_URL}/g, imgData.dl_url)
.replace(/{UPLOAD_DATE}/g, `${date.toLocaleDateString("default")} - ${date.toLocaleTimeString("default")}`)
res.setHeader('Content-Type', `text/html`)
res.send(template)
})
app.get('/i/:id', async (req, res) => {
let id = req.params.id;
id = id.split('.')[0]
let imgData = await images.get(id)
if(!imgData) return res.send({ error: true, msg: "Not Found" })
console.log(id, imgData)
let img = await drive.get(`${id}.${imgData.type}`);
if(!img) return res.send({ error: true, msg: "Image Not Found." })
let buffer = await img.arrayBuffer();
let data = Buffer.from(buffer)
res.setHeader('Content-Type', `image/${imgData.type}`)
res.send(data)
})
app.get('/download/:id', async (req, res) => {
let id = req.params.id;
id = id.split('.')[0]
let imgData = await images.get(id)
if(!imgData) return res.send({ error: true, msg: "Not Found" })
console.log(id, imgData)
let img = await drive.get(`${id}.${imgData.type}`);
if(!img) return res.send({ error: true, msg: "Image Not Found." })
let buffer = await img.arrayBuffer();
let data = Buffer.from(buffer)
res.writeHead(200, {
'Content-Disposition': `attachment; filename="${imgData.name.split(".")[0]}.${imgData.type}"`,
'Content-Type': `image/${imgData.type}`,
})
res.end(data);
})
app.post('/upload', async (req, res) => {
//console.log(req.files.image)
if(!req.headers.key || req.headers.key !== process.env.ENV_UPLOAD_KEY) return res.send({ error: true, msg: "Unauthorized. Invalid Key" })
let id = await generateId()
let name = req.files.image.name;
let contents = req.files.image.data;
let type = req.files.image.mimetype
var host = req.get('host')
if(!allowed_filetypes.includes(type)) return res.send({ error: true, msg : "Unsupported Filetype." })
type = type.split("/")[1]
let data = {
key: id,
name: name ? name : `${id}.${type}`,
url: `${req.secure ? 'https://' : 'http://'}${host}/${id}`,
preview_url: `${req.secure ? 'https://' : 'http://'}${host}/i/${id}`,
dl_url: `${req.secure ? 'https://' : 'http://'}${host}/download/${id}`,
type: `${type}`,
timestamp: `${Date.now()}`
}
console.log(data)
let image = await images.put(data)
let img = await drive.put(`${id}.${type}`, {data: contents});
res.send(data);
})
const generateId = async() => {
let id = await uniqid.time()
let found = await images.get(id)
if(found) {
return await generateId()
} else {
return id
}
}
// export 'app'
module.exports = app
app.listen(3000)