@@ -336,103 +336,109 @@ angular.module('mm.core')
336336 return $q . reject ( ) ;
337337 }
338338
339- return self . _fixPluginfileURL ( siteId , fileUrl ) . then ( function ( fileUrl ) {
340-
341- timemodified = timemodified || 0 ;
342- revision = self . getRevisionFromUrl ( fileUrl ) ;
343- fileId = self . _getFileIdByUrl ( fileUrl ) ;
344- priority = priority || 0 ;
345-
346- // Set up the component.
347- if ( typeof component !== 'undefined' ) {
348- link = {
349- component : component ,
350- componentId : componentId
351- } ;
339+ return $mmSitesManager . getSite ( siteId ) . then ( function ( site ) {
340+ if ( ! site . canDownloadFiles ( ) ) {
341+ return $q . reject ( ) ;
352342 }
353343
354- // Retrieve the queue deferred now if it exists to prevent errors if file is removed from queue
355- // while we're checking if the file is in queue.
356- queueDeferred = self . _getQueueDeferred ( siteId , fileId , false ) ;
344+ return self . _fixPluginfileURL ( siteId , fileUrl ) . then ( function ( fileUrl ) {
357345
358- return db . get ( mmFilepoolQueueStore , [ siteId , fileId ] ) . then ( function ( fileObject ) {
359- var foundLink = false ,
360- update = false ;
346+ timemodified = timemodified || 0 ;
347+ revision = self . getRevisionFromUrl ( fileUrl ) ;
348+ fileId = self . _getFileIdByUrl ( fileUrl ) ;
349+ priority = priority || 0 ;
361350
362- if ( fileObject ) {
363- // We already have the file in queue, we update the priority and links.
364- if ( fileObject . priority < priority ) {
365- update = true ;
366- fileObject . priority = priority ;
367- }
368- if ( revision && fileObject . revision !== revision ) {
369- update = true ;
370- fileObject . revision = revision ;
371- }
372- if ( timemodified && fileObject . timemodified !== timemodified ) {
373- update = true ;
374- fileObject . timemodified = timemodified ;
375- }
376- if ( filePath && fileObject . path !== filePath ) {
377- update = true ;
378- fileObject . path = filePath ;
379- }
351+ // Set up the component.
352+ if ( typeof component !== 'undefined' ) {
353+ link = {
354+ component : component ,
355+ componentId : componentId
356+ } ;
357+ }
380358
381- if ( link ) {
382- // We need to add the new link if it does not exist yet.
383- angular . forEach ( fileObject . links , function ( fileLink ) {
384- if ( fileLink . component == link . component && fileLink . componentId == link . componentId ) {
385- foundLink = true ;
386- }
387- } ) ;
388- if ( ! foundLink ) {
359+ // Retrieve the queue deferred now if it exists to prevent errors if file is removed from queue
360+ // while we're checking if the file is in queue.
361+ queueDeferred = self . _getQueueDeferred ( siteId , fileId , false ) ;
362+
363+ return db . get ( mmFilepoolQueueStore , [ siteId , fileId ] ) . then ( function ( fileObject ) {
364+ var foundLink = false ,
365+ update = false ;
366+
367+ if ( fileObject ) {
368+ // We already have the file in queue, we update the priority and links.
369+ if ( fileObject . priority < priority ) {
389370 update = true ;
390- fileObject . links . push ( link ) ;
371+ fileObject . priority = priority ;
372+ }
373+ if ( revision && fileObject . revision !== revision ) {
374+ update = true ;
375+ fileObject . revision = revision ;
376+ }
377+ if ( timemodified && fileObject . timemodified !== timemodified ) {
378+ update = true ;
379+ fileObject . timemodified = timemodified ;
380+ }
381+ if ( filePath && fileObject . path !== filePath ) {
382+ update = true ;
383+ fileObject . path = filePath ;
391384 }
392- }
393385
394- if ( update ) {
395- // Update only when required.
396- $log . debug ( 'Updating file ' + fileId + ' which is already in queue' ) ;
397- return db . insert ( mmFilepoolQueueStore , fileObject ) . then ( function ( ) {
398- return self . _getQueuePromise ( siteId , fileId ) ;
399- } ) ;
400- }
386+ if ( link ) {
387+ // We need to add the new link if it does not exist yet.
388+ angular . forEach ( fileObject . links , function ( fileLink ) {
389+ if ( fileLink . component == link . component && fileLink . componentId == link . componentId ) {
390+ foundLink = true ;
391+ }
392+ } ) ;
393+ if ( ! foundLink ) {
394+ update = true ;
395+ fileObject . links . push ( link ) ;
396+ }
397+ }
398+
399+ if ( update ) {
400+ // Update only when required.
401+ $log . debug ( 'Updating file ' + fileId + ' which is already in queue' ) ;
402+ return db . insert ( mmFilepoolQueueStore , fileObject ) . then ( function ( ) {
403+ return self . _getQueuePromise ( siteId , fileId ) ;
404+ } ) ;
405+ }
401406
402- $log . debug ( 'File ' + fileId + ' already in queue and does not require update' ) ;
403- if ( queueDeferred ) {
404- // If we were able to retrieve the queue deferred before we use that one, since the file download
405- // might have finished now and the deferred wouldn't be in the array anymore.
406- return queueDeferred . promise ;
407+ $log . debug ( 'File ' + fileId + ' already in queue and does not require update' ) ;
408+ if ( queueDeferred ) {
409+ // If we were able to retrieve the queue deferred before we use that one, since the file download
410+ // might have finished now and the deferred wouldn't be in the array anymore.
411+ return queueDeferred . promise ;
412+ } else {
413+ return self . _getQueuePromise ( siteId , fileId ) ;
414+ }
407415 } else {
408- return self . _getQueuePromise ( siteId , fileId ) ;
416+ return addToQueue ( ) ;
409417 }
410- } else {
418+ } , function ( ) {
419+ // Unsure why we could not get the record, let's add to the queue anyway.
411420 return addToQueue ( ) ;
421+ } ) ;
422+
423+ function addToQueue ( ) {
424+ $log . debug ( 'Adding ' + fileId + ' to the queue' ) ;
425+ return db . insert ( mmFilepoolQueueStore , {
426+ siteId : siteId ,
427+ fileId : fileId ,
428+ added : now . getTime ( ) ,
429+ priority : priority ,
430+ url : fileUrl ,
431+ revision : revision ,
432+ timemodified : timemodified ,
433+ path : filePath ,
434+ links : link ? [ link ] : [ ]
435+ } ) . then ( function ( ) {
436+ // Check if the queue is running.
437+ self . checkQueueProcessing ( ) ;
438+ return self . _getQueuePromise ( siteId , fileId ) ;
439+ } ) ;
412440 }
413- } , function ( ) {
414- // Unsure why we could not get the record, let's add to the queue anyway.
415- return addToQueue ( ) ;
416441 } ) ;
417-
418- function addToQueue ( ) {
419- $log . debug ( 'Adding ' + fileId + ' to the queue' ) ;
420- return db . insert ( mmFilepoolQueueStore , {
421- siteId : siteId ,
422- fileId : fileId ,
423- added : now . getTime ( ) ,
424- priority : priority ,
425- url : fileUrl ,
426- revision : revision ,
427- timemodified : timemodified ,
428- path : filePath ,
429- links : link ? [ link ] : [ ]
430- } ) . then ( function ( ) {
431- // Check if the queue is running.
432- self . checkQueueProcessing ( ) ;
433- return self . _getQueuePromise ( siteId , fileId ) ;
434- } ) ;
435- }
436442 } ) ;
437443 } ;
438444
0 commit comments