Skip to content

Commit 31335b7

Browse files
committed
MOBILE-1987 file: Show error only if user clicked file
1 parent e1488d1 commit 31335b7

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

www/core/directives/file.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ angular.module('mm.core')
8282

8383
scope.isDownloading = true;
8484
return $mmFilepool.downloadUrl(siteId, fileUrl, false, component, componentId, timeModified).then(function(localUrl) {
85-
getState(scope, siteId, fileUrl, timeModified, alwaysDownload); // Update state.
8685
return localUrl;
87-
}, function() {
86+
}).catch(function() {
87+
// Call getState to make sure we have the right state.
8888
return getState(scope, siteId, fileUrl, timeModified, alwaysDownload).then(function() {
8989
if (scope.isDownloaded) {
90-
return localUrl;
90+
return $mmFilepool.getInternalUrlByUrl(siteId, fileUrl);
9191
} else {
9292
return $q.reject();
9393
}
@@ -127,8 +127,10 @@ angular.module('mm.core')
127127
return $q.reject();
128128
}
129129

130+
var isDownloading = scope.isDownloading;
131+
scope.isDownloading = true; // This check could take a while, show spinner.
130132
return $mmFilepool.shouldDownloadBeforeOpen(fixedUrl, fileSize).then(function() {
131-
if (scope.isDownloading) {
133+
if (isDownloading) {
132134
// It's already downloading, stop.
133135
return;
134136
}
@@ -140,7 +142,7 @@ angular.module('mm.core')
140142
downloadFile(scope, siteId, fileUrl, component, componentId, timeModified, alwaysDownload);
141143
}
142144

143-
if (scope.isDownloading || !scope.isDownloaded || isOnline) {
145+
if (isDownloading|| !scope.isDownloaded || isOnline) {
144146
// Not downloaded or outdated and online, return the online URL.
145147
return fixedUrl;
146148
} else {
@@ -201,12 +203,10 @@ angular.module('mm.core')
201203
if (canDownload) {
202204
getState(scope, siteId, fileUrl, timeModified, alwaysDownload);
203205

206+
// Update state when receiving events about this file.
204207
$mmFilepool.getFileEventNameByUrl(siteId, fileUrl).then(function(eventName) {
205-
observer = $mmEvents.on(eventName, function(data) {
208+
observer = $mmEvents.on(eventName, function() {
206209
getState(scope, siteId, fileUrl, timeModified, alwaysDownload);
207-
if (data.action == 'download' && !data.success) {
208-
$mmUtil.showErrorModal('mm.core.errordownloading', true);
209-
}
210210
});
211211
});
212212
}
@@ -229,7 +229,7 @@ angular.module('mm.core')
229229
// File needs to be opened now. If file needs to be downloaded, skip the queue.
230230
openFile(scope, siteId, fileUrl, fileSize, component, componentId, timeModified, alwaysDownload)
231231
.catch(function(error) {
232-
$mmUtil.showErrorModal(error);
232+
$mmUtil.showErrorModalDefault(error, 'mm.core.errordownloading', true);
233233
});
234234
} else {
235235
// File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big.
@@ -238,7 +238,9 @@ angular.module('mm.core')
238238
// User confirmed, add the file to queue.
239239
$mmFilepool.invalidateFileByUrl(siteId, fileUrl).finally(function() {
240240
scope.isDownloading = true;
241-
$mmFilepool.addToQueueByUrl(siteId, fileUrl, component, componentId, timeModified);
241+
$mmFilepool.addToQueueByUrl(siteId, fileUrl, component, componentId, timeModified).catch(function() {
242+
$mmUtil.showErrorModal('mm.core.errordownloading', true);
243+
});
242244
});
243245
});
244246
}

www/core/lib/filepool.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ angular.module('mm.core')
12461246
*
12471247
* @module mm.core
12481248
* @ngdoc method
1249-
* @name $mmFilepool#getInternalUrlByUrl
1249+
* @name $mmFilepool#getDirectoryUrlByUrl
12501250
* @param {String} siteId The site ID.
12511251
* @param {String} fileUrl The file URL.
12521252
* @return {Promise} Resolved with the URL. Rejected otherwise.
@@ -1744,6 +1744,26 @@ angular.module('mm.core')
17441744
return $q.reject();
17451745
};
17461746

1747+
/**
1748+
* Returns the local URL of a file.
1749+
*
1750+
* @module mm.core
1751+
* @ngdoc method
1752+
* @name $mmFilepool#getInternalUrlByUrl
1753+
* @param {String} siteId The site ID.
1754+
* @param {String} fileUrl The file URL.
1755+
* @return {Promise} Resolved with the URL. Rejected otherwise.
1756+
*/
1757+
self.getInternalUrlByUrl = function(siteId, fileUrl) {
1758+
if ($mmFS.isAvailable()) {
1759+
return self._fixPluginfileURL(siteId, fileUrl).then(function(fileUrl) {
1760+
var fileId = self._getFileIdByUrl(fileUrl);
1761+
return self._getInternalUrlById(siteId, fileId);
1762+
});
1763+
}
1764+
return $q.reject();
1765+
};
1766+
17471767
/**
17481768
* Get the path to a directory to store a package files. We use the old implementation of getFileId.
17491769
*

0 commit comments

Comments
 (0)