diff --git a/src/mixins/FetchFilesMixin.js b/src/mixins/FetchFilesMixin.js index e01282451..27201e023 100644 --- a/src/mixins/FetchFilesMixin.js +++ b/src/mixins/FetchFilesMixin.js @@ -20,6 +20,8 @@ * */ +import moment from '@nextcloud/moment' + import logger from '../services/logger.js' import getPhotos from '../services/PhotoSearch.js' import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js' @@ -33,12 +35,19 @@ export default { ], data() { + const dateTimeUpperBound = moment() + const dateTimeLowerBound = moment(dateTimeUpperBound).subtract(1, 'months') + return { errorFetchingFiles: null, loadingFiles: false, doneFetchingFiles: false, fetchSemaphore: new SemaphoreWithPriority(1), fetchedFileIds: [], + dateTimeUpperBound, + dateTimeLowerBound, + timeWindowSteps: 1, + firstResultOffset: 0, } }, @@ -69,17 +78,47 @@ export default { const numberOfImagesPerBatch = 200 + logger.debug('[FetchFilesMixin] Fetching file between', this.dateTimeUpperBound.format('L'), 'and', this.dateTimeLowerBound?.format('L')) + // Load next batch of images const fetchedFiles = await getPhotos(path, { - firstResult: this.fetchedFileIds.length, + firstResult: this.firstResultOffset, nbResults: numberOfImagesPerBatch, + dateTimeUpperBound: this.dateTimeUpperBound.unix(), + dateTimeLowerBound: this.dateTimeLowerBound?.unix(), ...options, signal: this.abortController.signal, }) - // If we get less files than requested that means we got to the end - if (fetchedFiles.length !== numberOfImagesPerBatch) { - this.doneFetchingFiles = true + if (fetchedFiles.length === numberOfImagesPerBatch) { + // If we have the same number of files that as requested + // then the time window probably contains more, so we simply bump the first result offset. + this.firstResultOffset += fetchedFiles.length + } else if (fetchedFiles.length === 0 && this.firstResultOffset === 0) { + // If we tried a new window and it is empty + if (this.dateTimeLowerBound === undefined) { + // if lower bound has been cleared, then we clear upper bound + // this will allow the server to return all files with either empty or above than now original date time + this.dateTimeUpperBound = undefined + } else if (this.dateTimeUpperBound === undefined) { + // else if upper bound has been cleared, then we are done fetching files. + this.doneFetchingFiles = true + } else if (moment(this.dateTimeUpperBound).diff(this.dateTimeLowerBound, 'months') > 64) { + // else if we reach 64 months, we clear the lower bound. + this.dateTimeUpperBound = this.dateTimeLowerBound + this.dateTimeLowerBound = undefined + } else { + // else we progressively increase the time window until we reach 64 months (4 requests) + this.timeWindowSteps *= 4 + this.dateTimeUpperBound = this.dateTimeLowerBound + this.dateTimeLowerBound = moment(this.dateTimeLowerBound).subtract(this.timeWindowSteps, 'months') + } + } else if (fetchedFiles.length !== numberOfImagesPerBatch) { + // If we get less files than requested, + // we are at the end for the current time window, so we move to the next one. + this.timeWindowSteps = 1 + this.dateTimeUpperBound = this.dateTimeLowerBound + this.dateTimeLowerBound = moment(this.dateTimeUpperBound).subtract(this.timeWindowSteps, 'months') } const fileIds = fetchedFiles @@ -121,6 +160,9 @@ export default { this.doneFetchingFiles = false this.errorFetchingFiles = null this.loadingFiles = false + this.dateTimeUpperBound = moment() + this.dateTimeLowerBound = moment(this.dateTimeUpperBound).subtract(1, 'months') + this.timeWindowSteps = 1 this.fetchedFileIds = [] }, }, diff --git a/src/services/PhotoSearch.js b/src/services/PhotoSearch.js index e405153b6..927ae8271 100644 --- a/src/services/PhotoSearch.js +++ b/src/services/PhotoSearch.js @@ -38,9 +38,11 @@ import moment from '@nextcloud/moment' * @param {boolean} [options.full=false] get full data of the files * @param {boolean} [options.onThisDay=false] get only items from this day of year * @param {boolean} [options.onlyFavorites=false] get only favorite items + * @param {number} [options.dateTimeUpperBound] limit the search to photos taken before this lower bound + * @param {number} [options.dateTimeLowerBound] limit the search to photos taken after this lower bound * @return {Promise} the file list */ -export default async function(path = '', options = {}) { +export default async function (path = '', options = {}) { // default function options options = { firstResult: 0, @@ -51,7 +53,7 @@ export default async function(path = '', options = {}) { ...options, } - const prefixPath = `/files/${getCurrentUser().uid}` + const prefixPath = `/files/${getCurrentUser()?.uid}` // generating the search or condition // based on the allowed mimetypes @@ -95,6 +97,26 @@ export default async function(path = '', options = {}) { }).join('\n')}` : '' + let timeWindow = '' + if (options.dateTimeUpperBound !== undefined) { + timeWindow = ` + + + + + ${options.dateTimeUpperBound} + ` + } + if (options.dateTimeLowerBound !== undefined) { + timeWindow += ` + + + + + ${options.dateTimeLowerBound} + ` + } + options = Object.assign({ method: 'SEARCH', headers: { @@ -125,6 +147,7 @@ export default async function(path = '', options = {}) { ${eqFavorites} ${onThisDay} + ${timeWindow}