Consider getting file extension from the url if response headers are not useful
MODIFY
lib/basedriver/helpers.js
lib/basedriver/helpers.js
@@ -21,6 +21,7 @@ const APPLICATIONS_CACHE = new LRU({
max: 100,
});
const APPLICATIONS_CACHE_GUARD = new AsyncLock();
+const SANITIZE_REPLACEMENT = '-';
async function retrieveHeaders (link) {
try {
@@ -93,30 +94,40 @@ async function configureApp (app, supportedAppExtensions) {
logger.info(`The application at '${cachedPath}' does not exist anymore. Deleting it from the cache`);
APPLICATIONS_CACHE.del(app);
}
- let fileName = `appium-app${_.first(supportedAppExtensions)}`;
+ let fileName = null;
// to determine if we need to unzip the app, we have a number of places
// to look: content type, content disposition, or the file extension
- const downloadZipName = 'appium-app.zip';
+ if (ZIP_EXTS.includes(path.extname(newApp))) {
+ fileName = sanitize(path.basename(newApp), {
+ replacement: SANITIZE_REPLACEMENT
+ });
+ shouldUnzipApp = true;
+ }
if (headers['content-type']) {
logger.debug(`Content-Type: ${headers['content-type']}`);
// the filetype may not be obvious for certain urls, so check the mime type too
if (ZIP_MIME_TYPES.includes(headers['content-type'])) {
- fileName = downloadZipName;
- shouldUnzipApp = true;
+ if (!fileName) {
+ fileName = 'appium-app.zip';
}
- } else if (ZIP_EXTS.includes(path.extname(newApp))) {
- fileName = downloadZipName;
shouldUnzipApp = true;
}
+ }
if (headers['content-disposition'] && /^attachment/i.test(headers['content-disposition'])) {
const match = /filename="([^"]+)/i.exec(headers['content-disposition']);
if (match) {
logger.debug(`Parsed file name '${match[1]}' from 'Content-Disposition' header`);
- fileName = sanitize(match[1], {replacement: '-'});
+ fileName = sanitize(match[1], {
+ replacement: SANITIZE_REPLACEMENT
+ });
shouldUnzipApp = shouldUnzipApp || ZIP_EXTS.includes(path.extname(fileName));
}
}
+ if (!fileName) {
+ // assign the default extension if none has been detected
+ fileName = `appium-app${_.first(supportedAppExtensions)}`;
+ }
const targetPath = await tempDir.path({
prefix: fileName,
suffix: '',

Consider getting file extension from the url if response headers are not useful (#288)