Skip to content

Commit

Permalink
Fix richardgirges#342 URI malformed error
Browse files Browse the repository at this point in the history
  • Loading branch information
boly38 committed Dec 15, 2022
1 parent 4f81fc8 commit 30809b6
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const copyFile = (src, dst, callback) => {

/**
* moveFile: moves the file from src to dst.
* Firstly trying to rename the file if no luck copying it to dst and then deleteing src.
* First try to rename the file if no luck copy it to dst and then delete src.
* @param {string} src - Path to the source file
* @param {string} dst - Path to the destination file.
* @param {Function} callback - A callback function.
Expand Down Expand Up @@ -224,12 +224,26 @@ const saveBufferToFile = (buffer, filePath, callback) => {
};

/**
* Decodes uriEncoded file names.
* Decodes uriEncoded file names
* and remove invalid char range
* @param opts {Object} - { uriDecodeFileNames } options
* @param fileName {String} - file name to decode.
* @returns {String}
*/
const uriDecodeFileName = (opts, fileName) => {
return opts.uriDecodeFileNames ? decodeURIComponent(fileName) : fileName;
if (opts.uriDecodeFileNames && fileName) {
try {
// ignore invalid char ranges
var byteLike = unescape(encodeURIComponent(fileName));
var decodedFileName = decodeURIComponent(escape(byteLike));
} catch (error) {
// ignore decode error
}
// remove �
decodedFileName = decodedFileName.replace(/\uFFFD/g, '');
return decodedFileName;
}
return fileName;
};

/**
Expand Down Expand Up @@ -275,7 +289,7 @@ const parseFileNameExtension = (preserveExtension, fileName) => {
const parseFileName = (opts, fileName) => {
// Check fileName argument
if (!fileName || typeof fileName !== 'string') return getTempFilename();
// Cut off file name if it's lenght more then 255.
// Cut off file name if it's length more then 255.
let parsedName = fileName.length <= 255 ? fileName : fileName.substr(0, 255);
// Decode file name if uriDecodeFileNames option set true.
parsedName = uriDecodeFileName(opts, parsedName);
Expand Down

0 comments on commit 30809b6

Please sign in to comment.