mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-04-07 14:04:35 +08:00
[feature] Add info/config handler
This commit is contained in:
102
DocService/sources/routes/config.js
Normal file
102
DocService/sources/routes/config.js
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2024
|
||||
*
|
||||
* This program is a free software product. You can redistribute it and/or
|
||||
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
||||
* version 3 as published by the Free Software Foundation. In accordance with
|
||||
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
||||
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
||||
* of any third-party rights.
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
||||
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
*
|
||||
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
|
||||
* street, Riga, Latvia, EU, LV-1050.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of the Program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU AGPL version 3.
|
||||
*
|
||||
* Pursuant to Section 7(b) of the License you must retain the original Product
|
||||
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
||||
* grant you any rights under trademark law for use of our trademarks.
|
||||
*
|
||||
* All the Product's GUI elements, including illustrations and icon sets, as
|
||||
* well as technical writing content are licensed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
||||
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
*
|
||||
*/
|
||||
|
||||
const config = require('config');
|
||||
const { readFile, writeFile, stat, cp } = require('fs/promises');
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const tenantManager = require('../../../Common/sources/tenantManager');
|
||||
const operationContext = require('../../../Common/sources/operationContext');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const rawFileParser = bodyParser.raw(
|
||||
{inflate: true, limit: config.get('services.CoAuthoring.server.limits_tempfile_upload'), type: function() {return true;}});
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
let ctx = new operationContext.Context();
|
||||
let result = '{}';
|
||||
try {
|
||||
ctx.initFromRequest(req);
|
||||
await ctx.initTenantCache();
|
||||
if (tenantManager.isMultitenantMode(ctx) && !tenantManager.isDefaultTenant(ctx)) {
|
||||
//todo
|
||||
}
|
||||
let configPath = path.join(process.env.NODE_CONFIG_DIR, 'local.json');
|
||||
result = await readFile(configPath);
|
||||
} catch (error) {
|
||||
ctx.logger.error('baseurl error: %s', error.stack);
|
||||
}
|
||||
finally {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(result);
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/', rawFileParser, async (req, res) => {
|
||||
let ctx = new operationContext.Context();
|
||||
try {
|
||||
ctx.initFromRequest(req);
|
||||
await ctx.initTenantCache();
|
||||
if (tenantManager.isMultitenantMode(ctx) && !tenantManager.isDefaultTenant(ctx)) {
|
||||
//todo
|
||||
}
|
||||
|
||||
// Define file paths
|
||||
let configPath = path.join(process.env.NODE_CONFIG_DIR, 'local.json');
|
||||
let backupPath = path.join(process.env.NODE_CONFIG_DIR, 'local.json.bak');
|
||||
|
||||
// Create backup of current config before saving
|
||||
let sampleFileStat = null;
|
||||
try {
|
||||
sampleFileStat = await stat(backupPath);
|
||||
} catch (backupError) {
|
||||
ctx.logger.error('Failed to create configuration backup: %s', backupError.stack);
|
||||
}
|
||||
if(!sampleFileStat){
|
||||
await cp(configPath, backupPath, {force: true, recursive: true});
|
||||
}
|
||||
|
||||
await writeFile(configPath, req.body, {encoding: 'utf8'});
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
ctx.logger.error('baseurl error: %s', error.stack);
|
||||
res.status(500).json({
|
||||
error: 'Failed to save configuration',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
@ -58,6 +58,7 @@ const commonDefines = require('./../../Common/sources/commondefines');
|
||||
const operationContext = require('./../../Common/sources/operationContext');
|
||||
const tenantManager = require('./../../Common/sources/tenantManager');
|
||||
const staticRouter = require('./routes/static');
|
||||
const configRouter = require('./routes/config');
|
||||
const ms = require('ms');
|
||||
const aiProxyHandler = require('./ai/aiProxyHandler');
|
||||
|
||||
@ -236,6 +237,7 @@ docsCoServer.install(server, () => {
|
||||
converterService.builder(req, res);
|
||||
});
|
||||
app.get('/info/info.json', utils.checkClientIp, docsCoServer.licenseInfo);
|
||||
app.use('/info/config', utils.checkClientIp, configRouter);
|
||||
app.put('/internal/cluster/inactive', utils.checkClientIp, docsCoServer.shutdown);
|
||||
app.delete('/internal/cluster/inactive', utils.checkClientIp, docsCoServer.shutdown);
|
||||
app.get('/internal/connections/edit', docsCoServer.getEditorConnectionsCount);
|
||||
|
||||
@ -539,6 +539,40 @@
|
||||
document.querySelector('.td-link[value=' + value + ']').classList.add('current');
|
||||
}
|
||||
|
||||
function getConfig() {
|
||||
const baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
|
||||
return fetch(baseUrl + 'config')
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`Failed to load: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching config:', error);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
function putConfig(config) {
|
||||
const baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
|
||||
return fetch(baseUrl + 'config', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(config)
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`Failed to save: ${response.status}`);
|
||||
console.log('Configuration saved successfully');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching config:', error);
|
||||
});
|
||||
}
|
||||
function updateConfig() {
|
||||
getConfig().then(config => {
|
||||
config.field = 'value';
|
||||
putConfig(config);
|
||||
});
|
||||
}
|
||||
|
||||
(function(){
|
||||
try {
|
||||
var xhrObj = _createXMLHTTPObject();
|
||||
@ -557,6 +591,7 @@
|
||||
fillStatistic(info);
|
||||
initEvents();
|
||||
applyMode();
|
||||
//updateConfig();
|
||||
} else {
|
||||
document.getElementById('doc-server-err').classList.remove("hidden");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user