rippanteq7
commited on
Commit
•
2e4ef40
1
Parent(s):
fa1ba63
fix obj not defined, add favicon
Browse files- favicon.ico +0 -0
- index.js +195 -192
- package.json +1 -0
favicon.ico
ADDED
index.js
CHANGED
@@ -1,192 +1,195 @@
|
|
1 |
-
import bytes from 'bytes'
|
2 |
-
import carbon from 'carbon-now-scraper'
|
3 |
-
import cp from 'child_process'
|
4 |
-
import express from 'express'
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
app.
|
18 |
-
app.use(express.
|
19 |
-
|
20 |
-
app.use((
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
}
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
const
|
40 |
-
|
41 |
-
|
42 |
-
const
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
const
|
62 |
-
|
63 |
-
|
64 |
-
})
|
65 |
-
const
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
}
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
if (!
|
83 |
-
|
84 |
-
const
|
85 |
-
|
86 |
-
|
87 |
-
const
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
}
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
if (
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
const
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
const
|
118 |
-
await
|
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 |
-
if (
|
173 |
-
|
174 |
-
|
175 |
-
if (
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
if (
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
doc.
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
}
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
1 |
+
import bytes from 'bytes'
|
2 |
+
import carbon from 'carbon-now-scraper'
|
3 |
+
import cp from 'child_process'
|
4 |
+
import express from 'express'
|
5 |
+
import favicon from 'serve-favicon'
|
6 |
+
import fs from 'fs'
|
7 |
+
import morgan from 'morgan'
|
8 |
+
import os from 'os'
|
9 |
+
import path from 'path'
|
10 |
+
import PDFDocument from 'pdfkit'
|
11 |
+
import sharp from 'sharp'
|
12 |
+
import util from 'util'
|
13 |
+
|
14 |
+
const app = express()
|
15 |
+
const tmpDir = os.tmpdir()
|
16 |
+
|
17 |
+
app.set('json spaces', 4)
|
18 |
+
app.use(express.json({ limit: '200mb' }))
|
19 |
+
app.use(express.urlencoded({ extended: true, limit: '200mb' }))
|
20 |
+
app.use(favicon(path.join(import.meta.dirname, 'favicon.ico')))
|
21 |
+
app.use(morgan('combined'))
|
22 |
+
|
23 |
+
app.use((_, __, next) => {
|
24 |
+
fs.readdirSync(tmpDir).map((file) => {
|
25 |
+
file = `${tmpDir}/${file}`
|
26 |
+
const stat = fs.statSync(file)
|
27 |
+
const exp = Date.now() - stat.mtimeMs >= 1000 * 60 * 30
|
28 |
+
if (stat.isFile() && exp) {
|
29 |
+
console.log('Deleted file', file)
|
30 |
+
fs.unlinkSync(file)
|
31 |
+
}
|
32 |
+
})
|
33 |
+
next()
|
34 |
+
})
|
35 |
+
|
36 |
+
app.use('/file', express.static(tmpDir))
|
37 |
+
|
38 |
+
app.all('/', (_, res) => {
|
39 |
+
const status = {}
|
40 |
+
status['diskUsage'] = cp.execSync('du -sh').toString().split('M')[0] + ' MB'
|
41 |
+
|
42 |
+
const used = process.memoryUsage()
|
43 |
+
for (let x in used) status[x] = formatSize(used[x])
|
44 |
+
|
45 |
+
const totalmem = os.totalmem()
|
46 |
+
const freemem = os.freemem()
|
47 |
+
status['memoryUsage'] = `${formatSize(totalmem - freemem)} / ${formatSize(totalmem)}`
|
48 |
+
|
49 |
+
res.json({
|
50 |
+
creator: `@${process.env['SPACE_AUTHOR_NAME'] || 'rippanteq7'}`,
|
51 |
+
message: 'Hello World!',
|
52 |
+
uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4],
|
53 |
+
status
|
54 |
+
})
|
55 |
+
})
|
56 |
+
|
57 |
+
app.all('/carbon', async (req, res) => {
|
58 |
+
if (!['GET', 'POST'].includes(req.method)) return res.status(405).json({ success: false, message: 'Method Not Allowed' })
|
59 |
+
|
60 |
+
try {
|
61 |
+
const obj = req.method !== 'GET' ? req.body : req.query
|
62 |
+
if (!obj.code) return res.status(400).json({ success: false, message: 'Required parameter \'code\'' })
|
63 |
+
|
64 |
+
const filePath = `${tmpDir}/${randomName('.png')}`
|
65 |
+
const image = await carbon(obj.code, filePath, {
|
66 |
+
...obj, puppeteer: { executablePath: process.env.CHROME_BIN }
|
67 |
+
})
|
68 |
+
const resultUrl = `https://${req.hostname}/${image.replace(tmpDir, 'file')}`
|
69 |
+
isTrue(obj.json) ? res.json({ success: true, result: resultUrl }) : res[isTrue(obj.raw) ? 'send' : 'redirect'](resultUrl)
|
70 |
+
} catch (e) {
|
71 |
+
console.log(e)
|
72 |
+
res.status(500).json({ error: true, message: getError(e) })
|
73 |
+
}
|
74 |
+
})
|
75 |
+
|
76 |
+
app.all('/topdf', async (req, res) => {
|
77 |
+
if (req.method !== 'POST') return res.status(405).json({ success: false, message: 'Method Not Allowed' })
|
78 |
+
|
79 |
+
try {
|
80 |
+
const { images: urls, json, raw } = req.body
|
81 |
+
if (!urls) return res.status(400).json({ success: false, message: 'Payload \'images\' requires an array of urls' })
|
82 |
+
if (!Array.isArray(urls)) urls = [urls]
|
83 |
+
|
84 |
+
const bufferPDF = await toPDF(urls)
|
85 |
+
if (!bufferPDF.length) return res.status(400).json({ success: false, message: 'Can\'t convert to pdf' })
|
86 |
+
|
87 |
+
const fileName = randomName('.pdf')
|
88 |
+
await fs.promises.writeFile(`${tmpDir}/${fileName}`, bufferPDF)
|
89 |
+
|
90 |
+
const resultUrl = `https://${req.hostname}/file/${fileName}`
|
91 |
+
isTrue(json) ? res.json({ success: true, result: resultUrl }) : res[isTrue(raw) ? 'send' : 'redirect'](resultUrl)
|
92 |
+
} catch (e) {
|
93 |
+
console.log(e)
|
94 |
+
res.status(500).json({ error: true, message: getError(e) })
|
95 |
+
}
|
96 |
+
})
|
97 |
+
|
98 |
+
app.all(/^\/webp2(gif|mp4|png)/, async (req, res) => {
|
99 |
+
if (req.method !== 'POST') return res.status(405).json({ success: false, message: 'Method Not Allowed' })
|
100 |
+
|
101 |
+
try {
|
102 |
+
const { file, json, raw } = req.body
|
103 |
+
if (!file) return res.status(400).json({ success: false, message: 'Payload \'file\' requires base64 string' })
|
104 |
+
if (!isBase64(file)) return res.status(400).json({ success: false, message: 'Invalid base64 format' })
|
105 |
+
|
106 |
+
const type = req.params[0]
|
107 |
+
if (type === 'png') {
|
108 |
+
const fileName = randomName('.png')
|
109 |
+
const fileBuffer = await sharp(Buffer.from(file, 'base64')).png().toBuffer()
|
110 |
+
await fs.promises.writeFile(`${tmpDir}/${fileName}`, fileBuffer)
|
111 |
+
|
112 |
+
const resultUrl = `https://${req.hostname}/file/${fileName}`
|
113 |
+
isTrue(json) ? res.json({ success: true, result: resultUrl }) : res[isTrue(raw) ? 'send' : 'redirect'](resultUrl)
|
114 |
+
return
|
115 |
+
} else {
|
116 |
+
const fileName = randomName('.webp')
|
117 |
+
const filePath = `${tmpDir}/${fileName}`
|
118 |
+
await fs.promises.writeFile(filePath, Buffer.from(file, 'base64'))
|
119 |
+
|
120 |
+
const exec = util.promisify(cp.exec).bind(cp)
|
121 |
+
await exec(`convert ${filePath} ${filePath.replace('webp', 'gif')}`)
|
122 |
+
|
123 |
+
let resultUrl
|
124 |
+
if (type === 'gif') resultUrl = `https://${req.hostname}/file/${fileName.replace('webp', 'gif')}`
|
125 |
+
else {
|
126 |
+
await exec(`ffmpeg -i ${filePath.replace('webp', 'gif')} -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" ${filePath.replace('webp', 'mp4')}`)
|
127 |
+
resultUrl = `https://${req.hostname}/file/${fileName.replace('webp', 'mp4')}`
|
128 |
+
}
|
129 |
+
|
130 |
+
isTrue(json) ? res.json({ success: true, result: resultUrl }) : res[isTrue(raw) ? 'send' : 'redirect'](resultUrl)
|
131 |
+
}
|
132 |
+
} catch (e) {
|
133 |
+
console.log(e)
|
134 |
+
res.status(500).json({ error: true, message: getError(e) })
|
135 |
+
}
|
136 |
+
})
|
137 |
+
|
138 |
+
app.listen(7860, () => console.log('App running on port 7860'))
|
139 |
+
|
140 |
+
function formatSize(n) {
|
141 |
+
return bytes(+n, { unitSeparator: ' ' })
|
142 |
+
}
|
143 |
+
|
144 |
+
function getError(e) {
|
145 |
+
e = String(e)
|
146 |
+
return e !== '[object Object]' ? e : 'Internal Server Error'
|
147 |
+
}
|
148 |
+
|
149 |
+
function isBase64(str) {
|
150 |
+
try {
|
151 |
+
return btoa(atob(str)) === str
|
152 |
+
} catch {
|
153 |
+
return false
|
154 |
+
}
|
155 |
+
}
|
156 |
+
|
157 |
+
function isTrue(str) {
|
158 |
+
return [true, 'true'].includes(str)
|
159 |
+
}
|
160 |
+
|
161 |
+
function randomName(ext = '') {
|
162 |
+
return Math.random().toString(36).slice(2) + ext
|
163 |
+
}
|
164 |
+
|
165 |
+
function toPDF(urls) {
|
166 |
+
return new Promise(async (resolve, reject) => {
|
167 |
+
try {
|
168 |
+
const doc = new PDFDocument({ margin: 0, size: 'A4' })
|
169 |
+
const buffs = []
|
170 |
+
|
171 |
+
for (let x = 0; x < urls.length; x++) {
|
172 |
+
if (!/https:\/\//.test(urls[x])) continue
|
173 |
+
const url = new URL(urls[x])
|
174 |
+
let image = await fetch(url.toString(), { headers: { referer: url.origin } })
|
175 |
+
if (!image.ok) continue
|
176 |
+
|
177 |
+
const type = image.headers.get('content-type')
|
178 |
+
if (!/image/.test(type)) continue
|
179 |
+
image = Buffer.from(await image.arrayBuffer())
|
180 |
+
if (/(gif|webp)$/.test(type)) image = await sharp(image).png().toBuffer()
|
181 |
+
|
182 |
+
doc.image(image, 0, 0, { fit: [595.28, 841.89], align: 'center', valign: 'center' })
|
183 |
+
if (urls.length != x + 1) doc.addPage()
|
184 |
+
}
|
185 |
+
|
186 |
+
doc.on('data', (chunk) => buffs.push(chunk))
|
187 |
+
doc.on('end', () => resolve(Buffer.concat(buffs)))
|
188 |
+
doc.on('error', (err) => reject(err))
|
189 |
+
doc.end()
|
190 |
+
} catch (e) {
|
191 |
+
console.log(e)
|
192 |
+
reject(e)
|
193 |
+
}
|
194 |
+
})
|
195 |
+
}
|
package.json
CHANGED
@@ -5,6 +5,7 @@
|
|
5 |
"express": "*",
|
6 |
"morgan": "*",
|
7 |
"pdfkit": "*",
|
|
|
8 |
"sharp": "*"
|
9 |
}
|
10 |
}
|
|
|
5 |
"express": "*",
|
6 |
"morgan": "*",
|
7 |
"pdfkit": "*",
|
8 |
+
"serve-favicon": "*",
|
9 |
"sharp": "*"
|
10 |
}
|
11 |
}
|