Fixed URL uploads

I forgot that getting rid of the old hashing system would break this
due to the lack of hashing.
So, fixed that.
This commit is contained in:
Bobby Wibowo 2020-06-01 12:23:15 +07:00
parent 5e5d5c5647
commit 7f58d80cff
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 32 additions and 14 deletions

View File

@ -38,22 +38,22 @@ DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
const finalPath = path.join(destination, filename)
const outStream = fs.createWriteStream(finalPath)
file.stream.pipe(outStream)
let hash = null
if (!file._ischunk) {
hash = blake3.createHash()
file.stream.on('data', d => hash.update(d))
const onerror = function (err) {
hash.dispose()
cb(err)
}
file.stream.on('error', onerror)
outStream.on('error', onerror)
file.stream.on('error', onerror)
file.stream.on('data', d => hash.update(d))
} else {
outStream.on('error', cb)
}
file.stream.pipe(outStream)
outStream.on('finish', function () {
cb(null, {
destination,

View File

@ -333,29 +333,47 @@ self.actuallyUploadUrls = async (req, res, user, albumid, age) => {
.replace(/{url}/g, encodeURIComponent(url))
.replace(/{url-noprot}/g, encodeURIComponent(url.replace(/^https?:\/\//, '')))
// Limit max response body size with maximum allowed size
const fetchFile = await fetch(url, { size: urlMaxSizeBytes })
if (fetchFile.status !== 200)
throw `${fetchFile.status} ${fetchFile.statusText}`
const headers = fetchFile.headers
const file = await fetchFile.buffer()
const length = self.parseFileIdentifierLength(req.headers.filelength)
const name = await self.getUniqueRandomName(length, extname)
const destination = path.join(paths.uploads, name)
await paths.writeFile(destination, file)
const outStream = fs.createWriteStream(destination)
const hash = blake3.createHash()
// Push to array early, so regardless of its progress it will be deleted on errors
downloaded.push(destination)
// Limit max response body size with maximum allowed size
const fetchFile = await fetch(url, { size: urlMaxSizeBytes })
.then(res => new Promise((resolve, reject) => {
if (res.status === 200) {
const onerror = error => {
hash.dispose()
reject(error)
}
outStream.on('error', onerror)
res.body.on('error', onerror)
res.body.on('data', d => hash.update(d))
res.body.pipe(outStream)
outStream.on('finish', () => resolve(res))
} else {
resolve(res)
}
}))
if (fetchFile.status !== 200)
throw `${fetchFile.status} ${fetchFile.statusText}`
infoMap.push({
path: destination,
data: {
filename: name,
originalname: original,
extname,
mimetype: headers.get('content-type').split(';')[0] || '',
size: file.byteLength,
mimetype: fetchFile.headers.get('content-type').split(';')[0] || '',
size: outStream.bytesWritten,
hash: hash.digest('hex'),
albumid,
age
}