@@ -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 }
0 commit comments