HFP-1905 Add update functionality for metadata

pull/52/head
Oliver Tacke 2018-04-03 15:17:46 +02:00
parent b28624ba8e
commit 95d99d0ad3
3 changed files with 61 additions and 23 deletions

View File

@ -7,7 +7,7 @@ H5P.ContentUpgradeProcess = (function (Version) {
* @class * @class
* @namespace H5P * @namespace H5P
*/ */
function ContentUpgradeProcess(name, oldVersion, newVersion, params, id, loadLibrary, done) { function ContentUpgradeProcess(name, oldVersion, newVersion, params, extras, id, loadLibrary, done) {
var self = this; var self = this;
// Make params possible to work with // Make params possible to work with
@ -24,20 +24,38 @@ H5P.ContentUpgradeProcess = (function (Version) {
}); });
} }
// Make extras possible to work with
for (var element in extras) {
if (extras.hasOwnProperty(element)) {
try {
extras[element] = JSON.parse(extras[element]);
if (!(extras[element] instanceof Object)) {
throw true;
}
}
catch (event) {
return done({
type: 'errorExtrasBroken',
id: id
});
}
}
}
self.loadLibrary = loadLibrary; self.loadLibrary = loadLibrary;
self.upgrade(name, oldVersion, newVersion, params, function (err, result) { self.upgrade(name, oldVersion, newVersion, params, extras, function (err, result) {
if (err) { if (err) {
return done(err); return done(err);
} }
done(null, JSON.stringify(params)); done(null, JSON.stringify(params), JSON.stringify(extras));
}); });
} }
/** /**
* *
*/ */
ContentUpgradeProcess.prototype.upgrade = function (name, oldVersion, newVersion, params, done) { ContentUpgradeProcess.prototype.upgrade = function (name, oldVersion, newVersion, params, extras, done) {
var self = this; var self = this;
// Load library details and upgrade routines // Load library details and upgrade routines
@ -47,7 +65,7 @@ H5P.ContentUpgradeProcess = (function (Version) {
} }
// Run upgrade routines on params // Run upgrade routines on params
self.processParams(library, oldVersion, newVersion, params, function (err, params) { self.processParams(library, oldVersion, newVersion, params, extras, function (err, params, extras) {
if (err) { if (err) {
return done(err); return done(err);
} }
@ -61,7 +79,7 @@ H5P.ContentUpgradeProcess = (function (Version) {
next(err); next(err);
}); });
}, function (err) { }, function (err) {
done(err, params); done(err, params, extras);
}); });
}); });
}); });
@ -77,7 +95,7 @@ H5P.ContentUpgradeProcess = (function (Version) {
* @param {Object} params * @param {Object} params
* @param {Function} next * @param {Function} next
*/ */
ContentUpgradeProcess.prototype.processParams = function (library, oldVersion, newVersion, params, next) { ContentUpgradeProcess.prototype.processParams = function (library, oldVersion, newVersion, params, extras, next) {
if (H5PUpgrades[library.name] === undefined) { if (H5PUpgrades[library.name] === undefined) {
if (library.upgradesScript) { if (library.upgradesScript) {
// Upgrades script should be loaded so the upgrades should be here. // Upgrades script should be loaded so the upgrades should be here.
@ -110,10 +128,11 @@ H5P.ContentUpgradeProcess = (function (Version) {
var unnecessaryWrapper = (upgrade.contentUpgrade !== undefined ? upgrade.contentUpgrade : upgrade); var unnecessaryWrapper = (upgrade.contentUpgrade !== undefined ? upgrade.contentUpgrade : upgrade);
try { try {
unnecessaryWrapper(params, function (err, upgradedParams) { unnecessaryWrapper(params, function (err, upgradedParams, upgradedExtras) {
params = upgradedParams; params = upgradedParams;
extras = upgradedExtras;
nextMinor(err); nextMinor(err);
}); }, extras);
} }
catch (err) { catch (err) {
if (console && console.log) { if (console && console.log) {
@ -127,7 +146,7 @@ H5P.ContentUpgradeProcess = (function (Version) {
}, nextMajor); }, nextMajor);
} }
}, function (err) { }, function (err) {
next(err, params); next(err, params, extras);
}); });
}; };
@ -168,11 +187,21 @@ H5P.ContentUpgradeProcess = (function (Version) {
return done(); // Larger or same version that's available return done(); // Larger or same version that's available
} }
// Currently, we only use metadata as additional things that might need change
var extras = {
metadata: params.metadata
};
// A newer version is available, upgrade params // A newer version is available, upgrade params
return self.upgrade(availableLib[0], usedVer, availableVer, params.params, function (err, upgraded) { return self.upgrade(availableLib[0], usedVer, availableVer, params.params, extras, function (err, upgraded, extras) {
if (!err) { if (!err) {
params.library = availableLib[0] + ' ' + availableVer.major + '.' + availableVer.minor; params.library = availableLib[0] + ' ' + availableVer.major + '.' + availableVer.minor;
params.params = upgraded; params.params = upgraded;
if (extras) {
if (extras.metadata) {
params.metadata = extras.metadata;
}
}
} }
done(err, params); done(err, params);
}); });

View File

@ -9,7 +9,7 @@ var libraryLoadedCallback;
var messageHandlers = { var messageHandlers = {
newJob: function (job) { newJob: function (job) {
// Start new job // Start new job
new H5P.ContentUpgradeProcess(job.name, new H5P.Version(job.oldVersion), new H5P.Version(job.newVersion), job.params, job.id, function loadLibrary(name, version, next) { new H5P.ContentUpgradeProcess(job.name, new H5P.Version(job.oldVersion), new H5P.Version(job.newVersion), job.params, job.extras, job.id, function loadLibrary(name, version, next) {
// TODO: Cache? // TODO: Cache?
postMessage({ postMessage({
action: 'loadLibrary', action: 'loadLibrary',
@ -17,7 +17,7 @@ var messageHandlers = {
version: version.toString() version: version.toString()
}); });
libraryLoadedCallback = next; libraryLoadedCallback = next;
}, function done(err, result) { }, function done(err, result, extras) {
if (err) { if (err) {
// Return error // Return error
postMessage({ postMessage({
@ -33,7 +33,8 @@ var messageHandlers = {
postMessage({ postMessage({
action: 'done', action: 'done',
id: job.id, id: job.id,
params: result params: result,
extras: extras
}); });
}); });
}, },

View File

@ -116,7 +116,7 @@
// Register message handlers // Register message handlers
var messageHandlers = { var messageHandlers = {
done: function (result) { done: function (result) {
self.workDone(result.id, result.params, this); self.workDone(result.id, result.params, result.extras, this);
}, },
error: function (error) { error: function (error) {
self.printError(error.err); self.printError(error.err);
@ -184,7 +184,7 @@
self.token = inData.token; self.token = inData.token;
// Start processing // Start processing
self.processBatch(inData.params); self.processBatch(inData.params, {metadata: inData.metadata});
}); });
}; };
@ -202,7 +202,7 @@
* *
* @param {Object} parameters * @param {Object} parameters
*/ */
ContentUpgrade.prototype.processBatch = function (parameters) { ContentUpgrade.prototype.processBatch = function (parameters, extras) {
var self = this; var self = this;
// Track upgraded params // Track upgraded params
@ -210,6 +210,7 @@
// Track current batch // Track current batch
self.parameters = parameters; self.parameters = parameters;
self.extras = extras;
// Create id mapping // Create id mapping
self.ids = []; self.ids = [];
@ -247,6 +248,11 @@
self.current++; self.current++;
self.working++; self.working++;
// Rewrap metadata
if (self.extras.metadata) {
self.extras.metadata = self.extras.metadata[id];
}
if (worker) { if (worker) {
worker.postMessage({ worker.postMessage({
action: 'newJob', action: 'newJob',
@ -254,11 +260,12 @@
name: info.library.name, name: info.library.name,
oldVersion: info.library.version, oldVersion: info.library.version,
newVersion: self.version.toString(), newVersion: self.version.toString(),
params: self.parameters[id] params: self.parameters[id],
extras: self.extras
}); });
} }
else { else {
new H5P.ContentUpgradeProcess(info.library.name, new Version(info.library.version), self.version, self.parameters[id], id, function loadLibrary(name, version, next) { new H5P.ContentUpgradeProcess(info.library.name, new Version(info.library.version), self.version, self.parameters[id], self.extras, id, function loadLibrary(name, version, next) {
self.loadLibrary(name, version, function (err, library) { self.loadLibrary(name, version, function (err, library) {
if (library.upgradesScript) { if (library.upgradesScript) {
self.loadScript(library.upgradesScript, function (err) { self.loadScript(library.upgradesScript, function (err) {
@ -273,13 +280,13 @@
} }
}); });
}, function done(err, result) { }, function done(err, result, extras) {
if (err) { if (err) {
self.printError(err); self.printError(err);
return ; return ;
} }
self.workDone(id, result); self.workDone(id, result, extras);
}); });
} }
}; };
@ -287,7 +294,7 @@
/** /**
* *
*/ */
ContentUpgrade.prototype.workDone = function (id, result, worker) { ContentUpgrade.prototype.workDone = function (id, result, extras, worker) {
var self = this; var self = this;
self.working--; self.working--;
@ -302,7 +309,8 @@
self.nextBatch({ self.nextBatch({
libraryId: self.version.libraryId, libraryId: self.version.libraryId,
token: self.token, token: self.token,
params: JSON.stringify(self.upgraded) params: JSON.stringify(self.upgraded),
extras: extras
}); });
} }
}; };