Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 89 additions & 83 deletions www/core/lib/filepool.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,103 +336,109 @@ angular.module('mm.core')
return $q.reject();
}

return self._fixPluginfileURL(siteId, fileUrl).then(function(fileUrl) {

timemodified = timemodified || 0;
revision = self.getRevisionFromUrl(fileUrl);
fileId = self._getFileIdByUrl(fileUrl);
priority = priority || 0;

// Set up the component.
if (typeof component !== 'undefined') {
link = {
component: component,
componentId: componentId
};
return $mmSitesManager.getSite(siteId).then(function(site) {
if (!site.canDownloadFiles()) {
return $q.reject();
}

// Retrieve the queue deferred now if it exists to prevent errors if file is removed from queue
// while we're checking if the file is in queue.
queueDeferred = self._getQueueDeferred(siteId, fileId, false);
return self._fixPluginfileURL(siteId, fileUrl).then(function(fileUrl) {

return db.get(mmFilepoolQueueStore, [siteId, fileId]).then(function(fileObject) {
var foundLink = false,
update = false;
timemodified = timemodified || 0;
revision = self.getRevisionFromUrl(fileUrl);
fileId = self._getFileIdByUrl(fileUrl);
priority = priority || 0;

if (fileObject) {
// We already have the file in queue, we update the priority and links.
if (fileObject.priority < priority) {
update = true;
fileObject.priority = priority;
}
if (revision && fileObject.revision !== revision) {
update = true;
fileObject.revision = revision;
}
if (timemodified && fileObject.timemodified !== timemodified) {
update = true;
fileObject.timemodified = timemodified;
}
if (filePath && fileObject.path !== filePath) {
update = true;
fileObject.path = filePath;
}
// Set up the component.
if (typeof component !== 'undefined') {
link = {
component: component,
componentId: componentId
};
}

if (link) {
// We need to add the new link if it does not exist yet.
angular.forEach(fileObject.links, function(fileLink) {
if (fileLink.component == link.component && fileLink.componentId == link.componentId) {
foundLink = true;
}
});
if (!foundLink) {
// Retrieve the queue deferred now if it exists to prevent errors if file is removed from queue
// while we're checking if the file is in queue.
queueDeferred = self._getQueueDeferred(siteId, fileId, false);

return db.get(mmFilepoolQueueStore, [siteId, fileId]).then(function(fileObject) {
var foundLink = false,
update = false;

if (fileObject) {
// We already have the file in queue, we update the priority and links.
if (fileObject.priority < priority) {
update = true;
fileObject.links.push(link);
fileObject.priority = priority;
}
if (revision && fileObject.revision !== revision) {
update = true;
fileObject.revision = revision;
}
if (timemodified && fileObject.timemodified !== timemodified) {
update = true;
fileObject.timemodified = timemodified;
}
if (filePath && fileObject.path !== filePath) {
update = true;
fileObject.path = filePath;
}
}

if (update) {
// Update only when required.
$log.debug('Updating file ' + fileId + ' which is already in queue');
return db.insert(mmFilepoolQueueStore, fileObject).then(function() {
return self._getQueuePromise(siteId, fileId);
});
}
if (link) {
// We need to add the new link if it does not exist yet.
angular.forEach(fileObject.links, function(fileLink) {
if (fileLink.component == link.component && fileLink.componentId == link.componentId) {
foundLink = true;
}
});
if (!foundLink) {
update = true;
fileObject.links.push(link);
}
}

if (update) {
// Update only when required.
$log.debug('Updating file ' + fileId + ' which is already in queue');
return db.insert(mmFilepoolQueueStore, fileObject).then(function() {
return self._getQueuePromise(siteId, fileId);
});
}

$log.debug('File ' + fileId + ' already in queue and does not require update');
if (queueDeferred) {
// If we were able to retrieve the queue deferred before we use that one, since the file download
// might have finished now and the deferred wouldn't be in the array anymore.
return queueDeferred.promise;
$log.debug('File ' + fileId + ' already in queue and does not require update');
if (queueDeferred) {
// If we were able to retrieve the queue deferred before we use that one, since the file download
// might have finished now and the deferred wouldn't be in the array anymore.
return queueDeferred.promise;
} else {
return self._getQueuePromise(siteId, fileId);
}
} else {
return self._getQueuePromise(siteId, fileId);
return addToQueue();
}
} else {
}, function() {
// Unsure why we could not get the record, let's add to the queue anyway.
return addToQueue();
});

function addToQueue() {
$log.debug('Adding ' + fileId + ' to the queue');
return db.insert(mmFilepoolQueueStore, {
siteId: siteId,
fileId: fileId,
added: now.getTime(),
priority: priority,
url: fileUrl,
revision: revision,
timemodified: timemodified,
path: filePath,
links: link ? [link] : []
}).then(function() {
// Check if the queue is running.
self.checkQueueProcessing();
return self._getQueuePromise(siteId, fileId);
});
}
}, function() {
// Unsure why we could not get the record, let's add to the queue anyway.
return addToQueue();
});

function addToQueue() {
$log.debug('Adding ' + fileId + ' to the queue');
return db.insert(mmFilepoolQueueStore, {
siteId: siteId,
fileId: fileId,
added: now.getTime(),
priority: priority,
url: fileUrl,
revision: revision,
timemodified: timemodified,
path: filePath,
links: link ? [link] : []
}).then(function() {
// Check if the queue is running.
self.checkQueueProcessing();
return self._getQueuePromise(siteId, fileId);
});
}
});
};

Expand Down