Implemence pace based os download

This commit is contained in:
Juan Cruz Viotti 2014-12-02 11:08:22 -04:00
parent 0b078be0f3
commit ccf92411fe
6 changed files with 71 additions and 1 deletions

View File

@ -6,3 +6,4 @@ module.exports =
keys: require('./keys')
logs: require('./logs')
preferences: require('./preferences')
os: require('./os')

43
lib/actions/os.coffee Normal file
View File

@ -0,0 +1,43 @@
_ = require('lodash')
async = require('async')
path = require('path')
mkdirp = require('mkdirp')
url = require('url')
resin = require('../resin')
connection = require('../connection/connection')
exports.download = (id) ->
fileName = "#{id}-#{Date.now()}"
outputFile = path.join(resin.config.osDirectory, fileName)
async.waterfall [
(callback) ->
# We need to ensure this directory exists
mkdirp resin.config.osDirectory, (error) ->
return callback(error)
(callback) ->
params =
network: resin.cli.getArgument('network')
wifiSsid: resin.cli.getArgument('wifiSsid')
wifiKey: resin.cli.getArgument('wifiKey')
connection.parseConnectionParameters(params, callback)
(parameters, callback) ->
parameters.appId = id
query = url.format(query: parameters)
downloadUrl = url.resolve(resin.config.urls.download, query)
return callback(null, downloadUrl)
(downloadUrl, callback) ->
resin.ui.patterns.downloadFile(downloadUrl, outputFile, callback)
], (error) ->
resin.errors.handle(error) if error?
console.log "\n\nFinished downloading #{outputFile}"

View File

@ -121,6 +121,13 @@ resin.cli.addCommand
action: actions.logs.logs
permission: 'user'
# ---------- OS Module ----------
resin.cli.addCommand
command: 'os:download <id>'
description: 'download device OS'
action: actions.os.download
permission: 'user'
resin.data.prefix.set resin.config.dataPrefix, (error) ->
resin.errors.handle(error) if error?

View File

@ -30,5 +30,6 @@ config =
download: '/download'
config.pluginsDirectory = path.join(config.dataPrefix, 'plugins')
config.osDirectory = path.join(config.dataPrefix, 'os')
module.exports = config

View File

@ -1,5 +1,8 @@
async = require('async')
pace = require('pace')
fs = require('fs')
widgets = require('../widgets/widgets')
server = require('../../server/server')
exports.remove = (name, confirmAttribute, deleteFunction, outerCallback) ->
async.waterfall([
@ -15,3 +18,16 @@ exports.remove = (name, confirmAttribute, deleteFunction, outerCallback) ->
deleteFunction(callback)
], outerCallback)
exports.downloadFile = (url, dest, callback) ->
bar = null
server.request
method: 'GET'
url: url
pipe: fs.createWriteStream(dest)
, (error) ->
return callback(error)
, (state) ->
return if not state?
bar ?= pace(state.total)
bar.op(state.received)

View File

@ -52,6 +52,8 @@
"pluralize": "~1.1.0",
"indefinite-article": "0.0.2",
"pubnub": "~3.7.0",
"request-progress": "~0.3.1"
"request-progress": "~0.3.1",
"progress-bar": "~0.1.1",
"pace": "0.0.4"
}
}