Skip to content

Commit

Permalink
[feature] Allow "WOPISrc" and "shardkey" query params as shard key
Browse files Browse the repository at this point in the history
  • Loading branch information
konovalovsergey committed Apr 17, 2024
1 parent 11fc888 commit e07e14f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Common/sources/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ exports.VIEWER_ONLY = /^(?:(pdf|djvu|xps|oxps))$/;
exports.DEFAULT_DOC_ID = 'docId';
exports.DEFAULT_USER_ID = 'userId';
exports.ALLOWED_PROTO = /^https?$/i;
exports.SHARED_KEY_NAME = 'WOPISrc';
exports.SHARD_KEY_WOPI_NAME = 'WOPISrc';
exports.SHARD_KEY_API_NAME = 'shardkey';

exports.RIGHTS = {
None : 0,
Expand Down
21 changes: 14 additions & 7 deletions Common/sources/operationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ function Context(){
this.logger = logger.getLogger('nodeJS');
this.initDefault();
}
Context.prototype.init = function(tenant, docId, userId, opt_shardKey) {
Context.prototype.init = function(tenant, docId, userId, opt_shardKey, opt_WopiSrc) {
this.setTenant(tenant);
this.setDocId(docId);
this.setUserId(userId);
this.setShardKey(opt_shardKey);
this.setWopiSrc(opt_WopiSrc);

this.config = null;
this.secret = null;
Expand All @@ -65,21 +66,23 @@ Context.prototype.initFromConnection = function(conn) {
}
}
let userId = conn.user?.id;
let shardKey = utils.getShardByConnection(this, conn);
this.init(tenant, docId || this.docId, userId || this.userId, shardKey);
let shardKey = utils.getShardKeyByConnection(this, conn);
let wopiSrc = utils.getWopiSrcByConnection(this, conn);
this.init(tenant, docId || this.docId, userId || this.userId, shardKey, wopiSrc);
};
Context.prototype.initFromRequest = function(req) {
let tenant = tenantManager.getTenantByRequest(this, req);
let shardKey = utils.getShardKeyByRequest(this, req);
this.init(tenant, this.docId, this.userId, shardKey);
let wopiSrc = utils.getWopiSrcByRequest(this, req);
this.init(tenant, this.docId, this.userId, shardKey, wopiSrc);
};
Context.prototype.initFromTaskQueueData = function(task) {
let ctx = task.getCtx();
this.init(ctx.tenant, ctx.docId, ctx.userId, ctx.shardKey);
this.init(ctx.tenant, ctx.docId, ctx.userId, ctx.shardKey, ctx.wopiSrc);
};
Context.prototype.initFromPubSub = function(data) {
let ctx = data.ctx;
this.init(ctx.tenant, ctx.docId, ctx.userId, ctx.shardKey);
this.init(ctx.tenant, ctx.docId, ctx.userId, ctx.shardKey, ctx.wopiSrc);
};
Context.prototype.initTenantCache = async function() {
this.config = await tenantManager.getTenantConfig(this);
Expand All @@ -101,12 +104,16 @@ Context.prototype.setUserId = function(userId) {
Context.prototype.setShardKey = function(shardKey) {
this.shardKey = shardKey;
};
Context.prototype.setWopiSrc = function(wopiSrc) {
this.wopiSrc = wopiSrc;
};
Context.prototype.toJSON = function() {
return {
tenant: this.tenant,
docId: this.docId,
userId: this.userId,
shardKey: this.shardKey
shardKey: this.shardKey,
wopiSrc: this.wopiSrc
}
};
Context.prototype.getCfg = function(property, defaultValue) {
Expand Down
5 changes: 4 additions & 1 deletion Common/sources/storage-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ async function getSignedUrl(ctx, storageCfg, baseUrl, strPath, urlType, optFilen
url += '?md5=' + encodeURIComponent(md5);
url += '&expires=' + encodeURIComponent(expires);
if (ctx.shardKey) {
url += `&${constants.SHARED_KEY_NAME}=${encodeURIComponent(ctx.shardKey)}`;
url += `&${constants.SHARD_KEY_API_NAME}=${encodeURIComponent(ctx.shardKey)}`;
}
if (ctx.wopiSrc) {
url += `&${constants.SHARD_KEY_WOPI_NAME}=${encodeURIComponent(ctx.wopiSrc)}`;
}
url += '&filename=' + userFriendlyName;
return url;
Expand Down
16 changes: 12 additions & 4 deletions Common/sources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,14 +787,22 @@ function getDomainByRequest(ctx, req) {
}
exports.getDomainByConnection = getDomainByConnection;
exports.getDomainByRequest = getDomainByRequest;
function getShardByConnection(ctx, conn) {
return conn?.handshake?.query?.[constants.SHARED_KEY_NAME];
function getShardKeyByConnection(ctx, conn) {
return conn?.handshake?.query?.[constants.SHARD_KEY_API_NAME];
}
function getWopiSrcByConnection(ctx, conn) {
return conn?.handshake?.query?.[constants.SHARD_KEY_WOPI_NAME];
}
function getShardKeyByRequest(ctx, req) {
return req.query[constants.SHARED_KEY_NAME];
return req.query?.[constants.SHARD_KEY_API_NAME];
}
function getWopiSrcByRequest(ctx, req) {
return req.query?.[constants.SHARD_KEY_WOPI_NAME];
}
exports.getShardByConnection = getShardByConnection;
exports.getShardKeyByConnection = getShardKeyByConnection;
exports.getWopiSrcByConnection = getWopiSrcByConnection;
exports.getShardKeyByRequest = getShardKeyByRequest;
exports.getWopiSrcByRequest = getWopiSrcByRequest;
function stream2Buffer(stream) {
return new Promise(function(resolve, reject) {
if (!stream.readable) {
Expand Down
5 changes: 4 additions & 1 deletion DocService/sources/DocsCoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ exports.install = function(server, callbackFunction) {
upsertRes = yield canvasService.commandOpenStartPromise(ctx, docId, utils.getBaseUrlByConnection(ctx, conn), data.documentCallbackUrl, format);
curIndexUser = upsertRes.insertId;
//todo update additional in commandOpenStartPromise
if ((upsertRes.isInsert || (wopiParams && 2 === curIndexUser)) && (undefined !== data.timezoneOffset || ctx.shardKey)) {
if ((upsertRes.isInsert || (wopiParams && 2 === curIndexUser)) && (undefined !== data.timezoneOffset || ctx.shardKey || ctx.wopiSrc)) {
//todo insert in commandOpenStartPromise. insert here for database compatibility
if (false === canvasService.hasAdditionalCol) {
let selectRes = yield taskResult.select(ctx, docId);
Expand All @@ -2539,6 +2539,9 @@ exports.install = function(server, callbackFunction) {
if (ctx.shardKey) {
task.additional += sqlBase.DocumentAdditional.prototype.setShardKey(ctx.shardKey);
}
if (ctx.wopiSrc) {
task.additional += sqlBase.DocumentAdditional.prototype.setWopiSrc(ctx.wopiSrc);
}
yield taskResult.update(ctx, task);
} else {
ctx.logger.warn('auth unknown column "additional"');
Expand Down
7 changes: 6 additions & 1 deletion DocService/sources/canvasservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ let commandSfctByCmd = co.wrap(function*(ctx, cmd, opt_priority, opt_expiration,
}
if (opt_initShardKey) {
ctx.setShardKey(sqlBase.DocumentAdditional.prototype.getShardKey(row.additional));
ctx.setWopiSrc(sqlBase.DocumentAdditional.prototype.getWopiSrc(row.additional));
}
yield* addRandomKeyTaskCmd(ctx, cmd);
addPasswordToCmd(ctx, cmd, row.password);
Expand Down Expand Up @@ -1525,7 +1526,10 @@ function getPrintFileUrl(ctx, docId, baseUrl, filename) {
let userFriendlyName = encodeURIComponent(filename.replace(/\//g, "%2f"));
let res = `${baseUrl}/printfile/${encodeURIComponent(docId)}/${userFriendlyName}?token=${encodeURIComponent(token)}`;
if (ctx.shardKey) {
res += `&${constants.SHARED_KEY_NAME}=${encodeURIComponent(ctx.shardKey)}`;
res += `&${constants.SHARD_KEY_API_NAME}=${encodeURIComponent(ctx.shardKey)}`;
}
if (ctx.wopiSrc) {
res += `&${constants.SHARD_KEY_WOPI_NAME}=${encodeURIComponent(ctx.wopiSrc)}`;
}
res += `&filename=${userFriendlyName}`;
return res;
Expand Down Expand Up @@ -1727,6 +1731,7 @@ exports.saveFromChanges = function(ctx, docId, statusInfo, optFormat, opt_userId
}
if (opt_initShardKey) {
ctx.setShardKey(sqlBase.DocumentAdditional.prototype.getShardKey(row.additional));
ctx.setWopiSrc(sqlBase.DocumentAdditional.prototype.getWopiSrc(row.additional));
}
var cmd = new commonDefines.InputCommand();
cmd.setCommand('sfc');
Expand Down
17 changes: 17 additions & 0 deletions DocService/sources/databaseConnectors/connectorUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ DocumentAdditional.prototype.getShardKey = function(str) {
return res;
};

DocumentAdditional.prototype.setWopiSrc = function(wopiSrc) {
let additional = new DocumentAdditional();
additional.data.push({wopiSrc});
return additional.toSQLInsert();
};
DocumentAdditional.prototype.getWopiSrc = function(str) {
let res;
let val = new DocumentAdditional();
val.fromString(str);
val.data.forEach((elem) => {
if (elem.wopiSrc) {
res = elem.wopiSrc;
}
});
return res;
};

module.exports = {
UserCallback,
DocumentPassword,
Expand Down
3 changes: 2 additions & 1 deletion DocService/sources/gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ var checkFileExpire = function(expireSeconds) {
let tenant = expired[i].tenant;
let docId = expired[i].id;
let shardKey = sqlBase.DocumentAdditional.prototype.getShardKey(expired[i].additional);
ctx.init(tenant, docId, ctx.userId, shardKey);
let wopiSrc = sqlBase.DocumentAdditional.prototype.getWopiSrc(expired[i].additional);
ctx.init(tenant, docId, ctx.userId, shardKey, wopiSrc);
yield ctx.initTenantCache();
//todo tenant
//check that no one is in the document
Expand Down

0 comments on commit e07e14f

Please sign in to comment.