1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-03 17:59:28 +02:00

Implement APK uploads using the new storage system. Installation from URL still does not work, and dropping the file on the screen may not work either.

This commit is contained in:
Simo Kinnunen 2014-05-22 13:33:38 +09:00
parent 1db48e9fcb
commit 41ed33f5c4
17 changed files with 389 additions and 336 deletions

67
lib/util/download.js Normal file
View file

@ -0,0 +1,67 @@
var fs = require('fs')
var Promise = require('bluebird')
var request = require('request')
var progress = require('request-progress')
var temp = require('temp')
module.exports = function download(url, options) {
var resolver = Promise.defer()
var path = temp.path(options)
function errorListener(err) {
resolver.reject(err)
}
function progressListener(state) {
if (state.total !== null) {
resolver.progress({
lengthComputable: true
, loaded: state.received
, total: state.total
})
}
else {
resolver.progress({
lengthComputable: false
, loaded: state.received
, total: state.received
})
}
}
function closeListener() {
resolver.resolve({
path: path
})
}
resolver.progress({
percent: 0
})
try {
var req = progress(request(url), {
throttle: 100 // Throttle events, not upload speed
})
.on('progress', progressListener)
resolver.promise.finally(function() {
req.removeListener('progress', progressListener)
})
var save = req.pipe(fs.createWriteStream(path))
.on('error', errorListener)
.on('close', closeListener)
resolver.promise.finally(function() {
save.removeListener('error', errorListener)
save.removeListener('close', closeListener)
})
}
catch (err) {
resolver.reject(err)
}
return resolver.promise
}