akkun3704 commited on
Commit
64dcc49
·
verified ·
1 Parent(s): 178b776

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +21 -4
index.js CHANGED
@@ -18,6 +18,19 @@ app.use(express.json({ limit: limitSize }))
18
  app.use(express.urlencoded({ extended: true, limit: limitSize }))
19
 
20
  const tmpFolder = os.tmpdir()
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  app.use('/file', express.static(tmpFolder))
22
 
23
  app.all('/', (req, res) => {
@@ -25,6 +38,9 @@ app.all('/', (req, res) => {
25
  const used = process.memoryUsage()
26
  for (let key in used) status[key] = formatSize(used[key])
27
 
 
 
 
28
  const totalmem = os.totalmem()
29
  const freemem = os.freemem()
30
  status.memoryUsage = `${formatSize(totalmem - freemem)} / ${formatSize(totalmem)}`
@@ -77,7 +93,7 @@ app.all('/webp2png', async (req, res) => {
77
  }
78
  })
79
 
80
- app.all('/webp2mp4', async (req, res) => {
81
  if (!['POST'].includes(req.method)) return res.status(405).json({ success: false, message: 'Method Not Allowed' })
82
 
83
  try {
@@ -85,14 +101,15 @@ app.all('/webp2mp4', async (req, res) => {
85
  if (!(file && isBase64(file))) return res.json({ success: false, message: 'Payload body file must be filled in base64 format' })
86
 
87
  const fileBuffer = Buffer.from(file, 'base64')
88
- const fileName = `${Math.random().toString(36)}.webp`
89
  const filePath = `${tmpFolder}/${fileName}`
90
  await fs.promises.writeFile(filePath, fileBuffer)
91
 
92
  const exec = util.promisify(cp.exec).bind(cp)
93
- await exec(`convert ${filePath} ${filePath}.gif`)
94
- await exec(`ffmpeg -i ${filePath}.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" ${filePath.replace(/.webp|.gif/g, '')}.mp4`)
95
 
 
96
  res.send(`https://${req.get('host')}/file/${fileName.replace('.webp', '.mp4')}`)
97
  } catch (e) {
98
  console.log(e)
 
18
  app.use(express.urlencoded({ extended: true, limit: limitSize }))
19
 
20
  const tmpFolder = os.tmpdir()
21
+ app.use((req, res, next) => {
22
+ fs.readdirSync(tmpFolder).map(file => {
23
+ file = `${tmpFolder}/${file}`
24
+ let stats = fs.statSync(file)
25
+ if (!stats.isFile()) return
26
+ if (Date.now() - stats.mtimeMs >= 1000 * 60 * 30) {
27
+ fs.unlinkSync(file)
28
+ console.log('Deleted file', file)
29
+ }
30
+ })
31
+ next()
32
+ })
33
+
34
  app.use('/file', express.static(tmpFolder))
35
 
36
  app.all('/', (req, res) => {
 
38
  const used = process.memoryUsage()
39
  for (let key in used) status[key] = formatSize(used[key])
40
 
41
+ const disk = cp.execSync('du -sh').toString().split('M')[0]
42
+ status.diskUsage = `${disk} MB`
43
+
44
  const totalmem = os.totalmem()
45
  const freemem = os.freemem()
46
  status.memoryUsage = `${formatSize(totalmem - freemem)} / ${formatSize(totalmem)}`
 
93
  }
94
  })
95
 
96
+ app.all(['/webp2gif', '/webp2mp4'], async (req, res) => {
97
  if (!['POST'].includes(req.method)) return res.status(405).json({ success: false, message: 'Method Not Allowed' })
98
 
99
  try {
 
101
  if (!(file && isBase64(file))) return res.json({ success: false, message: 'Payload body file must be filled in base64 format' })
102
 
103
  const fileBuffer = Buffer.from(file, 'base64')
104
+ const fileName = `${Math.random().toString(36).slice(2)}.webp`
105
  const filePath = `${tmpFolder}/${fileName}`
106
  await fs.promises.writeFile(filePath, fileBuffer)
107
 
108
  const exec = util.promisify(cp.exec).bind(cp)
109
+ await exec(`convert ${filePath} ${filePath.replace('.webp', '.gif')}`)
110
+ if (/gif/.test(req.path)) return res.send(`https://${req.get('host')}/file/${fileName.replace('.webp', '.gif')}`)
111
 
112
+ 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|.gif/g, '')}.mp4`)
113
  res.send(`https://${req.get('host')}/file/${fileName.replace('.webp', '.mp4')}`)
114
  } catch (e) {
115
  console.log(e)