Skip to content

Commit e1488d1

Browse files
committed
MOBILE-1987 filepool: Improve getFileStateByUrl
If a file was being downloaded using downloadUrl, the state returned was "notdownloaded"
1 parent 8e0efc6 commit e1488d1

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

www/core/lib/filepool.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,12 @@ angular.module('mm.core')
740740

741741
if (typeof fileObject === 'undefined') {
742742
// We do not have the file, download and add to pool.
743+
self._notifyFileDownloading(siteId, fileId);
743744
return self._downloadForPoolByUrl(siteId, fileUrl, revision, timemodified, filePath);
744745

745746
} else if (self._isFileOutdated(fileObject, revision, timemodified) && $mmApp.isOnline() && !ignoreStale) {
746747
// The file is outdated, force the download and update it.
748+
self._notifyFileDownloading(siteId, fileId);
747749
return self._downloadForPoolByUrl(siteId, fileUrl, revision, timemodified, filePath, fileObject);
748750
}
749751

@@ -757,11 +759,13 @@ angular.module('mm.core')
757759
return response;
758760
}, function() {
759761
// The file was not found in the pool, weird.
762+
self._notifyFileDownloading(siteId, fileId);
760763
return self._downloadForPoolByUrl(siteId, fileUrl, revision, timemodified, filePath, fileObject);
761764
});
762765

763766
}, function() {
764767
// The file is not in the pool just yet.
768+
self._notifyFileDownloading(siteId, fileId);
765769
return self._downloadForPoolByUrl(siteId, fileUrl, revision, timemodified, filePath);
766770
})
767771
.then(function(response) {
@@ -804,8 +808,6 @@ angular.module('mm.core')
804808
addExtension = typeof filePath == "undefined",
805809
pathPromise = filePath ? filePath : self._getFilePath(siteId, fileId, extension);
806810

807-
self._notifyFileDownloading(siteId, fileId);
808-
809811
return $q.when(pathPromise).then(function(filePath) {
810812
if (poolFileObject && poolFileObject.fileId !== fileId) {
811813
$log.error('Invalid object to update passed');
@@ -1631,9 +1633,10 @@ angular.module('mm.core')
16311633
* @param {String} siteId The site ID.
16321634
* @param {String} fileUrl File URL.
16331635
* @param {Number} [timemodified=0] The time this file was modified.
1636+
* @param {String} [filePath] Filepath to download the file to. If defined, no extension will be added.
16341637
* @return {Promise} Promise resolved with the file state.
16351638
*/
1636-
self.getFileStateByUrl = function(siteId, fileUrl, timemodified) {
1639+
self.getFileStateByUrl = function(siteId, fileUrl, timemodified, filePath) {
16371640
var fileId,
16381641
revision;
16391642

@@ -1646,18 +1649,28 @@ angular.module('mm.core')
16461649
// Restore old file if needed.
16471650
return self._restoreOldFileIfNeeded(siteId, fileId, fileUrl);
16481651
}).then(function() {
1649-
16501652
return self._hasFileInQueue(siteId, fileId).then(function() {
16511653
return mmCoreDownloading;
16521654
}, function() {
1653-
return self._hasFileInPool(siteId, fileId).then(function(fileObject) {
1654-
if (self._isFileOutdated(fileObject, revision, timemodified)) {
1655-
return mmCoreOutdated;
1656-
} else {
1657-
return mmCoreDownloaded;
1655+
// Check if the file is being downloaded right now.
1656+
var extension = $mmFS.guessExtensionFromUrl(fileUrl),
1657+
pathPromise = filePath ? filePath : self._getFilePath(siteId, fileId, extension);
1658+
1659+
return $q.when(pathPromise).then(function(filePath) {
1660+
var downloadId = self.getFileDownloadId(fileUrl, filePath);
1661+
if (filePromises[siteId] && filePromises[siteId][downloadId]) {
1662+
return mmCoreDownloading;
16581663
}
1659-
}, function() {
1660-
return mmCoreNotDownloaded;
1664+
1665+
return self._hasFileInPool(siteId, fileId).then(function(fileObject) {
1666+
if (self._isFileOutdated(fileObject, revision, timemodified)) {
1667+
return mmCoreOutdated;
1668+
} else {
1669+
return mmCoreDownloaded;
1670+
}
1671+
}, function() {
1672+
return mmCoreNotDownloaded;
1673+
});
16611674
});
16621675
});
16631676
});

0 commit comments

Comments
 (0)