mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 03:43:34 +00:00 
			
		
		
		
	This fixes the `generateFileName` function in the Strapi upload plugin, so that reserved and unsafe charactes for URLs are replaced with underscores. Signed-off-by: Marvin König <dev@mkqavi.com>
		
			
				
	
	
		
			41 lines
		
	
	
		
			894 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			894 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
'use strict';
 | 
						|
 | 
						|
const slugify = require('@sindresorhus/slugify');
 | 
						|
 | 
						|
const nameToSlug = (name, options = { separator: '-' }) => slugify(name, options);
 | 
						|
 | 
						|
const nameToCollectionName = name => slugify(name, { separator: '_' });
 | 
						|
 | 
						|
const getCommonBeginning = (str1 = '', str2 = '') => {
 | 
						|
  let common = '';
 | 
						|
  let index = 0;
 | 
						|
  while (index < str1.length && index < str2.length) {
 | 
						|
    if (str1[index] === str2[index]) {
 | 
						|
      common += str1[index];
 | 
						|
      index += 1;
 | 
						|
    } else {
 | 
						|
      break;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return common;
 | 
						|
};
 | 
						|
 | 
						|
const escapeQuery = (query, charsToEscape, escapeChar = '\\') => {
 | 
						|
  return query
 | 
						|
    .split('')
 | 
						|
    .reduce(
 | 
						|
      (escapedQuery, char) =>
 | 
						|
        charsToEscape.includes(char)
 | 
						|
          ? `${escapedQuery}${escapeChar}${char}`
 | 
						|
          : `${escapedQuery}${char}`,
 | 
						|
      ''
 | 
						|
    );
 | 
						|
};
 | 
						|
 | 
						|
module.exports = {
 | 
						|
  nameToSlug,
 | 
						|
  nameToCollectionName,
 | 
						|
  getCommonBeginning,
 | 
						|
  escapeQuery,
 | 
						|
};
 |