Skip to content

Commit

Permalink
[feature] WOPI created zero-weight file replacement with template
Browse files Browse the repository at this point in the history
  • Loading branch information
DrToldme committed Jan 10, 2024
1 parent ef01431 commit 7faaaf5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Common/sources/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,33 @@ exports.FILE_STATUS_UPDATE_VERSION = 'updateversion';

exports.ACTIVEMQ_QUEUE_PREFIX = 'queue://';
exports.ACTIVEMQ_TOPIC_PREFIX = 'topic://';

exports.LOCALE_MAP = {
'az': 'az-Latn-AZ',
'bg': 'bg-BG',
'cs': 'cs-CZ',
'de': 'de-DE',
'el': 'el-GR',
'en': 'en-US', // collision
'es': 'es-ES',
'eu': 'eu-ES',
'fr': 'fr-FR',
'gl': 'gl-ES',
'hy': 'hy-AM',
'it': 'it-IT',
'ja': 'ja-JP',
'ko': 'ko-KR',
'lv': 'lv-LV',
'ms': 'ms-MY',
'nl': 'nl-NL',
'pl': 'pl-PL',
'pt': 'pt-BR', // collision
'ru': 'ru-RU',
'si': 'si-LK',
'sk': 'sk-SK',
'sv': 'sv-SE',
'tr': 'tr-TR',
'uk': 'uk-UA',
'vi': 'vi-VN',
'zh': 'zh-CH' // collision
};
12 changes: 12 additions & 0 deletions Common/sources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,3 +1111,15 @@ exports.checksumFile = function(hashName, path) {
stream.on('end', () => resolve(hash.digest('hex')));
});
};

exports.getLocaleFullCode = function(unformattedLocale) {
// Expects that full locale string must starts with at last 2 lowercase characters,
// then comes optional part with dash and at last 2 characters in any case
// and ends with at last 2 uppercase characters with dash.
const isAlreadyFullLocale = /^[a-z]{2,}(-[A-Za-z]{2,})?-[A-Z]{2,}$/.test(unformattedLocale);
if (isAlreadyFullLocale) {
return unformattedLocale;
}

return constants.LOCALE_MAP[unformattedLocale];
}
36 changes: 36 additions & 0 deletions DocService/sources/wopiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ const {URL} = require('url');
const co = require('co');
const jwt = require('jsonwebtoken');
const config = require('config');
const fs = require('fs');
const utf7 = require('utf7');
const mimeDB = require('mime-db');
const xmlbuilder2 = require('xmlbuilder2');
const logger = require('./../../Common/sources/logger');
const utils = require('./../../Common/sources/utils');
const constants = require('./../../Common/sources/constants');
const commonDefines = require('./../../Common/sources/commondefines');
const formatChecker = require('./../../Common/sources/formatchecker');
const operationContext = require('./../../Common/sources/operationContext');
const tenantManager = require('./../../Common/sources/tenantManager');
const sqlBase = require('./databaseConnectors/baseConnector');
Expand All @@ -57,6 +59,7 @@ const cfgTokenOutboxExpires = config.get('services.CoAuthoring.token.outbox.expi
const cfgTokenEnableBrowser = config.get('services.CoAuthoring.token.enable.browser');
const cfgCallbackRequestTimeout = config.get('services.CoAuthoring.server.callbackRequestTimeout');
const cfgAllowPrivateIPAddressForSignedRequests = config.get('services.CoAuthoring.server.allowPrivateIPAddressForSignedRequests');
const cfgNewFileTemplate = config.get('services.CoAuthoring.server.newFileTemplate');
const cfgDownloadTimeout = config.get('FileConverter.converter.downloadTimeout');
const cfgWopiFileInfoBlockList = config.get('wopi.fileInfoBlockList');
const cfgWopiWopiZone = config.get('wopi.wopiZone');
Expand Down Expand Up @@ -440,6 +443,39 @@ function getEditorHtml(req, res) {
yield canvasService.commandOpenStartPromise(ctx, docId, utils.getBaseUrlByRequest(ctx, req), commonInfo, fileType);
}

const fileFormat = (extension) => {
const format = formatChecker.getFormatFromString(extension);
if (formatChecker.isDocumentFormat(format)) {
if (format === constants.AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCXF) {
return 'docxf';
}

return 'docx';
}
if (formatChecker.isSpreadsheetFormat(format)) {
return 'xlsx';
}
if (formatChecker.isPresentationFormat(format)) {
return 'pptx';
}

return '';
};
const fileType = getFileTypeByInfo(fileInfo);
const format = fileFormat(fileType);

// TODO: throw error if format not supported?
if (fileInfo.Size === 0 && format.length !== 0) {
const wopiParams = getWopiParams('', fileInfo, wopiSrc, access_token, access_token_ttl);
const locale = utils.getLocaleFullCode(params.queryParams.lang || params.queryParams.ui || 'en-US');
const defaultFilePath = `${cfgNewFileTemplate}/en-US/new.${format}`;
const expectedPath = `${cfgNewFileTemplate}/${locale}/new.${format}`;
const filePath = fs.existsSync(expectedPath) ? expectedPath : defaultFilePath;
const templateFileStream = fs.createReadStream(filePath);
const templateFileSize = fs.lstatSync(filePath).size;
yield putFile(ctx, wopiParams, undefined, templateFileStream, templateFileSize, fileInfo.UserId, false, false, false);
}

//Lock
if ('view' !== mode) {
let lockRes = yield lock(ctx, 'LOCK', lockId, fileInfo, userAuth);
Expand Down

0 comments on commit 7faaaf5

Please sign in to comment.