-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathuploadFile.js
66 lines (64 loc) · 1.66 KB
/
uploadFile.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
import fetch from 'node-fetch'
import { FormData, Blob } from 'formdata-node'
import { fileTypeFromBuffer } from 'file-type'
/**
* Upload epheremal file to file.io
* `Expired in 1 day`
* `100MB Max Filesize`
* @param {Buffer} buffer File Buffer
*/
const fileIO = async buffer => {
const { ext, mime } = await fileTypeFromBuffer(buffer) || {}
let form = new FormData()
const blob = new Blob([buffer.toArrayBuffer()], { type: mime })
form.append('file', blob, 'tmp.' + ext)
let res = await fetch('https://file.io/?expires=1d', { // 1 Day Expiry Date
method: 'POST',
body: form
})
let json = await res.json()
if (!json.success) throw json
return json.link
}
/**
* Upload file to storage.restfulapi.my.id
* @param {Buffer|ReadableStream|(Buffer|ReadableStream)[]} inp File Buffer/Stream or Array of them
* @returns {string|null|(string|null)[]}
*/
const RESTfulAPI = async inp => {
let form = new FormData()
let buffers = inp
if (!Array.isArray(inp)) buffers = [inp]
for (let buffer of buffers) {
const blob = new Blob([buffer.toArrayBuffer()])
form.append('file', blob)
}
let res = await fetch('https://storage.restfulapi.my.id/upload', {
method: 'POST',
body: form
})
let json = await res.text()
try {
json = JSON.parse(json)
if (!Array.isArray(inp)) return json.files[0].url
return json.files.map(res => res.url)
} catch (e) {
throw json
}
}
/**
*
* @param {Buffer} inp
* @returns {Promise<string>}
*/
export default async function (inp) {
let err = false
for (let upload of [RESTfulAPI, fileIO]) {
try {
return await upload(inp)
} catch (e) {
err = e
}
}
if (err) throw err
}