various upload fixes

- recognize 201 as success (dataType must not be 'json')
- support uploading non-notebook files (b64-encoded)
This commit is contained in:
MinRK 2014-07-26 12:53:22 -07:00
parent 3c26b079f0
commit fb89646a29

View File

@ -75,30 +75,27 @@ define([
} }
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
var f = files[i]; var f = files[i];
var reader = new FileReader();
reader.readAsText(f);
var name_and_ext = utils.splitext(f.name); var name_and_ext = utils.splitext(f.name);
var file_ext = name_and_ext[1]; var file_ext = name_and_ext[1];
var reader = new FileReader();
if (file_ext === '.ipynb') { if (file_ext === '.ipynb') {
var item = that.new_item(0); reader.readAsText(f);
item.addClass('new-file');
that.add_name_input(f.name, item);
// Store the notebook item in the reader so we can use it later
// to know which item it belongs to.
$(reader).data('item', item);
reader.onload = function (event) {
var nbitem = $(event.target).data('item');
that.add_notebook_data(event.target.result, nbitem);
that.add_upload_button(nbitem);
};
} else { } else {
var dialog_body = 'Uploaded notebooks must be .ipynb files'; // read non-notebook files as binary
dialog.modal({ reader.readAsArrayBuffer(f);
title : 'Invalid file type',
body : dialog_body,
buttons : {'OK' : {'class' : 'btn-primary'}}
});
} }
var item = that.new_item(0);
item.addClass('new-file');
that.add_name_input(f.name, item);
// Store the list item in the reader so we can use it later
// to know which item it belongs to.
$(reader).data('item', item);
reader.onload = function (event) {
var item = $(event.target).data('item');
that.add_file_data(event.target.result, item);
that.add_upload_button(item);
};
} }
// Replace the file input form wth a clone of itself. This is required to // Replace the file input form wth a clone of itself. This is required to
// reset the form. Otherwise, if you upload a file, delete it and try to // reset the form. Otherwise, if you upload a file, delete it and try to
@ -268,16 +265,16 @@ define([
item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width'); item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
item.find(".item_name").empty().append( item.find(".item_name").empty().append(
$('<input/>') $('<input/>')
.addClass("nbname_input") .addClass("filename_input")
.attr('value', utils.splitext(name)[0]) .attr('value', name)
.attr('size', '30') .attr('size', '30')
.attr('type', 'text') .attr('type', 'text')
); );
}; };
NotebookList.prototype.add_notebook_data = function (data, item) { NotebookList.prototype.add_file_data = function (data, item) {
item.data('nbdata', data); item.data('filedata', data);
}; };
@ -314,8 +311,8 @@ define([
click(function (e) { click(function (e) {
// $(this) is the button that was clicked. // $(this) is the button that was clicked.
var that = $(this); var that = $(this);
// We use the nbname and notebook_id from the parent notebook_item element's // We use the filename from the parent list_item element's
// data because the outer scopes values change as we iterate through the loop. // data because the outer scope's values change as we iterate through the loop.
var parent_item = that.parents('div.list_item'); var parent_item = that.parents('div.list_item');
var name = parent_item.data('name'); var name = parent_item.data('name');
var message = 'Are you sure you want to permanently delete the file: ' + name + '?'; var message = 'Are you sure you want to permanently delete the file: ' + name + '?';
@ -354,32 +351,55 @@ define([
}; };
NotebookList.prototype.add_upload_button = function (item) { NotebookList.prototype.add_upload_button = function (item, type) {
var that = this; var that = this;
var upload_button = $('<button/>').text("Upload") var upload_button = $('<button/>').text("Upload")
.addClass('btn btn-primary btn-xs upload_button') .addClass('btn btn-primary btn-xs upload_button')
.click(function (e) { .click(function (e) {
var nbname = item.find('.item_name > input').val();
if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") {
nbname = nbname + ".ipynb";
}
var path = that.notebook_path; var path = that.notebook_path;
var nbdata = item.data('nbdata'); var filename = item.find('.item_name > input').val();
var content_type = 'application/json'; var filedata = item.data('filedata');
var format = 'text';
if (filedata instanceof ArrayBuffer) {
// base64-encode binary file data
var bytes = '';
var buf = new Uint8Array(filedata);
var nbytes = buf.byteLength;
for (var i=0; i<nbytes; i++) {
bytes += String.fromCharCode(buf[i]);
}
filedata = btoa(bytes);
format = 'base64';
}
var model = { var model = {
path: path, path: path,
name: nbname, name: filename
content : JSON.parse(nbdata),
type : 'notebook'
}; };
var name_and_ext = utils.splitext(filename);
var file_ext = name_and_ext[1];
var content_type;
if (file_ext === '.ipynb') {
model.type = 'notebook';
model.format = 'json';
model.content = JSON.parse(filedata);
content_type = 'application/json';
} else {
model.type = 'file';
model.format = format;
model.content = filedata;
content_type = 'application/octet-stream';
}
var filedata = item.data('filedata');
var settings = { var settings = {
processData : false, processData : false,
cache : false, cache : false,
type : 'PUT', type : 'PUT',
dataType : 'json',
data : JSON.stringify(model), data : JSON.stringify(model),
headers : {'Content-Type': content_type}, headers : {'Content-Type': content_type},
success : function (data, status, xhr) { success : function (data, status, xhr) {
item.removeClass('new-file');
that.add_link(model, item); that.add_link(model, item);
that.add_delete_button(item); that.add_delete_button(item);
}, },
@ -390,7 +410,7 @@ define([
that.base_url, that.base_url,
'api/contents', 'api/contents',
that.notebook_path, that.notebook_path,
nbname filename
); );
$.ajax(url, settings); $.ajax(url, settings);
return false; return false;
@ -398,7 +418,6 @@ define([
var cancel_button = $('<button/>').text("Cancel") var cancel_button = $('<button/>').text("Cancel")
.addClass("btn btn-default btn-xs") .addClass("btn btn-default btn-xs")
.click(function (e) { .click(function (e) {
console.log('cancel click');
item.remove(); item.remove();
return false; return false;
}); });