Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually upload projects using xhr, rather than scratch-storage #4015

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions src/lib/project-saver-hoc.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import bindAll from 'lodash.bindall';
import React from 'react';
import PropTypes from 'prop-types';
import queryString from 'query-string';
import {connect} from 'react-redux';
import VM from 'scratch-vm';
import xhr from 'xhr';

import log from '../lib/log';
import storage from '../lib/storage';
Expand Down Expand Up @@ -209,18 +211,45 @@ const ProjectSaverHOC = function (WrappedComponent) {
)
)
).then(() => {
const body = new FormData();
const sb3Json = new Blob([this.props.vm.toJSON()], {type: 'application/json'});
body.append('sb3_file', sb3Json, 'sb3_file');
for (const key in requestParams) {
if (requestParams.hasOwnProperty(key)) body.append(key, requestParams[key]);
const opts = {
body: this.props.vm.toJSON(),
// If we set json:true then the body is double-stringified, so don't
headers: {
'Content-Type': 'application/json'
},
withCredentials: true
};
const creatingProject = projectId === null || typeof projectId === 'undefined';
let qs = queryString.stringify(requestParams);
if (qs) qs = `?${qs}`;
if (creatingProject) {
Object.assign(opts, {
method: 'post',
url: `${storage.projectHost}/${qs}`
});
} else {
Object.assign(opts, {
method: 'put',
url: `${storage.projectHost}/${projectId}${qs}`
});
}
return storage.store(
storage.AssetType.Project,
storage.DataFormat.JSON,
body,
projectId
);
return new Promise((resolve, reject) => {
xhr(opts, (err, response) => {
if (err) return reject(err);
let body;
try {
// Since we didn't set json: true, we have to parse manually
body = JSON.parse(response.body);
} catch (e) {
return reject(e);
}
body.id = projectId;
if (creatingProject) {
body.id = body['content-name'];
}
resolve(body);
});
});
})
.then(response => {
this.props.onSetProjectUnchanged();
Expand Down