Skip to content
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
34 changes: 19 additions & 15 deletions src/services/agent-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,42 +382,46 @@ const getImageSnapshot = async function (fog, transaction) {
};

const putImageSnapshot = async function (req, fog, transaction) {
if (req.headers['content-type'].includes('multipart/form-data')) {
const opts = {
maxFieldsSize: 500 * 1024 * 1024,
maxFileSize: 500 * 1024 * 1024
};
if (!req.headers['content-type'].includes('multipart/form-data')) {
throw new Errors.ValidationError(ErrorMessages.INVALID_CONTENT_TYPE);
}

const form = new formidable.IncomingForm();
const form = new formidable.IncomingForm(opts);
form.uploadDir = path.join(appRoot, '../') + 'data';
if (!fs.existsSync(form.uploadDir)) {
fs.mkdirSync(form.uploadDir);
}
const absolutePath = await saveSnapShot(req, form);
await MicroserviceManager.update({
iofogUuid: fog.uuid,
imageSnapshot: 'get_image'
}, {
imageSnapshot: absolutePath
}, transaction);
await saveSnapShot(req, form,fog, transaction);

};

const saveSnapShot = function (req, form) {
const saveSnapShot = function (req, form, fog, transaction) {
return new Promise((resolve, reject) => {

form.parse(req, async function (error, fields, files) {
const file = files['upstream'];

if (file === undefined) {
reject(new Errors.ValidationError(ErrorMessages.UPLOADED_FILE_NOT_FOUND));
return;
}

const filePath = file['path'];


let absolutePath = path.resolve(filePath);
fs.rename(absolutePath, absolutePath + '.tar.gz');
resolve(absolutePath + '.tar.gz');
fs.renameSync(absolutePath, absolutePath + '.tar.gz');

await MicroserviceManager.update({
iofogUuid: fog.uuid,
imageSnapshot: 'get_image'
}, {
imageSnapshot: absolutePath + '.tar.gz'
}, transaction);

resolve();

});
});
};
Expand Down