@@ -40,13 +40,13 @@ angular.module('mm.core')
4040 * Convenience function to get the file state and set scope variables based on it.
4141 *
4242 * @param {Object } scope Directive's scope.
43- * @param {String } siteid Site ID.
44- * @param {String } fileurl File URL.
45- * @param {Number } [timemodified ] File's timemodified.
43+ * @param {String } siteId Site ID.
44+ * @param {String } fileUrl File URL.
45+ * @param {Number } [timeModified ] File's timemodified.
4646 * @return {Void }
4747 */
48- function getState ( scope , siteid , fileurl , timemodified ) {
49- return $mmFilepool . getFileStateByUrl ( siteid , fileurl , timemodified ) . then ( function ( state ) {
48+ function getState ( scope , siteId , fileUrl , timeModified ) {
49+ return $mmFilepool . getFileStateByUrl ( siteId , fileUrl , timeModified ) . then ( function ( state ) {
5050 var canDownload = $mmSite . canDownloadFiles ( ) ;
5151 scope . isDownloaded = state === mmCoreDownloaded || state === mmCoreOutdated ;
5252 scope . isDownloading = canDownload && state === mmCoreDownloading ;
@@ -58,25 +58,25 @@ angular.module('mm.core')
5858 * Convenience function to download a file.
5959 *
6060 * @param {Object } scope Directive's scope.
61- * @param {String } siteid Site ID.
62- * @param {String } fileurl File URL.
61+ * @param {String } siteId Site ID.
62+ * @param {String } fileUrl File URL.
6363 * @param {String } component Component the file belongs to.
64- * @param {Number } componentid Component ID.
65- * @param {Number } [timemodified ] File's timemodified.
64+ * @param {Number } componentId Component ID.
65+ * @param {Number } [timeModified ] File's timemodified.
6666 * @return {Promise } Promise resolved when file is downloaded.
6767 */
68- function downloadFile ( scope , siteid , fileurl , component , componentid , timemodified ) {
68+ function downloadFile ( scope , siteId , fileUrl , component , componentId , timeModified ) {
6969 if ( ! $mmSite . canDownloadFiles ( ) ) {
7070 $mmUtil . showErrorModal ( 'mm.core.cannotdownloadfiles' , true ) ;
7171 return $q . reject ( ) ;
7272 }
7373
7474 scope . isDownloading = true ;
75- return $mmFilepool . downloadUrl ( siteid , fileurl , true , component , componentid , timemodified ) . then ( function ( localUrl ) {
76- getState ( scope , siteid , fileurl , timemodified ) ; // Update state.
75+ return $mmFilepool . downloadUrl ( siteId , fileUrl , false , component , componentId , timeModified ) . then ( function ( localUrl ) {
76+ getState ( scope , siteId , fileUrl , timeModified ) ; // Update state.
7777 return localUrl ;
7878 } , function ( ) {
79- return getState ( scope , siteid , fileurl , timemodified ) . then ( function ( ) {
79+ return getState ( scope , siteId , fileUrl , timeModified ) . then ( function ( ) {
8080 if ( scope . isDownloaded ) {
8181 return localUrl ;
8282 } else {
@@ -86,29 +86,101 @@ angular.module('mm.core')
8686 } ) ;
8787 }
8888
89+ /**
90+ * Convenience function to open a file, downloading it if needed.
91+ *
92+ * @param {Object } scope Directive's scope.
93+ * @param {String } siteId Site ID.
94+ * @param {String } fileUrl File URL.
95+ * @param {String } fileSize File size.
96+ * @param {String } component Component the file belongs to.
97+ * @param {Number } componentId Component ID.
98+ * @param {Number } [timeModified] File's timemodified.
99+ * @return {Promise } Promise resolved when file is opened.
100+ */
101+ function openFile ( scope , siteId , fileUrl , fileSize , component , componentId , timeModified ) {
102+ var fixedUrl = $mmSite . fixPluginfileURL ( fileUrl ) ,
103+ promise ;
104+
105+ if ( $mmFS . isAvailable ( ) ) {
106+ promise = $q . when ( ) . then ( function ( ) {
107+ // The file system is available.
108+ var isWifi = ! $mmApp . isNetworkAccessLimited ( ) ,
109+ isOnline = $mmApp . isOnline ( ) ;
110+
111+ if ( scope . isDownloaded && ! scope . showDownload ) {
112+ // Get the local file URL.
113+ return $mmFilepool . getUrlByUrl ( siteId , fileUrl , component , componentId , timeModified ) ;
114+ } else {
115+ if ( ! isOnline && ! scope . isDownloaded ) {
116+ // Not downloaded and we're offline, reject.
117+ return $q . reject ( ) ;
118+ }
119+
120+ return $mmFilepool . shouldDownloadBeforeOpen ( fixedUrl , fileSize ) . then ( function ( ) {
121+ if ( scope . isDownloading ) {
122+ // It's already downloading, stop.
123+ return ;
124+ }
125+ // Download and then return the local URL.
126+ return downloadFile ( scope , siteId , fileUrl , component , componentId , timeModified ) ;
127+ } , function ( ) {
128+ // Start the download if in wifi, but return the URL right away so the file is opened.
129+ if ( isWifi && isOnline ) {
130+ downloadFile ( scope , siteId , fileUrl , component , componentId , timeModified ) ;
131+ }
132+
133+ if ( scope . isDownloading || ! scope . isDownloaded || isOnline ) {
134+ // Not downloaded or outdated and online, return the online URL.
135+ return fixedUrl ;
136+ } else {
137+ // Outdated but offline, so we return the local URL.
138+ return $mmFilepool . getUrlByUrl ( siteId , fileUrl , component , componentId , timeModified ) ;
139+ }
140+ } ) ;
141+ }
142+ } ) ;
143+ } else {
144+ // We use the live URL.
145+ promise = $q . when ( fixedUrl ) ;
146+ }
147+
148+ return promise . then ( function ( url ) {
149+ if ( ! url ) {
150+ return ;
151+ }
152+
153+ if ( url . indexOf ( 'http' ) === 0 ) {
154+ return $mmUtil . openOnlineFile ( url ) ;
155+ } else {
156+ return $mmUtil . openFile ( url ) ;
157+ }
158+ } ) ;
159+ }
160+
89161 return {
90162 restrict : 'E' ,
91163 templateUrl : 'core/templates/file.html' ,
92164 scope : {
93165 file : '='
94166 } ,
95167 link : function ( scope , element , attrs ) {
96- var fileurl = scope . file . fileurl || scope . file . url ,
97- filename = scope . file . filename ,
98- filesize = scope . file . filesize ,
99- timemodified = attrs . timemodified || 0 ,
100- siteid = $mmSite . getId ( ) ,
168+ var fileUrl = scope . file . fileurl || scope . file . url ,
169+ fileName = scope . file . filename ,
170+ fileSize = scope . file . filesize ,
171+ timeModified = attrs . timemodified || 0 ,
172+ siteId = $mmSite . getId ( ) ,
101173 component = attrs . component ,
102- componentid = attrs . componentId ,
174+ componentId = attrs . componentId ,
103175 observer ;
104176
105- scope . filename = filename ;
106- scope . fileicon = $mmFS . getFileIcon ( filename ) ;
107- getState ( scope , siteid , fileurl , timemodified ) ;
177+ scope . filename = fileName ;
178+ scope . fileicon = $mmFS . getFileIcon ( fileName ) ;
179+ getState ( scope , siteId , fileUrl , timeModified ) ;
108180
109- $mmFilepool . getFileEventNameByUrl ( siteid , fileurl ) . then ( function ( eventName ) {
181+ $mmFilepool . getFileEventNameByUrl ( siteId , fileUrl ) . then ( function ( eventName ) {
110182 observer = $mmEvents . on ( eventName , function ( data ) {
111- getState ( scope , siteid , fileurl , timemodified ) ;
183+ getState ( scope , siteId , fileUrl , timeModified ) ;
112184 if ( ! data . success ) {
113185 $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
114186 }
@@ -120,7 +192,7 @@ angular.module('mm.core')
120192 e . stopPropagation ( ) ;
121193 var promise ;
122194
123- if ( scope . isDownloading ) {
195+ if ( scope . isDownloading && ! openAfterDownload ) {
124196 return ;
125197 }
126198
@@ -131,19 +203,17 @@ angular.module('mm.core')
131203
132204 if ( openAfterDownload ) {
133205 // File needs to be opened now. If file needs to be downloaded, skip the queue.
134- downloadFile ( scope , siteid , fileurl , component , componentid , timemodified ) . then ( function ( localUrl ) {
135- $mmUtil . openFile ( localUrl ) . catch ( function ( error ) {
136- $mmUtil . showErrorModal ( error ) ;
137- } ) ;
206+ openFile ( scope , siteId , fileUrl , fileSize , component , componentId , timeModified ) . catch ( function ( error ) {
207+ $mmUtil . showErrorModal ( error ) ;
138208 } ) ;
139209 } else {
140210 // File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big.
141- promise = filesize ? $mmUtil . confirmDownloadSize ( filesize ) : $q . when ( ) ;
211+ promise = fileSize ? $mmUtil . confirmDownloadSize ( fileSize ) : $q . when ( ) ;
142212 promise . then ( function ( ) {
143213 // User confirmed, add the file to queue.
144- $mmFilepool . invalidateFileByUrl ( siteid , fileurl ) . finally ( function ( ) {
214+ $mmFilepool . invalidateFileByUrl ( siteId , fileUrl ) . finally ( function ( ) {
145215 scope . isDownloading = true ;
146- $mmFilepool . addToQueueByUrl ( siteid , fileurl , component , componentid , timemodified ) ;
216+ $mmFilepool . addToQueueByUrl ( siteId , fileUrl , component , componentId , timeModified ) ;
147217 } ) ;
148218 } ) ;
149219 }
0 commit comments