Files
klz-cables.com/.pnpm-store/v10/files/c4/3ad7a5ef29e750a5c213067ae4e717fef5134281e6a923ddb82bbedf29f38a55193ac420901c1c7fd4ffdfc7071ceb9956d4284420817b73ef3ccda8594ee3
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

60 lines
2.3 KiB
Plaintext

import sanitize from 'sanitize-filename';
import { docWithFilenameExists } from './docWithFilenameExists.js';
import { fileExists } from './fileExists.js';
/**
* Increments a filename by appending or incrementing a numeric suffix.
* @example
* incrementName('file.jpg') // 'file-1.jpg'
* incrementName('file-1.jpg') // 'file-2.jpg'
* incrementName('file-99.jpg') // 'file-100.jpg'
*/ export const incrementName = (name)=>{
const extension = name.split('.').pop();
const baseFilename = sanitize(name.substring(0, name.lastIndexOf('.')) || name);
let incrementedName = baseFilename;
const regex = /(.*)-(\d+)$/;
const found = baseFilename.match(regex);
if (found === null) {
incrementedName += '-1';
} else {
const matchedName = found[1];
const matchedNumber = found[2];
const incremented = Number(matchedNumber) + 1;
incrementedName = `${matchedName}-${incremented}`;
}
return `${incrementedName}.${extension}`;
};
/**
* Generates a safe, unique filename by checking for conflicts in both the database
* and filesystem. If a conflict exists, it increments a numeric suffix until a
* unique name is found.
*
* @param args.collectionSlug - The slug of the upload collection
* @param args.desiredFilename - The original filename to make safe
* @param args.prefix - Optional prefix path for cloud storage adapters
* @param args.req - The Payload request object
* @param args.staticPath - The filesystem path where uploads are stored
* @returns A unique filename that doesn't conflict with existing files
*
* @example
* // If 'photo.jpg' already exists, returns 'photo-1.jpg'
* const safeName = await getSafeFileName({
* collectionSlug: 'media',
* desiredFilename: 'photo.jpg',
* req,
* staticPath: '/uploads/media',
* })
*/ export async function getSafeFileName({ collectionSlug, desiredFilename, prefix, req, staticPath }) {
let modifiedFilename = desiredFilename;
while(await docWithFilenameExists({
collectionSlug,
filename: modifiedFilename,
path: staticPath,
prefix,
req
}) || await fileExists(`${staticPath}/${modifiedFilename}`)){
modifiedFilename = incrementName(modifiedFilename);
}
return modifiedFilename;
}
//# sourceMappingURL=getSafeFilename.js.map