Skip to content

Commit

Permalink
Merge pull request #4015 from rschamp/project-server-reconfig
Browse files Browse the repository at this point in the history
Manually upload projects using xhr, rather than scratch-storage
  • Loading branch information
rschamp authored Dec 11, 2018
2 parents c4c481c + 2812720 commit a83601c
Showing 1 changed file with 40 additions and 11 deletions.
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

0 comments on commit a83601c

Please sign in to comment.