Permalink
Browse files

Replaced snekfetch with node-fetch

  • Loading branch information...
BobbyWibowo committed Sep 23, 2018
1 parent db015c8 commit 30c011ce5cb92c15b8535c83699fb39303442477
Showing with 71 additions and 64 deletions.
  1. +49 −46 controllers/uploadController.js
  2. +17 −13 controllers/utilsController.js
  3. +1 −1 package.json
  4. +4 −4 yarn.lock
@@ -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 }
try {
const fetchHead = await fetch(url, { method: 'HEAD' })
if (fetchHead.status !== 200) {
return erred(`${fetchHead.status} ${fetchHead.statusText}`)
}
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 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.')
}
const download = await snekfetch.get(url)
.catch(error => error)
if (download.status !== 200) { return erred(download.toString()) }
if (!download) { return }
const fetchFile = await fetch(url)
if (fetchFile.status !== 200) {
return erred(`${fetchHead.status} ${fetchHead.statusText}`)
}
const length = uploadsController.getFileNameLength(req)
const name = await uploadsController.getUniqueRandomName(length, extension)
.catch(erred)
if (!name) { return }
const file = await fetchFile.buffer()
const destination = path.join(uploadsDir, name)
fs.writeFile(destination, download.body, async error => {
if (error) { return erred(error) }
const length = uploadsController.getFileNameLength(req)
const name = await uploadsController.getUniqueRandomName(length, extension)
const data = {
filename: name,
originalname: original,
mimetype: download.headers['content-type'].split(';')[0] || '',
size,
albumid
}
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
}
infoMap.push({
path: destination,
data
})
infoMap.push({
path: destination,
data
})
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) }
}
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 }
const result = await uploadsController.formatInfoMap(req, res, user, infoMap)
.catch(erred)
if (!result) { return }
uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
}
})
uploadsController.processFilesForDisplay(req, res, result.files, result.existingFiles)
}
})
} catch (error) { erred(error) }
}
}
@@ -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
@@ -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

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 30c011c

Please sign in to comment.