Skip to content

Commit

Permalink
better passing of mapid and dirty state for initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
camilleanne committed Jul 15, 2014
1 parent bf3b930 commit d8bcb95
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
20 changes: 13 additions & 7 deletions app/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ window.Export = function(templates, source, job) {
this.poll();
};
Exporter.prototype.refresh = function() {
if (source.mapid) {
if (!this.model.get('update')) $('.js-upload').html('Already Uploaded').addClass('disabled').prop('title', source.mapid);
else $('.js-upload').html('Upload Update').removeClass('disabled').prop('title', source.mapid);
} else {
$('.js-upload').html('Upload').prop('title', null);
}
if (!this.model.get('progress')) {
var pct = '100.0';
var spd = 0;
Expand All @@ -51,7 +45,7 @@ window.Export = function(templates, source, job) {
var pct = this.model.get('progress').percentage || 0;
var spd = this.model.get('progress').delta || 0;
$('.js-cancel').html('Cancel ' + this.model.get('type'));
if (this.model.get('type') === 'export') this.model.set({update: true});
if (this.model.get('type') === 'export') this.model.set({dirty: true});
$('body').removeClass('stat').addClass('task');
}
var pctel = this.$('.percent');
Expand All @@ -68,13 +62,24 @@ window.Export = function(templates, source, job) {
this.model.get('type') === 'export' ?
this.$('.speed').text(spd + ' tiles/sec') :
this.$('.speed').text(templates.exportsize(spd * 10) + '/sec');

source.mapid = this.model.get('mapid') || source.mapid;
if (source.mapid) {
$('.js-mapid').html(source.mapid);
console.log(this.model.get('dirty'))
if (!this.model.get('dirty')) $('.js-upload').html('Already Uploaded').addClass('disabled').prop('title', source.mapid);
else $('.js-upload').html('Upload Update').removeClass('disabled').prop('title', source.mapid);
} else {
$('.js-upload').html('Upload').prop('title', null);
}
};
Exporter.prototype.recache = function() {
var view = this;
if (this.model.get('type') != 'export') {
job.type = 'export';
this.model = new Job(job);
}
this.model.set({dirty: true});

_(this).bindAll('poll', 'refresh');
this.model.on('change', this.refresh);
Expand All @@ -89,6 +94,7 @@ window.Export = function(templates, source, job) {
job.type = 'upload';
job.progress = null;
this.model = new Upload(job);
this.model.set({dirty: false});

_(this).bindAll('poll', 'refresh');
this.model.on('change', this.refresh);
Expand Down
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,19 @@ app.all('/upload.json', function(req, res, next) {
}
});

source.info(req.query.id, function(err, info) {
if (err) return next(err);
source.upload({
id: req.query.id,
oauth: tm.db.get('oauth')
}, false, function(err, job){
if (err && err.code) {
res.send(err.code, err.message);
} else if (err) {
next(err);
} else {
res.send(job);
}
});
source.upload({
id: req.query.id,
oauth: tm.db.get('oauth')
}, false, function(err, job){
if (err && err.code) {
res.send(err.code, err.message);
} else if (err) {
next(err);
} else {
res.send(job);
}
});

});

app.get('/source.xml', middleware.source, function(req, res, next) {
Expand Down
19 changes: 9 additions & 10 deletions lib/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var deflayer = {
};

var cache = {};
var update = false;
var dirty = false;

module.exports = source;
tilelive.protocols['mapbox:'] = source;
Expand Down Expand Up @@ -437,7 +437,7 @@ source.mbtiles = function(id, force, callback) {
if (task.get()) return callback(new Error('Active task in progress'));

// if redoing export, allow upload to be redone as well
update = true;
dirty = true;
task.set(source.mbtilesExport(id));

callback(null, task.get());
Expand Down Expand Up @@ -546,7 +546,7 @@ source.upload = function(data, force, callback) {
source.toHash(info.id, function(err, file) {
if (err) return callback(err);
if (force) {
update = false;
dirty = true;
data.mapid = setMapid(data, info);
startUpload(file, info);
} else {
Expand All @@ -555,9 +555,7 @@ source.upload = function(data, force, callback) {
// if it doesn't exist, continue with upload
source.checkMapid(info.mapid, function(err, mapExists){
if (err) return callback(err);
if (!mapExists) {
data.mapid = setMapid(data, info);
}
if (!mapExists) data.mapid = setMapid(data, info);
startUpload(file, info);
});
} else {
Expand All @@ -571,9 +569,10 @@ source.upload = function(data, force, callback) {

function startUpload(file, info) {
// Upload to mapbox.com complete, and no update necessary
if (!data.mapid && update === false) return callback(null, new task.Done(data.id, 'upload', '/mbtiles?id=' + data.id, 1000));
if (!data.mapid && dirty === false) return callback(null, new task.Done(data.id, 'upload', '/mbtiles?id=' + data.id, fs.statSync(file).size, info.mapid));
// if the source has be re-exported, update flag is tripped
if (update) data.mapid = info.mapid;
info.mapid = info.mapid || setMapid(data, info);
if (dirty) data.mapid = info.mapid;

// Check before setting creating task that there is no
// active task. Remaining calls beyond this point are sync
Expand Down Expand Up @@ -641,9 +640,9 @@ source.mbtilesUpload = function(data, info, file, callback){

function dataSaved(info, prog, callback) {
// save mapid to data.yml
if (update === false) info.mapid = data.mapid;
if (dirty === false) info.mapid = data.mapid;
dirty = false;
source.save(info, function(){
update = false;
prog.emit('mapid saved');
});
}
Expand Down
9 changes: 6 additions & 3 deletions lib/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ Task.prototype.toJSON = function() {
type: this.type,
progress: this.progress.progress(),
size: null,
url: null
url: null,
mapid: null
};
};

// A Done object, a record of the completed result of a Task.
function Done(id, type, url, size) {
function Done(id, type, url, size, mapid) {
if (typeof id !== 'string') throw new Error('Done id must be a string');
if (type !== 'export' && type !== 'upload') throw new Error('Done type must be one of [export, upload]');
if (typeof url !== 'string') throw new Error('Done url must be a string');
Expand All @@ -68,6 +69,7 @@ function Done(id, type, url, size) {
this.url = url;
this.size = size;
this.progress = null;
this.mapid = mapid || null;
}

Done.prototype.toJSON = function() {
Expand All @@ -76,7 +78,8 @@ Done.prototype.toJSON = function() {
type: this.type,
progress: null,
size: this.size,
url: this.url
url: this.url,
mapid: this.mapid || null
};
};

0 comments on commit d8bcb95

Please sign in to comment.