Replaced snekfetch with node-fetch

This commit is contained in:
Bobby Wibowo 2018-09-23 23:28:15 +07:00
parent db015c87aa
commit 30c011ce5c
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
4 changed files with 74 additions and 67 deletions

View File

@ -1,11 +1,11 @@
const config = require('./../config')
const crypto = require('crypto')
const db = require('knex')(config.database)
const fetch = require('node-fetch')
const fs = require('fs')
const multer = require('multer')
const path = require('path')
const randomstring = require('randomstring')
const snekfetch = require('snekfetch')
const utils = require('./utilsController')
const uploadsController = {}
@ -220,60 +220,63 @@ uploadsController.actuallyUploadByUrl = async (req, res, user, albumid) => {
return erred(`${extension.substr(1).toUpperCase()} files are not permitted due to security reasons.`)
}
const head = await snekfetch.head(url)
.catch(error => error)
if (head.status !== 200) { return erred(head.toString()) }
if (!head) { return }
const size = parseInt(head.headers['content-length'])
if (isNaN(size)) {
return erred('URLs with missing Content-Length HTTP header are not supported.')
}
if (size > urlMaxSizeBytes) {
return erred('File too large.')
}
const download = await snekfetch.get(url)
.catch(error => error)
if (download.status !== 200) { return erred(download.toString()) }
if (!download) { return }
const length = uploadsController.getFileNameLength(req)
const name = await uploadsController.getUniqueRandomName(length, extension)
.catch(erred)
if (!name) { return }
const destination = path.join(uploadsDir, name)
fs.writeFile(destination, download.body, async error => {
if (error) { return erred(error) }
const data = {
filename: name,
originalname: original,
mimetype: download.headers['content-type'].split(';')[0] || '',
size,
albumid
try {
const fetchHead = await fetch(url, { method: 'HEAD' })
if (fetchHead.status !== 200) {
return erred(`${fetchHead.status} ${fetchHead.statusText}`)
}
infoMap.push({
path: destination,
data
})
const headers = fetchHead.headers
const size = parseInt(headers.get('content-length'))
if (isNaN(size)) {
return erred('URLs with missing Content-Length HTTP header are not supported.')
}
if (size > urlMaxSizeBytes) {
return erred('File too large.')
}
iteration++
if (iteration === urls.length) {
if (config.uploads.scan && config.uploads.scan.enabled) {
const scan = await uploadsController.scanFiles(req, infoMap)
if (scan) { return erred(scan) }
const fetchFile = await fetch(url)
if (fetchFile.status !== 200) {
return erred(`${fetchHead.status} ${fetchHead.statusText}`)
}
const file = await fetchFile.buffer()
const length = uploadsController.getFileNameLength(req)
const name = await uploadsController.getUniqueRandomName(length, extension)
const destination = path.join(uploadsDir, name)
fs.writeFile(destination, file, async error => {
if (error) { return erred(error) }
const data = {
filename: name,
originalname: original,
mimetype: headers.get('content-type').split(';')[0] || '',
size,
albumid
}
const result = await uploadsController.formatInfoMap(req, res, user, infoMap)
.catch(erred)
if (!result) { return }
infoMap.push({
path: destination,
data
})
uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
}
})
iteration++
if (iteration === urls.length) {
if (config.uploads.scan && config.uploads.scan.enabled) {
const scan = await uploadsController.scanFiles(req, infoMap)
if (scan) { return erred(scan) }
}
const result = await uploadsController.formatInfoMap(req, res, user, infoMap)
.catch(erred)
if (!result) { return }
uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
}
})
} catch (error) { erred(error) }
}
}

View File

@ -1,10 +1,10 @@
const config = require('./../config')
const db = require('knex')(config.database)
const fetch = require('node-fetch')
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const gm = require('gm')
const path = require('path')
const snekfetch = require('snekfetch')
const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
@ -259,19 +259,23 @@ utilsController.purgeCloudflareCache = async names => {
return url
})
const purge = await snekfetch
.post(`https://api.cloudflare.com/client/v4/zones/${config.cloudflare.zoneId}/purge_cache`)
.set({
'X-Auth-Email': config.cloudflare.email,
'X-Auth-Key': config.cloudflare.apiKey
})
.send({ files: names.concat(thumbs) })
.catch(error => error)
try {
const url = `https://api.cloudflare.com/client/v4/zones/${config.cloudflare.zoneId}/purge_cache`
const fetchPurge = await fetch(url, {
method: 'POST',
body: JSON.stringify({ files: names.concat(thumbs) }),
headers: {
'Content-Type': 'application/json',
'X-Auth-Email': config.cloudflare.email,
'X-Auth-Key': config.cloudflare.apiKey
}
}).then(res => res.json())
if (!purge.body) {
console.error(`CF: ${purge.toString()}`)
} else if (!purge.body.success && purge.body.errors) {
purge.body.errors.forEach(error => console.error(`CF: ${error.code}: ${error.message}`))
if (fetchPurge.errors) {
fetchPurge.errors.forEach(error => console.error(`CF: ${error.code}: ${error.message}`))
}
} catch (error) {
console.error(`CF: ${error.toString()}`)
}
}

View File

@ -32,9 +32,9 @@
"jszip": "^3.1.5",
"knex": "^0.14.6",
"multer": "^1.3.0",
"node-fetch": "^2.2.0",
"nunjucks": "^3.1.2",
"randomstring": "^1.1.5",
"snekfetch": "^3.6.4",
"sqlite3": "^4.0.0"
},
"devDependencies": {

View File

@ -1803,6 +1803,10 @@ nocache@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/nocache/-/nocache-2.0.0.tgz#202b48021a0c4cbde2df80de15a17443c8b43980"
node-fetch@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
node-pre-gyp@0.9.1, node-pre-gyp@^0.9.0, node-pre-gyp@~0.9.0:
version "0.9.1"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.9.1.tgz#f11c07516dd92f87199dbc7e1838eab7cd56c9e0"
@ -2444,10 +2448,6 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"
snekfetch@^3.6.4:
version "3.6.4"
resolved "https://registry.yarnpkg.com/snekfetch/-/snekfetch-3.6.4.tgz#d13e80a616d892f3d38daae4289f4d258a645120"
source-map-resolve@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.1.tgz#7ad0f593f2281598e854df80f19aae4b92d7a11a"