Skip to content

Commit

Permalink
Merge pull request #233 from Kitware/create-project-fix
Browse files Browse the repository at this point in the history
fix(project): closes #221 proper project file uploading with metadata
  • Loading branch information
TristanWright committed Mar 14, 2016
2 parents 63177eb + e1acf60 commit 4ab9320
Showing 1 changed file with 67 additions and 33 deletions.
100 changes: 67 additions & 33 deletions src/network/remoteClient/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,81 @@ export function getProject(id) {
return girder.getProject(id);
}

function createProjectPromise(project, name, file) {
return new Promise((resolve, reject) => {
var projectResp;
girder.createProject(project)
function createItemForProject(project, name, file) {
return girder.createItem(project.metadata.inputFolder._id, name)
.then((resp) => {
// fill item with file
const itemId = resp.data._id,
params = {};
params.parentType = 'item';
params.parentId = itemId;
params.name = file.name;
params.size = file.size;
console.log('Attach file to', itemId);
return girder.uploadFileToItem(params, file);
})
.then((resp) => {
project.metadata.inputFolder.files[name] = resp.data._id;
return girder.updateProject(project);
})
.catch((error) => {
const msg = error.data && error.data.message ? error.data.message : error.message;
console.error('upload failed:', msg);
});
}

export function saveProject(project, attachments) {
if (!project._id) {
let folder;
let outputFolder;
return girder.createProject(project)
// make output folder
.then((resp) => {
project = resp.data;
folder = {
name: 'output',
parentType: 'folder',
parentId: project.folderId,
};
return girder.createFolder(folder);
})
// make input folder
.then((resp) => {
// Upload file to folder
projectResp = resp;
return girder.createItem(resp.data.folderId, name);
outputFolder = resp.data._id;
folder.name = 'input';
return girder.createFolder(folder);
})
// update proj metadata
.then((resp) => {
// fill item with file
const itemId = resp.data._id,
params = {};
params.parentType = 'item';
params.parentId = itemId;
params.name = file.name;
params.size = file.size;
console.log('Attach file to', itemId);
return girder.uploadFileToItem(params, file);
const inputFolder = resp.data._id;
project.metadata = {
inputFolder: {
_id: inputFolder,
files: {},
},
outputFolder: {
_id: outputFolder,
files: {},
},
};
return girder.updateProject(project);
})
// upload files to inputfolder if there are any,
// returns either promise result array with sim object as last item or simulation object.
.then((resp) => {
resolve(projectResp);
if (attachments) {
const promises = [];
for (const file in attachments) {
promises.push(createItemForProject(project, file, attachments[file]));
}
promises.push(project);
return Promise.all(promises);
}
return (project);
})
.catch((error) => {
const msg = error.data && error.data.message ? error.data.message : error.message;
console.error('upload failed:', msg);
reject(error);
console.log(error);
});
});
}

export function saveProject(project, attachements) {
if (!project._id) {
if (attachements) {
const promises = [];
for (const file in attachements) {
promises.push(createProjectPromise(project, file, attachements[file]));
}
return Promise.all(promises);
}
return girder.createProject(project);
}
return girder.updateProject(project);
}
Expand Down

0 comments on commit 4ab9320

Please sign in to comment.