bg 624344 - File name not parsed correctly while trying to save page via 'Save link' r=enn a=blocking-fennec

This commit is contained in:
Alon Zakai 2011-03-04 18:29:01 -05:00
Родитель 39af021012
Коммит 3c7159de0d
1 изменённых файлов: 35 добавлений и 1 удалений

Просмотреть файл

@ -929,7 +929,41 @@ function validateFileName(aFileName)
}
else if (navigator.appVersion.indexOf("Macintosh") != -1)
re = /[\:\/]+/g;
else if (navigator.appVersion.indexOf("Android") != -1 ||
navigator.appVersion.indexOf("Maemo") != -1) {
// On mobile devices, the filesystem may be very limited in what
// it considers valid characters. To avoid errors, we sanitize
// conservatively.
const dangerousChars = "*?<>|\":/\\[];,+=";
var processed = "";
for (var i = 0; i < aFileName.length; i++)
processed += aFileName.charCodeAt(i) >= 32 &&
!(dangerousChars.indexOf(aFileName[i]) >= 0) ? aFileName[i]
: "_";
// Last character should not be a space
processed = processed.trim();
// If a large part of the filename has been sanitized, then we
// will use a default filename instead
if (processed.replace(/_/g, "").length <= processed.length/2) {
// We purposefully do not use a localized default filename,
// which we could have done using
// ContentAreaUtils.stringBundle.GetStringFromName("DefaultSaveFileName")
// since it may contain invalid characters.
var original = processed;
processed = "download";
// Preserve a suffix, if there is one
if (original.indexOf(".") >= 0) {
var suffix = original.split(".").slice(-1)[0];
if (suffix && suffix.indexOf("_") < 0)
processed += "." + suffix;
}
}
return processed;
}
return aFileName.replace(re, "_");
}