@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_resource')
2222 * @name $mmaModResource
2323 */
2424. factory ( '$mmaModResource' , function ( $mmFilepool , $mmSite , $mmUtil , $mmFS , $http , $log , $q , $sce , $mmApp , $mmSitesManager ,
25- mmaModResourceComponent ) {
25+ mmaModResourceComponent , mmCoreNotDownloaded , mmCoreDownloading , mmCoreDownloaded ) {
2626 $log = $log . getInstance ( '$mmaModResource' ) ;
2727
2828 var self = { } ;
@@ -356,20 +356,61 @@ angular.module('mm.addons.mod_resource')
356356 siteId = $mmSite . getId ( ) ,
357357 revision = $mmFilepool . getRevisionFromFileList ( files ) ,
358358 timeMod = $mmFilepool . getTimemodifiedFromFileList ( files ) ,
359+ component = mmaModResourceComponent ,
360+ url = contents [ 0 ] . fileurl ,
361+ fixedUrl = $mmSite . fixPluginfileURL ( url ) ,
359362 promise ;
360363
361364 if ( $mmFS . isAvailable ( ) ) {
362365 // The file system is available.
363- promise = $mmFilepool . downloadPackage ( siteId , files , mmaModResourceComponent , moduleId , revision , timeMod ) . then ( function ( ) {
364- return $mmFilepool . getUrlByUrl ( siteId , contents [ 0 ] . fileurl , mmaModResourceComponent , moduleId , timeMod ) ;
366+ promise = $mmFilepool . getPackageStatus ( siteId , component , moduleId , revision , timeMod ) . then ( function ( status ) {
367+ var isWifi = ! $mmApp . isNetworkAccessLimited ( ) ,
368+ isOnline = $mmApp . isOnline ( ) ;
369+
370+ if ( status === mmCoreDownloaded ) {
371+ // Get the local file URL.
372+ return $mmFilepool . getUrlByUrl ( siteId , url , component , moduleId , timeMod ) ;
373+ } else if ( status === mmCoreDownloading ) {
374+ // Return the online URL.
375+ return fixedUrl ;
376+ } else {
377+ if ( ! isOnline && status === mmCoreNotDownloaded ) {
378+ // Not downloaded and we're offline, reject.
379+ return $q . reject ( ) ;
380+ }
381+
382+ return $mmFilepool . shouldDownloadBeforeOpen ( fixedUrl , contents [ 0 ] . filesize ) . then ( function ( ) {
383+ // Download and then return the local URL.
384+ return $mmFilepool . downloadPackage ( siteId , files , component , moduleId , revision , timeMod ) . then ( function ( ) {
385+ return $mmFilepool . getUrlByUrl ( siteId , url , component , moduleId , timeMod ) ;
386+ } ) ;
387+ } , function ( ) {
388+ // Start the download if in wifi, but return the URL right away so the file is opened.
389+ if ( isWifi && isOnline ) {
390+ $mmFilepool . downloadPackage ( siteId , files , component , moduleId , revision , timeMod ) ;
391+ }
392+
393+ if ( status === mmCoreNotDownloaded || isOnline ) {
394+ // Not downloaded or outdated and online, return the online URL.
395+ return fixedUrl ;
396+ } else {
397+ // Outdated but offline, so we return the local URL.
398+ return $mmFilepool . getUrlByUrl ( siteId , url , component , moduleId , timeMod ) ;
399+ }
400+ } ) ;
401+ }
365402 } ) ;
366403 } else {
367404 // We use the live URL.
368- promise = $q . when ( $mmSite . fixPluginfileURL ( url ) ) ;
405+ promise = $q . when ( fixedUrl ) ;
369406 }
370407
371- return promise . then ( function ( localUrl ) {
372- return $mmUtil . openFile ( localUrl ) ;
408+ return promise . then ( function ( url ) {
409+ if ( url . indexOf ( 'http' ) === 0 ) {
410+ return $mmUtil . openOnlineFile ( url ) ;
411+ } else {
412+ return $mmUtil . openFile ( url ) ;
413+ }
373414 } ) ;
374415 } ;
375416
0 commit comments