2014-12-02 15:08:22 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
async = require('async')
|
|
|
|
path = require('path')
|
|
|
|
mkdirp = require('mkdirp')
|
2014-12-22 19:40:57 +00:00
|
|
|
ProgressBar = require('progress')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2014-12-22 16:41:14 +00:00
|
|
|
log = require('../log/log')
|
2014-12-12 21:20:29 +00:00
|
|
|
permissions = require('../permissions/permissions')
|
2014-12-22 16:47:12 +00:00
|
|
|
errors = require('../errors/errors')
|
2014-12-22 18:50:54 +00:00
|
|
|
cache = require('../cache/cache')
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
exports.download = (params, options) ->
|
2015-01-08 13:55:01 +00:00
|
|
|
|
|
|
|
# TODO: Evaluate if ConnectionParams is a good name for this object
|
|
|
|
# as it includes an application id, which is not connection related
|
|
|
|
# Maybe we should move appId outside this class?
|
|
|
|
connectionParams = new resin.connection.ConnectionParams
|
2014-12-12 21:20:29 +00:00
|
|
|
network: options.network
|
|
|
|
wifiSsid: options.ssid
|
|
|
|
wifiKey: options.key
|
2015-01-08 13:55:01 +00:00
|
|
|
appId: params.id
|
|
|
|
|
|
|
|
# TODO: Change cache.generateCacheName to accept a ConnectionParams instance
|
|
|
|
# to avoid the complication of having to omit it from the object and pass
|
|
|
|
# as another parameter
|
|
|
|
fileName = cache.generateCacheName(params.id, _.omit(connectionParams, 'appId'))
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
outputFile = options.output or path.join(resin.settings.get('directories.os'), fileName)
|
2014-12-02 15:08:22 +00:00
|
|
|
|
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
|
|
|
|
# We need to ensure this directory exists
|
2014-12-02 15:53:34 +00:00
|
|
|
mkdirp path.dirname(outputFile), (error) ->
|
2014-12-02 15:08:22 +00:00
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
|
2014-12-22 19:40:57 +00:00
|
|
|
bar = null
|
|
|
|
received = 0
|
|
|
|
|
2015-01-08 13:55:01 +00:00
|
|
|
resin.models.os.download connectionParams, outputFile, callback, (state) ->
|
2014-12-22 19:40:57 +00:00
|
|
|
|
|
|
|
# TODO: Allow quieting this progress bar
|
|
|
|
bar ?= new ProgressBar 'Downloading device OS [:bar] :percent :etas',
|
|
|
|
complete: '='
|
|
|
|
incomplete: ' '
|
|
|
|
width: 40
|
|
|
|
total: state.total
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2014-12-22 19:40:57 +00:00
|
|
|
return if bar.complete or not state?
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2014-12-22 19:40:57 +00:00
|
|
|
bar.tick(state.received - received)
|
|
|
|
received = state.received
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2015-01-09 15:22:18 +00:00
|
|
|
], errors.handleCallback ->
|
2014-12-22 16:41:14 +00:00
|
|
|
log.info("\nFinished downloading #{outputFile}")
|