Skip to content

Commit

Permalink
[feature] Add checkAndInvalidateCache to refreshFile
Browse files Browse the repository at this point in the history
  • Loading branch information
konovalovsergey committed Sep 30, 2024
1 parent 9b6e5d2 commit cdcc488
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DocService/sources/DocsCoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ async function startWopiRPC(ctx, docId, userId, userIdOriginal, data) {
res = await wopiClient.renameFile(ctx, wopiParams, data.name);
break;
case 'wopi_RefreshFile':
res = await wopiClient.refreshFile(ctx, wopiParams, docId);
res = await wopiClient.refreshFile(ctx, wopiParams, row.baseurl);
break;
}
}
Expand Down
87 changes: 55 additions & 32 deletions DocService/sources/wopiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,41 @@ async function checkAndReplaceEmptyFile(ctx, fileInfo, wopiSrc, access_token, ac
}
}
}
function createDocId(ctx, wopiSrc, mode, fileInfo) {
let fileId = wopiSrc.substring(wopiSrc.lastIndexOf('/') + 1);
let docId = undefined;
if ('view' !== mode) {
docId = `${fileId}`;
} else {
//todo rename operation requires lock
fileInfo.SupportsRename = false;
//todo change docId to avoid empty cache after editors are gone
if (fileInfo.LastModifiedTime) {
docId = `view.${fileId}.${fileInfo.LastModifiedTime}`;
} else {
docId = `view.${fileId}.${fileInfo.Version}`;
}
return docId;
}
docId = docId.replace(constants.DOC_ID_REPLACE_REGEX, '_').substring(0, constants.DOC_ID_MAX_LENGTH);
return docId;
}
async function preOpen(ctx, lockId, docId, fileInfo, userAuth, baseUrl, fileType) {
//todo move to lock and common info saving to websocket connection
//save common info
if (undefined === lockId) {
//Use deterministic(not random) lockId to fix issues with forgotten openings due to integrator failures
lockId = docId;
let commonInfo = JSON.stringify({lockId: lockId, fileInfo: fileInfo});
await canvasService.commandOpenStartPromise(ctx, docId, baseUrl, commonInfo, fileType);
}
//Lock
if ('view' !== userAuth.mode) {
let lockRes = await lock(ctx, 'LOCK', lockId, fileInfo, userAuth);
return !!lockRes;
}
return true;
}
function getEditorHtml(req, res) {
return co(function*() {
let params = {key: undefined, apiQuery: '', fileInfo: {}, userAuth: {}, queryParams: req.query, token: undefined, documentType: undefined, docs_api_config: {}};
Expand Down Expand Up @@ -550,20 +585,8 @@ function getEditorHtml(req, res) {
mode = 'view';
}
//docId
let docId = undefined;
if ('view' !== mode) {
docId = `${fileId}`;
} else {
//todo rename operation requires lock
fileInfo.SupportsRename = false;
//todo change docId to avoid empty cache after editors are gone
if (fileInfo.LastModifiedTime) {
docId = `view.${fileId}.${fileInfo.LastModifiedTime}`;
} else {
docId = `view.${fileId}.${fileInfo.Version}`;
}
}
docId = docId.replace(constants.DOC_ID_REPLACE_REGEX, '_').substring(0, constants.DOC_ID_MAX_LENGTH);
let docId = createDocId(ctx, wopiSrc, mode, fileInfo);
ctx.setDocId(fileId);
ctx.logger.debug(`wopiEditor`);
params.key = docId;
let userAuth = params.userAuth = {
Expand All @@ -573,27 +596,15 @@ function getEditorHtml(req, res) {

//check and invalidate cache
let checkRes = yield checkAndInvalidateCache(ctx, docId, fileInfo);
let lockId = checkRes.lockId;
if (!checkRes.success) {
params.fileInfo = {};
return;
}
if (!shutdownFlag) {
//save common info
if (undefined === lockId) {
//Use deterministic(not random) lockId to fix issues with forgotten openings due to integrator failures
lockId = docId;
let commonInfo = JSON.stringify({lockId: lockId, fileInfo: fileInfo});
yield canvasService.commandOpenStartPromise(ctx, docId, utils.getBaseUrlByRequest(ctx, req), commonInfo, fileType);
}

//Lock
if ('view' !== mode) {
let lockRes = yield lock(ctx, 'LOCK', lockId, fileInfo, userAuth);
if (!lockRes) {
params.fileInfo = {};
return;
}
let preOpenRes = yield preOpen(ctx, checkRes.lockId, docId, fileInfo, userAuth, utils.getBaseUrlByRequest(ctx, req), fileType);
if (!preOpenRes) {
params.fileInfo = {};
return;
}
}

Expand Down Expand Up @@ -813,7 +824,7 @@ function renameFile(ctx, wopiParams, name) {
});
}

async function refreshFile(ctx, wopiParams, docId) {
async function refreshFile(ctx, wopiParams, baseUrl) {
let res = {};
try {
ctx.logger.info('wopi RefreshFile start');
Expand All @@ -825,15 +836,27 @@ async function refreshFile(ctx, wopiParams, docId) {
const tenTokenOutboxAlgorithm = ctx.getCfg('services.CoAuthoring.token.outbox.algorithm', cfgTokenOutboxAlgorithm);
const tenTokenOutboxExpires = ctx.getCfg('services.CoAuthoring.token.outbox.expires', cfgTokenOutboxExpires);

const fileInfo = await checkFileInfo(ctx, userAuth.wopiSrc, userAuth.access_token);
const fileType = getFileTypeByInfo(fileInfo);
const docId = createDocId(ctx, userAuth.wopiSrc, userAuth.mode, res.fileInfo);
res.key = docId;
res.userAuth = userAuth;
res.fileInfo = await checkFileInfo(ctx, userAuth.wopiSrc, userAuth.access_token);
res.fileInfo = fileInfo;
res.queryParams = undefined;
if (tenTokenEnableBrowser) {
let options = {algorithm: tenTokenOutboxAlgorithm, expiresIn: tenTokenOutboxExpires};
let secret = await tenantManager.getTenantSecret(ctx, commonDefines.c_oAscSecretType.Browser);
res.token = jwt.sign(res, secret, options);
}
let checkRes = await checkAndInvalidateCache(ctx, docId, fileInfo);
if (!checkRes.success) {
res = {};
return;
}
let preOpenRes = await preOpen(ctx, checkRes.lockId, docId, fileInfo, userAuth, baseUrl, fileType);
if (!preOpenRes) {
res = {};
}
} catch (err) {
ctx.logger.error('wopi error RefreshFile:%s', err.stack);
} finally {
Expand Down

0 comments on commit cdcc488

Please sign in to comment.