Skip to content

Commit ca86532

Browse files
committed
Merge pull request #293 from dpalou/MOBILE-1324
MOBILE-1324 prefetch: Fix revision and timemod wrong calculation
2 parents d7ae264 + fc12f10 commit ca86532

File tree

5 files changed

+81
-56
lines changed

5 files changed

+81
-56
lines changed

www/addons/mod_book/services/book.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ angular.module('mm.addons.mod_book')
3636
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
3737
*/
3838
self.downloadAllContent = function(module) {
39-
var files = self.getDownloadableFiles(module);
40-
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModBookComponent, module.id);
39+
var files = self.getDownloadableFiles(module),
40+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
41+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
42+
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModBookComponent, module.id, revision, timemod);
4143
};
4244

4345
/**
@@ -369,8 +371,10 @@ angular.module('mm.addons.mod_book')
369371
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
370372
*/
371373
self.prefetchContent = function(module) {
372-
var files = self.getDownloadableFiles(module);
373-
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModBookComponent, module.id);
374+
var files = self.getDownloadableFiles(module),
375+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
376+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
377+
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModBookComponent, module.id, revision, timemod);
374378
};
375379

376380
return self;

www/addons/mod_imscp/services/imscp.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,12 @@ angular.module('mm.addons.mod_imscp')
137137
* @return {Promise} Promise resolved when content is downloaded. Data returned is not reliable.
138138
*/
139139
self.downloadAllContent = function(module) {
140-
var files = self.getDownloadableFiles(module);
140+
var files = self.getDownloadableFiles(module),
141+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
142+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
143+
141144
return $mmFilepool.getFilePathByUrl($mmSite.getId(), module.url).then(function(dirPath) {
142-
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModImscpComponent, module.id, dirPath);
145+
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModImscpComponent, module.id, revision, timemod, dirPath);
143146
});
144147
};
145148

@@ -369,9 +372,12 @@ angular.module('mm.addons.mod_imscp')
369372
* @return {Promise} Promise resolved when content is downloaded. Data returned is not reliable.
370373
*/
371374
self.prefetchContent = function(module) {
372-
var files = self.getDownloadableFiles(module);
375+
var files = self.getDownloadableFiles(module),
376+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
377+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
378+
373379
return $mmFilepool.getFilePathByUrl($mmSite.getId(), module.url).then(function(dirPath) {
374-
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModImscpComponent, module.id, dirPath);
380+
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModImscpComponent, module.id, revision, timemod, dirPath);
375381
});
376382
};
377383

www/addons/mod_page/services/page.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ angular.module('mm.addons.mod_page')
3636
* @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable.
3737
*/
3838
self.downloadAllContent = function(module) {
39-
var files = self.getDownloadableFiles(module);
40-
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModPageComponent, module.id);
39+
var files = self.getDownloadableFiles(module),
40+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
41+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
42+
return $mmFilepool.downloadPackage($mmSite.getId(), files, mmaModPageComponent, module.id, revision, timemod);
4143
};
4244

4345
/**
@@ -272,8 +274,10 @@ angular.module('mm.addons.mod_page')
272274
* @return {Promise} Promise resolved when all files have been downloaded. Data returned is not reliable.
273275
*/
274276
self.prefetchContent = function(module) {
275-
var files = self.getDownloadableFiles(module);
276-
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModPageComponent, module.id);
277+
var files = self.getDownloadableFiles(module),
278+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
279+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
280+
return $mmFilepool.prefetchPackage($mmSite.getId(), files, mmaModPageComponent, module.id, revision, timemod);
277281
};
278282

279283
return self;

www/addons/mod_resource/services/resource.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ angular.module('mm.addons.mod_resource')
3939
self.downloadAllContent = function(module) {
4040
var files = self.getDownloadableFiles(module),
4141
siteid = $mmSite.getId(),
42-
promise;
42+
promise,
43+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
44+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
4345

4446
if (self.isDisplayedInIframe(module)) {
4547
// Get path of the module folder in filepool.
@@ -49,7 +51,7 @@ angular.module('mm.addons.mod_resource')
4951
}
5052

5153
return promise.then(function(dirPath) {
52-
return $mmFilepool.downloadPackage(siteid, files, mmaModResourceComponent, module.id, dirPath);
54+
return $mmFilepool.downloadPackage(siteid, files, mmaModResourceComponent, module.id, revision, timemod, dirPath);
5355
});
5456
};
5557

@@ -368,7 +370,9 @@ angular.module('mm.addons.mod_resource')
368370
self.prefetchContent = function(module) {
369371
var files = self.getDownloadableFiles(module),
370372
siteid = $mmSite.getId(),
371-
promise;
373+
promise,
374+
revision = $mmFilepool.getRevisionFromFileList(module.contents),
375+
timemod = $mmFilepool.getTimemodifiedFromFileList(module.contents);
372376

373377
if (self.isDisplayedInIframe(module)) {
374378
// Get path of the module folder in filepool.
@@ -378,7 +382,7 @@ angular.module('mm.addons.mod_resource')
378382
}
379383

380384
return promise.then(function(dirPath) {
381-
return $mmFilepool.prefetchPackage(siteid, files, mmaModResourceComponent, module.id, dirPath);
385+
return $mmFilepool.prefetchPackage(siteid, files, mmaModResourceComponent, module.id, revision, timemod, dirPath);
382386
});
383387
};
384388

