diff --git a/Common/sources/constants.js b/Common/sources/constants.js index 36471cad7..29ae60265 100644 --- a/Common/sources/constants.js +++ b/Common/sources/constants.js @@ -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 +}; diff --git a/Common/sources/utils.js b/Common/sources/utils.js index 3b02a5f9f..0899e8893 100644 --- a/Common/sources/utils.js +++ b/Common/sources/utils.js @@ -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]; +} diff --git a/DocService/sources/wopiClient.js b/DocService/sources/wopiClient.js index 607b2adf8..2d950944c 100644 --- a/DocService/sources/wopiClient.js +++ b/DocService/sources/wopiClient.js @@ -38,6 +38,7 @@ 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'); @@ -45,6 +46,7 @@ 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'); @@ -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'); @@ -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);