Merge pull request #35333 from overleaf/bg-png2pdf-minimum-size-threshold

enforce a minimum size threshold on png2pdf conversions

GitOrigin-RevId: fa558afb5455ff5d0373e3d50e57f7207a923fcb
This commit is contained in:
Brian Gough
2026-09-17 11:29:09 +00:00
committed by Copybot
parent c3fa2bf304
commit aad50452af
2 changed files with 23 additions and 1 deletions
+21 -1
View File
@@ -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,
@@ -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 || '',