www/core/lib/filepool.js

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,14 @@ angular.module('mm.core')
580580
* @param {Boolean} prefetch True if should prefetch the contents (queue), false if they should be downloaded right now.
581581
* @param {String} component The component to link the file to.
582582
* @param {Number} [componentId] An ID to use in conjunction with the component.
583+
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
584+
* @param {Number} [timemod] Package's timemodified. If not defined, it will be calculated using the list of files.
583585
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
584586
* the files directly inside the filepool folder.
585587
* @return {Promise} Promise resolved when all files are downloaded.
586588
* @protected
587589
*/
588-
self._downloadOrPrefetchPackage = function(siteId, fileList, prefetch, component, componentId, dirPath) {
590+
self._downloadOrPrefetchPackage = function(siteId, fileList, prefetch, component, componentId, revision, timemod, dirPath) {
589591

590592
var packageId = self.getPackageId(component, componentId);
591593

@@ -596,9 +598,10 @@ angular.module('mm.core')
596598
packagesPromises[siteId] = {};
597599
}
598600

599-
var revision = self.getRevisionFromFileList(fileList),
600-
timemod = self.getTimemodifiedFromFileList(fileList),
601-
dwnPromise,
601+
revision = revision || self.getRevisionFromFileList(fileList);
602+
timemod = timemod || self.getTimemodifiedFromFileList(fileList);
603+
604+
var dwnPromise,
602605
deleted = false;
603606

604607
// Set package as downloading.
@@ -651,16 +654,18 @@ angular.module('mm.core')
651654
* @module mm.core
652655
* @ngdoc method
653656
* @name $mmFilepool#downloadPackage
654-
* @param {String} siteId The site ID.
655-
* @param {Object[]} fileList List of files to download.
656-
* @param {String} component The component to link the file to.
657-
* @param {Number} componentId An ID to identify the download. Must be unique.
658-
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
659-
* the files directly inside the filepool folder.
660-
* @return {Promise} Promise resolved when all files are downloaded.
657+
* @param {String} siteId The site ID.
658+
* @param {Object[]} fileList List of files to download.
659+
* @param {String} component The component to link the file to.
660+
* @param {Number} componentId An ID to identify the download. Must be unique.
661+
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
662+
* @param {Number} [timemodified] Package's timemodified. If not defined, it will be calculated using the list of files.
663+
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
664+
* the files directly inside the filepool folder.
665+
* @return {Promise} Promise resolved when all files are downloaded.
661666
*/
662-
self.downloadPackage = function(siteId, fileList, component, componentId, dirPath) {
663-
return self._downloadOrPrefetchPackage(siteId, fileList, false, component, componentId, dirPath);
667+
self.downloadPackage = function(siteId, fileList, component, componentId, revision, timemodified, dirPath) {
668+
return self._downloadOrPrefetchPackage(siteId, fileList, false, component, componentId, revision, timemodified, dirPath);
664669
};
665670

666671
/**
@@ -1340,18 +1345,18 @@ angular.module('mm.core')
13401345
* @return {Number} Package revision.
13411346
*/
13421347
self.getRevisionFromFileList = function(files) {
1343-
if (files && files.length) {
1344-
for (var i = 0; i < files.length; i++) {
1345-
var file = files[i];
1346-
if (file.fileurl) {
1347-
var revision = self.getRevisionFromUrl(file.fileurl);
1348-
if (typeof revision !== 'undefined') {
1349-
return revision;
1350-
}
1348+
var revision = 0;
1349+
1350+
angular.forEach(files, function(file) {
1351+
if (file.fileurl) {
1352+
var r = self.getRevisionFromUrl(file.fileurl);
1353+
if (r > revision) {
1354+
revision = r;
13511355
}
13521356
}
1353-
}
1354-
return 0;
1357+
});
1358+
1359+
return revision;
13551360
};
13561361

13571362
/**
@@ -1403,15 +1408,15 @@ angular.module('mm.core')
14031408
* @return {Number} Package time modified.
14041409
*/
14051410
self.getTimemodifiedFromFileList = function(files) {
1406-
if (files && files.length) {
1407-
for (var i = 0; i < files.length; i++) {
1408-
var file = files[i];
1409-
if (file.timemodified) {
1410-
return file.timemodified;
1411-
}
1411+
var timemod = 0;
1412+
1413+
angular.forEach(files, function(file) {
1414+
if (file.timemodified > timemod) {
1415+
timemod = file.timemodified;
14121416
}
1413-
}
1414-
return 0;
1417+
});
1418+
1419+
return timemod;
14151420
};
14161421

14171422
/**
@@ -1627,16 +1632,18 @@ angular.module('mm.core')
16271632
* @module mm.core
16281633
* @ngdoc method
16291634
* @name $mmFilepool#prefetchPackage
1630-
* @param {String} siteId The site ID.
1631-
* @param {Object[]} fileList List of files to download.
1632-
* @param {String} component The component to link the file to.
1633-
* @param {Number} componentId An ID to identify the download. Must be unique.
1634-
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
1635-
* the files directly inside the filepool folder.
1636-
* @return {Promise} Promise resolved when all files are downloaded.
1635+
* @param {String} siteId The site ID.
1636+
* @param {Object[]} fileList List of files to download.
1637+
* @param {String} component The component to link the file to.
1638+
* @param {Number} componentId An ID to identify the download. Must be unique.
1639+
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
1640+
* @param {Number} [timemodified] Package's timemodified. If not defined, it will be calculated using the list of files.
1641+
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
1642+
* the files directly inside the filepool folder.
1643+
* @return {Promise} Promise resolved when all files are downloaded.
16371644
*/
1638-
self.prefetchPackage = function(siteId, fileList, component, componentId, dirPath) {
1639-
return self._downloadOrPrefetchPackage(siteId, fileList, true, component, componentId, dirPath);
1645+
self.prefetchPackage = function(siteId, fileList, component, componentId, revision, timemodified, dirPath) {
1646+
return self._downloadOrPrefetchPackage(siteId, fileList, true, component, componentId, revision, timemodified, dirPath);
16401647
};
16411648

16421649
/**

0 commit comments

Comments
 (0)