Move project saving code to gui.js

Give it a small refactor, fix size checking.
pull/89/head
Michael Ball 2019-01-23 23:54:33 -08:00
rodzic 204f725437
commit 1fdded7fe5
2 zmienionych plików z 92 dodań i 76 usunięć

Wyświetl plik

@ -51,6 +51,9 @@ Cloud.prototype.init = function () {
this.username = null;
};
// Projects larger than this are rejected.
Cloud.MAX_FILE_SIZE = 10 * 2e20;
Cloud.prototype.knownDomains = {
'Snap!Cloud' : 'https://cloud.snap.berkeley.edu',
'Snap!Cloud (cs10)' : 'https://snap-cloud.cs10.org',
@ -146,6 +149,8 @@ Cloud.prototype.genericError = function () {
throw new Error(Cloud.genericErrorMessage);
};
// Low level functionality
Cloud.prototype.request = function (
@ -397,79 +402,17 @@ Cloud.prototype.resendVerification = function (username, onSuccess, onError) {
// Projects
Cloud.prototype.saveProject = function (ide, onSuccess, onError) {
Cloud.prototype.saveProject = function (projectName, body, onSuccess, onError) {
var myself = this;
this.checkCredentials(
function (username) {
if (username) {
var xml = ide.serializer.serialize(ide.stage),
thumbnail = normalizeCanvas(
ide.stage.thumbnail(
SnapSerializer.prototype.thumbnailSize
)).toDataURL(),
body, mediaSize, size;
ide.serializer.isCollectingMedia = true;
body = {
notes: ide.projectNotes,
xml: xml,
media: ide.hasChangedMedia ?
ide.serializer.mediaXML(ide.projectName) : null,
thumbnail: thumbnail,
remixID: ide.stage.remixID
};
ide.serializer.isCollectingMedia = false;
ide.serializer.flushMedia();
mediaSize = body.media ? body.media.length : 0;
size = body.xml.length + mediaSize;
if (mediaSize > 10485760) {
new DialogBoxMorph().inform(
'Snap!Cloud - Cannot Save Project',
'The media inside this project exceeds 10 MB.\n' +
'Please reduce the size of costumes or sounds.\n',
ide.world(),
ide.cloudIcon(null, new Color(180, 0, 0))
);
throw new Error('Project media exceeds 10 MB size limit');
}
// check if serialized data can be parsed back again
try {
ide.serializer.parse(body.xml);
} catch (err) {
ide.showMessage(
'Serialization of program data failed:\n' + err
);
throw new Error(
'Serialization of program data failed:\n' + err
);
}
if (body.media !== null) {
try {
ide.serializer.parse(body.media);
} catch (err) {
ide.showMessage(
'Serialization of media failed:\n' + err
);
throw new Error(
'Serialization of media failed:\n' + err
);
}
}
ide.serializer.isCollectingMedia = false;
ide.serializer.flushMedia();
ide.showMessage(
'Uploading ' + Math.round(size / 1024) + ' KB...'
);
myself.request(
'POST',
'/projects/' +
encodeURIComponent(username) +
'/' +
encodeURIComponent(ide.projectName),
encodeURIComponent(projectName),
onSuccess,
onError,
'Project could not be saved',

Wyświetl plik

@ -5583,17 +5583,90 @@ IDE_Morph.prototype.logout = function () {
);
};
IDE_Morph.prototype.saveProjectToCloud = function (name) {
var myself = this;
if (name) {
this.showMessage('Saving project\nto the cloud...');
this.setProjectName(name);
this.cloud.saveProject(
this,
function () {myself.showMessage('saved.', 2); },
this.cloudError()
IDE_Morph.prototype.buildProjectRequest = function () {
var xml = this.serializer.serialize(this.stage),
thumbnail = normalizeCanvas(
this.stage.thumbnail(
SnapSerializer.prototype.thumbnailSize
)).toDataURL(),
body;
this.serializer.isCollectingMedia = true;
body = {
notes: this.projectNotes,
xml: xml,
media: this.hasChangedMedia ?
this.serializer.mediaXML(this.projectName) : null,
thumbnail: thumbnail,
remixID: this.stage.remixID
};
this.serializer.isCollectingMedia = false;
this.serializer.flushMedia();
return body;
}
IDE_Morph.prototype.verifyProjectContents = function (body) {
var encodedBody = JSON.stringify(body);
if (ecodedBody.length > Cloud.MAX_FILE_SIZE) {
new DialogBoxMorph().inform(
'Snap!Cloud - Cannot Save Project',
'The media inside this project exceeds 10 MB.\n' +
'Please reduce the size of costumes or sounds.\n',
this.world(),
this.cloudIcon(null, new Color(180, 0, 0))
);
throw new Error('Project media exceeds 10 MB size limit');
}
// check if serialized data can be parsed back again
try {
this.serializer.parse(body.xml);
} catch (err) {
this.showMessage(
'Serialization of program data failed:\n' + err
);
throw new Error(
'Serialization of program data failed:\n' + err
);
}
if (body.media !== null) {
try {
this.serializer.parse(body.media);
} catch (err) {
this.showMessage(
'Serialization of media failed:\n' + err
);
throw new Error(
'Serialization of media failed:\n' + err
);
}
}
this.serializer.isCollectingMedia = false;
this.serializer.flushMedia();
return size;
}
IDE_Morph.prototype.saveProjectToCloud = function (name) {
var projectBody, projectSize;
if (!name) {return; }
this.showMessage('Saving project\nto the cloud...');
this.setProjectName(name);
projectBody = this.buildProjectRequest();
projectSize = this.verifyProjectSize(projectBody);
if (!projectSize) {return; }
this.showMessage(
'Uploading ' + Math.round(projectSize / 1024) + ' KB...'
);
this.cloud.saveProject(
name,
projectBody,
function () {myself.showMessage('saved.', 2); },
this.cloudError()
);
};
IDE_Morph.prototype.exportProjectMedia = function (name) {
@ -5804,11 +5877,11 @@ IDE_Morph.prototype.getURL = function (url, callback, responseType) {
async = callback instanceof Function,
myself = this,
rsp;
if (async) {
request.responseType = responseType || 'text';
if (async) {
request.responseType = responseType || 'text';
}
rsp = (!async || request.responseType === 'text') ? 'responseText'
: 'response';
: 'response';
try {
request.open('GET', url, async);
if (async) {