So working on this has produced an interesting problem. I’m using the Node.js client
I settled on a process that, when the upload comes in, I check that sessionId against a MongoDB to see whether it exists. If it does, I get some more ids from it to process elsewhere, if it doesn’t, I create it. I’ve included the complete function call below.
While I can use await
in the function to access the mongoDB, I can’t stop the onUploadCreate
from firing before I’ve had the chance to process the first upload file, presumably because it’s async (I’m pretty new to JS!).
This means that I can’t capture any kind of state when uploading more than one file.
So I guess the question is, is there anyway I can process one upload at a time? or, at least, any other architectural way I can achieve this? I’m basically trying to add one entry in a db with a list of all files the user uploads when they click upload. I can’t be the first person to want to do this?
Any help or suggestions would be greatly appreciated.
Thanks
async onUploadCreate(req, res, upload) {
/* Check the metadata */
const {ok, expected} = await common.validateMetadata(upload);
if (!ok) {
const body = `Expected "${expected}" in "Upload-Metadata" but received "${upload.metadata}"`;
throw {status_code: 500, body};
} else
logger.info(`Metadata is ok!`);
/* Check the user is logged in */
const userAuthorised = await catalogue.getSession(upload.metadata.userSession);
if (!userAuthorised) {
const body = `User not authorised`;
logger.info(`User with session id: "${upload.metadata.userSession}" not authed`);
throw {status_code: 401, body};
} else
logger.info(`User with session id: "${upload.metadata.userSession}" is logged in`);
/* Create the items in the catalogue and mongo db for tracking upload session*/
// check the mongo db for existing uploadId
const result = await mongo.getUploadIdInDB(upload.metadata.uploadId);
if (result == null) {
logger.info(`Upload ID: "${upload.metadata.uploadId}" not found in mongodb`)
// create a dataset in catalogue
let dataset = await catalogue.createDataset(upload.metadata);
// then create the datafiles in catalogue
let datafile = await catalogue.createDatafile(dataset, upload.id, upload.size, upload.metadata);
// then add the id in mongo
await mongo.createUpload(upload.metadata, dataset, datafile)
}
else {
logger.info(`Upload ID: "${upload.metadata.uploadId}" found in mongodb!`)
// create a new datafile to go this dataset
let datafile = await catalogue.createDatafile(result.datasetId[0], upload.id, upload.size, upload.metadata);
// then add the id of the nre datafile to the upload entry in mongo
await mongo.appendToUpload( result.datasetId, datafile)
}
logger.info(`Done processing upload file`)
return res;
},