aripbae commited on
Commit
bc93ab9
·
verified ·
1 Parent(s): c8223cf

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +54 -5
index.js CHANGED
@@ -1,17 +1,21 @@
 
1
  import express from 'express'
 
2
  import morgan from 'morgan'
3
  import pg from 'puppeteer-ghost'
4
  import * as prb from 'puppeteer-real-browser'
5
  import serveIndex from 'serve-index'
6
 
 
7
  import { tmpdir } from 'node:os'
8
  import { env } from 'node:process'
9
  import { setTimeout } from 'node:timers/promises'
10
  import { format } from 'node:util'
11
 
12
  const app = express()
 
13
 
14
- const limitSize = '200mb'
15
  app.set('json spaces', 2)
16
  app.use(express.json({ limit: limitSize }))
17
  app.use(express.urlencoded({
@@ -30,10 +34,55 @@ app.use(
30
  )
31
  )
32
 
33
- app.all(
34
- '/',
35
- (_, res) => res.json({ msg: 'Hello World' })
36
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  const executablePath = env.CHROME_BIN
39
  app.get('/ss', async (req, res) => {
 
1
+ import { format as formatBytes } from 'bytes'
2
  import express from 'express'
3
+ import { fileTypeFromBuffer } from 'file-type'
4
  import morgan from 'morgan'
5
  import pg from 'puppeteer-ghost'
6
  import * as prb from 'puppeteer-real-browser'
7
  import serveIndex from 'serve-index'
8
 
9
+ import { writeFile } from 'node:fs/promises'
10
  import { tmpdir } from 'node:os'
11
  import { env } from 'node:process'
12
  import { setTimeout } from 'node:timers/promises'
13
  import { format } from 'node:util'
14
 
15
  const app = express()
16
+ app.enable('trust proxy')
17
 
18
+ const limitSize = '1000mb'
19
  app.set('json spaces', 2)
20
  app.use(express.json({ limit: limitSize }))
21
  app.use(express.urlencoded({
 
34
  )
35
  )
36
 
37
+ const isBase64 = (str) => {
38
+ try {
39
+ return btoa(atob(str)) === str
40
+ } catch {
41
+ return false
42
+ }
43
+ }
44
+
45
+ app.all('/', async (req, res) => {
46
+ if (req.method !== 'POST') return res
47
+ .status(405)
48
+ .json({ msg: 'Hello World' })
49
+
50
+ const { file } = req.body
51
+ if (!isBase64(file)) return res
52
+ .status(400)
53
+ .json({ msg: 'Bad Request' })
54
+
55
+ const buffer = Buffer.from(file, 'base64')
56
+ const type = await fileTypeFromBuffer(buffer) ||
57
+ { mime: 'file', ext: 'bin' }
58
+
59
+ const name = format(
60
+ '%s/%s-%s.%s',
61
+ tmpDir,
62
+ type.mime.split('/')[0],
63
+ Math.random().toString(36).slice(2),
64
+ type.ext
65
+ )
66
+ await writeFile(name, buffer)
67
+
68
+ const bytes = buffer.length
69
+ res.json({
70
+ name: name.split('/').pop(),
71
+ size: {
72
+ bytes,
73
+ readable: formatBytes(
74
+ bytes,
75
+ { unitSeparator: ' ' }
76
+ )
77
+ },
78
+ type,
79
+ url: format(
80
+ '%s://%s',
81
+ req.protocol,
82
+ env.SPACE_HOST + name
83
+ )
84
+ })
85
+ }
86
 
87
  const executablePath = env.CHROME_BIN
88
  app.get('/ss', async (req, res) => {