[bug] Fix missing response on error status in WOPI requests; Fix bug 75684

This commit is contained in:
Sergey Konovalov
2025-07-02 17:16:15 +03:00
parent 106c43fc62
commit 496b069b7f

View File

@ -1650,17 +1650,32 @@ exports.printFile = function(req, res) {
}
});
};
/**
* Proxy download file request to the file storage
* @param {object} req - The HTTP request object
* @param {object} res - The HTTP response object
* @returns {Promise}
*/
exports.downloadFile = function(req, res) {
return co(function*() {
let ctx = new operationContext.Context();
let stream = null;
try {
let startDate = null;
if (clientStatsD) {
startDate = new Date();
}
const docId = req.params.docid;
if (!docId) {
res.status(400).send('docid is required');
return;
}
ctx.initFromRequest(req);
yield ctx.initTenantCache();
ctx.setDocId(req.params.docid);
ctx.setDocId(docId);
//todo remove in 8.1. For compatibility
let url = req.get('x-url');
if (url) {
@ -1679,7 +1694,7 @@ exports.downloadFile = function(req, res) {
let headers, fromTemplate;
let authRes = yield docsCoServer.getRequestParams(ctx, req);
if (authRes.code === constants.NO_ERROR) {
let decoded = authRes.params;
const decoded = authRes.params;
if (decoded.changesUrl) {
url = decoded.changesUrl;
isInJwtToken = true;
@ -1723,12 +1738,12 @@ exports.downloadFile = function(req, res) {
}
if (fromTemplate) {
ctx.logger.debug('downloadFile from file template: %s', fromTemplate);
let locale = constants.TEMPLATES_DEFAULT_LOCALE;
let fileTemplatePath = pathModule.join(tenNewFileTemplate, locale, 'new.' + fromTemplate);
const locale = constants.TEMPLATES_DEFAULT_LOCALE;
const fileTemplatePath = pathModule.join(tenNewFileTemplate, locale, 'new.' + fromTemplate);
res.sendFile(pathModule.resolve(fileTemplatePath));
} else {
if (utils.canIncludeOutboxAuthorization(ctx, url)) {
let secret = yield tenantManager.getTenantSecret(ctx, commonDefines.c_oAscSecretType.Outbox);
const secret = yield tenantManager.getTenantSecret(ctx, commonDefines.c_oAscSecretType.Outbox);
authorization = utils.fillJwtForRequest(ctx, {url: url}, secret, false);
}
let urlParsed = urlModule.parse(url);
@ -1746,7 +1761,9 @@ exports.downloadFile = function(req, res) {
headers['Range'] = req.get('Range');
}
const { response, stream } = yield utils.downloadUrlPromise(ctx, url, tenDownloadTimeout, tenDownloadMaxBytes, authorization, isInJwtToken, headers, true);
const downloadResult = yield utils.downloadUrlPromise(ctx, url, tenDownloadTimeout, tenDownloadMaxBytes, authorization, isInJwtToken, headers, true);
const response = downloadResult.response;
stream = downloadResult.stream;
//Set-Cookie resets browser session
delete response.headers['set-cookie'];
// Set the response headers to match the target response
@ -1763,6 +1780,9 @@ exports.downloadFile = function(req, res) {
catch (err) {
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
ctx.logger.debug('Error downloadFile: %s', err.stack);
if (!res.headersSent) {
res.sendStatus(499);
}
} else {
ctx.logger.error('Error downloadFile: %s', err.stack);
//catch errors because status may be sent while piping to response
@ -1772,8 +1792,8 @@ exports.downloadFile = function(req, res) {
res.sendStatus(408);
} else if (err.code === 'EMSGSIZE') {
res.sendStatus(413);
} else if (err.response) {
res.sendStatus(err.response.statusCode);
} else if (err.statusCode) {
res.sendStatus(err.statusCode);
} else {
res.sendStatus(400);
}
@ -1784,6 +1804,14 @@ exports.downloadFile = function(req, res) {
}
}
finally {
// Ensure stream is properly destroyed
if (stream && typeof stream.destroy === 'function') {
try {
stream.destroy();
} catch (destroyErr) {
ctx.logger.warn('Error destroying stream: %s', destroyErr.stack);
}
}
ctx.logger.info('End downloadFile');
}
});