diff --git a/services/clsi/app/js/HistoryResourceWriter.js b/services/clsi/app/js/HistoryResourceWriter.js index df5845d02e..6bce9f028e 100644 --- a/services/clsi/app/js/HistoryResourceWriter.js +++ b/services/clsi/app/js/HistoryResourceWriter.js @@ -38,6 +38,16 @@ function isPng(path) { return Path.extname(path).toLowerCase() === '.png' } +/** + * Returns true if the PNG file size (bytes) is below the conversion threshold + * @param {number} fileSize + * @returns {boolean} + */ + +function pngBelowSizeThreshold(fileSize) { + return fileSize < Settings.png2pdfMinFileSizeBytes +} + export const clearCacheCb = callbackify(clearCache) /** @@ -561,6 +571,7 @@ export async function syncResourcesToDisk( 'utf-8' ) } else { + const fileSize = file.getByteLength() || 0 const hash = file.getHash() if (!hash) { throw new OError('unexpected file without content and hash', { path }) @@ -574,7 +585,16 @@ export async function syncResourcesToDisk( try { const fallbackURL = null // no fallback const lastModified = new Date(0) // content is static - if (request.png2pdf && Png2Pdf.isEnabled() && isPng(path)) { + const isConvertiblePng = + request.png2pdf && Png2Pdf.isEnabled() && isPng(path) + // Avoid doing unnecessary work converting small PNGs + const skipConversion = + isConvertiblePng && pngBelowSizeThreshold(fileSize) + if (skipConversion) { + Metrics.inc('png2pdf-skipped-small') + } + // Take the conversion path for PNGs to be optimised + if (isConvertiblePng && !skipConversion) { // PNG files go through a batch conversion process first. const toConvert = await UrlCache.promises.downloadUrlToFile( projectId, diff --git a/services/clsi/config/settings.defaults.cjs b/services/clsi/config/settings.defaults.cjs index 8477cd26d8..d3dfb632d6 100644 --- a/services/clsi/config/settings.defaults.cjs +++ b/services/clsi/config/settings.defaults.cjs @@ -37,6 +37,8 @@ module.exports = { png2pdfImage: process.env.PNG2PDF_IMAGE || 'quay.io/sharelatex/png2pdf:2026-06-24', enablePng2pdfConversions: process.env.ENABLE_PNG2PDF_CONVERSIONS === 'true', + png2pdfMinFileSizeBytes: + parseInt(process.env.PNG2PDF_MIN_FILE_SIZE_BYTES, 10) || 1024 * 1024, maxUploadSize: 50 * 1024 * 1024, preciousFilePattern: process.env.PRECIOUS_FILE_PATTERN || '',