|
| 1 | +/** |
| 2 | + * @copyright Copyright (c) 2022 Louis Chemineau <[email protected]> |
| 3 | + * |
| 4 | + * @author Louis Chemineau <[email protected]> |
| 5 | + * |
| 6 | + * @license AGPL-3.0-or-later |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +import { mapActions } from 'vuex' |
| 24 | + |
| 25 | +import { showError } from '@nextcloud/dialogs' |
| 26 | + |
| 27 | +import AbortControllerMixin from './AbortControllerMixin.js' |
| 28 | +import { fetchCollection, fetchCollectionFiles } from '../services/collectionFetcher.js' |
| 29 | +import logger from '../services/logger.js' |
| 30 | +import SemaphoreWithPriority from '../utils/semaphoreWithPriority.js' |
| 31 | + |
| 32 | +export default { |
| 33 | + name: 'FetchCollectionsContentMixin', |
| 34 | + |
| 35 | + data() { |
| 36 | + return { |
| 37 | + semaphore: new SemaphoreWithPriority(30), |
| 38 | + fetchSemaphore: new SemaphoreWithPriority(1), |
| 39 | + semaphoreSymbol: null, |
| 40 | + loadingCollection: false, |
| 41 | + loadingCollectionFiles: false, |
| 42 | + errorFetchingCollection: null, |
| 43 | + errorFetchingCollectionFiles: null, |
| 44 | + } |
| 45 | + }, |
| 46 | + |
| 47 | + mixins: [ |
| 48 | + AbortControllerMixin, |
| 49 | + ], |
| 50 | + |
| 51 | + methods: { |
| 52 | + ...mapActions([ |
| 53 | + 'appendFiles', |
| 54 | + 'addCollections', |
| 55 | + 'setCollectionFiles', |
| 56 | + ]), |
| 57 | + |
| 58 | + async fetchCollection(collectionFileName) { |
| 59 | + if (this.loadingCollection) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + this.loadingCollection = true |
| 65 | + this.errorFetchingCollection = null |
| 66 | + |
| 67 | + const collection = await fetchCollection(collectionFileName, { signal: this.abortController.signal }) |
| 68 | + this.addCollections({ collections: [collection] }) |
| 69 | + return collection |
| 70 | + } catch (error) { |
| 71 | + if (error.response?.status === 404) { |
| 72 | + this.errorFetchingCollection = 404 |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + this.errorFetchingCollection = error |
| 77 | + logger.error('[PublicLocationContent] Error fetching location', { error }) |
| 78 | + showError(this.t('photos', 'Failed to fetch location.')) |
| 79 | + } finally { |
| 80 | + this.loadingCollection = false |
| 81 | + } |
| 82 | + }, |
| 83 | + |
| 84 | + async fetchCollectionFiles(collectionFileName) { |
| 85 | + if (this.loadingCollectionFiles) { |
| 86 | + return [] |
| 87 | + } |
| 88 | + |
| 89 | + const semaphoreSymbol = await this.semaphore.acquire(() => 0, 'fetchFiles') |
| 90 | + const fetchSemaphoreSymbol = await this.fetchSemaphore.acquire() |
| 91 | + |
| 92 | + try { |
| 93 | + this.errorFetchingCollectionFiles = null |
| 94 | + this.loadingCollectionFiles = true |
| 95 | + this.semaphoreSymbol = semaphoreSymbol |
| 96 | + |
| 97 | + const fetchedFiles = await fetchCollectionFiles(collectionFileName, { signal: this.abortController.signal }) |
| 98 | + const fileIds = fetchedFiles.map(file => file.fileid.toString()) |
| 99 | + |
| 100 | + this.appendFiles(fetchedFiles) |
| 101 | + |
| 102 | + if (fetchedFiles.length > 0) { |
| 103 | + await this.$store.commit('setCollectionFiles', { collectionFileName, fileIds }) |
| 104 | + } |
| 105 | + |
| 106 | + return fetchedFiles |
| 107 | + } catch (error) { |
| 108 | + if (error.response?.status === 404) { |
| 109 | + this.errorFetchingCollectionFiles = 404 |
| 110 | + return [] |
| 111 | + } |
| 112 | + |
| 113 | + this.errorFetchingCollectionFiles = error |
| 114 | + |
| 115 | + showError(this.t('photos', 'Failed to fetch locations list.')) |
| 116 | + logger.error('[PublicLocationContent] Error fetching location files', { error }) |
| 117 | + } finally { |
| 118 | + this.loadingCollectionFiles = false |
| 119 | + this.semaphore.release(semaphoreSymbol) |
| 120 | + this.fetchSemaphore.release(fetchSemaphoreSymbol) |
| 121 | + } |
| 122 | + |
| 123 | + return [] |
| 124 | + }, |
| 125 | + }, |
| 126 | +} |
0 commit comments