Skip to content

Commit

Permalink
feat: Allow multiple source folders
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Apr 17, 2024
1 parent 7208122 commit d4387cf
Show file tree
Hide file tree
Showing 30 changed files with 131 additions and 75 deletions.
4 changes: 2 additions & 2 deletions js/photos-dashboard.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-dashboard.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/photos-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-main.js.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/photos-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/photos-sidebar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-sidebar.js.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/photos-src_views_FaceContent_vue.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/photos-src_views_FaceContent_vue.js.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions lib/Migration/Version3000Date20240417075404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* Migrate the photosSourceFolder user config to photosSourceFolders
*/
class Version3000Date20240417075404 extends SimpleMigrationStep {
public function __construct(
private IDBConnection $db,
) {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$query = $this->db->getQueryBuilder();
$query->update('preferences')
->set('configvalue', $query->func()->concat($query->expr()->literal('["'), 'configvalue', $query->expr()->literal('"]')))
->set('configkey', $query->expr()->literal('photosSourceFolders'))
->where($query->expr()->eq('appid', $query->expr()->literal('photos')))
->andWhere($query->expr()->eq('configkey', $query->expr()->literal('photosSourceFolder')))
->executeStatement();
}
}
2 changes: 1 addition & 1 deletion lib/Service/UserConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UserConfigService {
public const DEFAULT_CONFIGS = [
'croppedLayout' => 'false',
'photosLocation' => '/Photos',
'photosSourceFolder' => '/Photos',
'photosSourceFolders' => '["/Photos"]',
];

private IConfig $config;
Expand Down
43 changes: 23 additions & 20 deletions src/components/Settings/PhotosSourceLocationsSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@
<template>
<div class="photos-locations-container">
<div class="photos-locations">
<PhotosFolder :path="photosSourceFolder" :root-folder-label="t('photos', 'All folders')" :root-folder-icon="FolderMultiple" />
<!-- TODO: uncomment when SEARCH on multiple folders is implemented. -->
<!-- <li v-for="(source, index) in photosSourceFolder"
<li v-for="(source, index) in photosSourceFolders"
:key="index">
<PhotosFolder :path="source"
:can-delete="photosSourceFolder.length !== 1"
:can-delete="photosSourceFolders.length !== 1"
:root-folder-label="t('photos', 'All folders')"
:root-folder-icon="FolderMultiple"
@remove-folder="removeSourceFolder(index)" />
</li> -->
</li>
</div>

<NcButton :aria-label="t('photos', 'Choose a Photos source for the timelines')"
<NcButton :aria-label="t('photos', 'Add a Photos source for the timelines')"
@click="debounceAddSourceFolder">
<!-- TODO: uncomment when SEARCH on multiple folders is implemented. -->
<!-- <template #icon>
<template #icon>
<Plus :size="20" />
</template> -->
{{ t('photos', 'Choose a different folder') }}
</template>
{{ t('photos', 'Add folder') }}
</NcButton>
</div>
</template>
Expand All @@ -50,6 +48,7 @@ import debounce from 'debounce'
import { defineComponent } from 'vue'

import FolderMultiple from 'vue-material-design-icons/FolderMultiple.vue'
import Plus from 'vue-material-design-icons/Plus.vue'

import { NcButton } from '@nextcloud/vue'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
Expand All @@ -63,6 +62,7 @@ export default defineComponent({
components: {
NcButton,
PhotosFolder,
Plus,
},

data() {
Expand All @@ -72,9 +72,9 @@ export default defineComponent({
},

computed: {
/** @return {string} */
photosSourceFolder() {
return this.$store.state.userConfig.photosSourceFolder
/** @return {string[]} */
photosSourceFolders() {
return this.$store.state.userConfig.photosSourceFolders
},
},

Expand All @@ -97,17 +97,16 @@ export default defineComponent({

async addSourceFolder() {
const pickedFolder = await this.openFilePicker(t('photos', 'Select a source folder for your media'))
// TODO: uncomment when SEARCH on multiple folders is implemented.
// if (this.photosSourceFolder.includes(pickedFolder)) {
// return
// }
this.$store.dispatch('updateUserConfig', { key: 'photosSourceFolder', value: pickedFolder })
if (this.photosSourceFolders.includes(pickedFolder)) {
return
}
this.$store.dispatch('updateUserConfig', { key: 'photosSourceFolders', value: [...this.photosSourceFolders, pickedFolder] })
},

removeSourceFolder(index) {
const folders = [...this.photosSourceFolder]
const folders = [...this.photosSourceFolders]
folders.splice(index, 1)
this.$store.dispatch('updateUserConfig', { key: 'photosSourceFolder', value: folders })
this.$store.dispatch('updateUserConfig', { key: 'photosSourceFolders', value: folders })
},

t,
Expand All @@ -123,6 +122,10 @@ export default defineComponent({

.photos-locations {
margin-bottom: 16px;

li {
list-style: none;
}
}
}
</style>
2 changes: 1 addition & 1 deletion src/components/Settings/SettingsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<CroppedLayoutSettings />
</NcAppSettingsSection>

<NcAppSettingsSection id="source-directories-settings" :name="t('photos', 'Media folder')">
<NcAppSettingsSection id="source-directories-settings" :name="t('photos', 'Media folders')">
<div class="setting-section-subline">
{{ t('photos', 'Choose the folders from where photos and videos are shown.') }}
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/mixins/FetchFilesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export default {
} catch (error) {
if (error.response?.status === 404) {
this.errorFetchingFiles = 404
const source = joinPaths(davRootPath, store.state.userConfig.photosSourceFolder ?? '/Photos') + '/'
// TODO improve error handling
const source = joinPaths(davRootPath, store.state.userConfig.photosSourceFolders ?? ['/Photos']) + '/'
logger.debug('Photo source does not exist, creating it.')
try {
await davGetClient().createDirectory(source)
Expand Down
26 changes: 10 additions & 16 deletions src/services/PhotoSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
*/

import { genFileInfo } from '../utils/fileUtils.js'
import { getCurrentUser } from '@nextcloud/auth'
import { allMimes } from './AllowedMimes.js'
import client from './DavClient.js'
import { props } from './DavRequest.js'
import moment from '@nextcloud/moment'
import store from '../store/index.js'
import { davRootPath } from '@nextcloud/files'

/**
* List files from a folder and filter out unwanted mimes
Expand All @@ -51,8 +51,6 @@ export default async function(options = {}) {
...options,
}

const prefixPath = `/files/${getCurrentUser().uid}`

// generating the search or condition
// based on the allowed mimetypes
const orMime = options.mimesType.reduce((str, mime) => `${str}
Expand Down Expand Up @@ -95,15 +93,14 @@ export default async function(options = {}) {
}).join('\n')}</d:or>`
: ''

// TODO: uncomment when SEARCH on multiple folders is implemented.
// const sourceFolders = store.state.userConfig.photosSourceFolder
// .map(folder => `
// <d:scope>
// <d:href>${davRootPath}/${folder}</d:href>
// <d:depth>infinity</d:depth>
// </d:scope>
// `)
// .join('\n')
const sourceFolders = store.state.userConfig.photosSourceFolders
.map(folder => `
<d:scope>
<d:href>${davRootPath}/${folder}</d:href>
<d:depth>infinity</d:depth>
</d:scope>`
)
.join('\n')

options = Object.assign({
method: 'SEARCH',
Expand All @@ -123,10 +120,7 @@ export default async function(options = {}) {
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>${prefixPath}/${store.state.userConfig.photosSourceFolder ?? '/Photos'}</d:href>
<d:depth>infinity</d:depth>
</d:scope>
${sourceFolders}
</d:from>
<d:where>
<d:and>
Expand Down
4 changes: 2 additions & 2 deletions src/store/userConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function getFolder(path) {
/**
* @typedef {object} UserConfigState
* @property {boolean} croppedLayout
* @property {string} photosSourceFolder
* @property {string[]} photosSourceFolders
* @property {string} photosLocation
* @property {import('@nextcloud/files').Folder} [photosLocationFolder]
*/
Expand All @@ -68,7 +68,7 @@ const module = {
state() {
return {
croppedLayout: loadState('photos', 'croppedLayout', 'false') === 'true',
photosSourceFolder: loadState('photos', 'photosSourceFolder', ''),
photosSourceFolders: JSON.parse(loadState('photos', 'photosSourceFolders', '[]')),
photosLocation: loadState('photos', 'photosLocation', ''),
photosLocationFolder: undefined,
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/Timeline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export default {
},

handleUserConfigChange({ key }) {
if (key === 'photosSourceFolder') {
if (key === 'photosSourceFolders') {
this.resetFetchFilesState()
}
},
Expand Down

0 comments on commit d4387cf

Please sign in to comment.