Skip to content

Commit

Permalink
[frontend] fix to upload files to folders with non ascii names for s3…
Browse files Browse the repository at this point in the history
… and abfs (#2907)

Co-authored-by: Björn Alm <balm@cloudera.com>
  • Loading branch information
bjornalm and bjornalm committed Jun 28, 2022
1 parent 91062b8 commit f5407f9
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions desktop/core/src/desktop/js/ext/fileuploader.custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ import $ from 'jquery';
// Helper functions
//

const splitProtocolAndPathName = (fullUrl) => {
// The path name can contain one or more questionmarks (?) that are NOT specifying
// the search/query start for these urls so we use a custom regex instead of URL() to parse.
const splitRegex = /^(\w*:)?(.*)$/i;
const [match, protocol, pathName] = splitRegex.exec(fullUrl);
return [protocol ?? '', pathName ]
}

const isUnicodeAlphaNumeric = /^[\p{L}\p{N}]*$/u;
const encodeUploadPath = (char) => {
const needsEncoding = !isUnicodeAlphaNumeric.test(char) && char !== '/' && char !== '?';
return needsEncoding ? encodeURIComponent(char) : char;
}

/**
* Fixes encoding issues with the destination folder url.
* Some special non-alphanumeric characters like plus (+) and ampersand (&) needs encoding
* while the slash (/) which represents a directory and question mark (?) should not be encoded.
* Alphanumeric characters that are non ASCII (e.g. åäö) should not be encoded since that
* will fail if the target is s3 or abfs.
* @param {string} destination The "destination url" used for the filesystem. Might not be a valid url.
*/
const fixUploadDestination = (destination) => {
const decodedDestination = decodeURIComponent(destination);
const [protocol, path] = splitProtocolAndPathName(decodedDestination);
const updatedPath = [...path].map(encodeUploadPath).join('');
return `${protocol}${updatedPath}`;
}


let qq = {};

Expand Down Expand Up @@ -1260,9 +1289,7 @@ qq.extend(qq.UploadHandlerXhr.prototype, {
formData.append(params.fileFieldLabel, file, file.name.normalize('NFC'));
formData.append('dest', params.dest);

// Encoding is needed to support folder names with some special
// non alfanumeric characters like plus (+) and ampersand (&)
var destination = encodeURIComponent(params.dest);
const destination = fixUploadDestination(params.dest);
var action = this._options.action + "?dest=" + destination;
xhr.open("POST", action, true);
xhr.send(formData);
Expand Down

0 comments on commit f5407f9

Please sign in to comment.