Skip to content

Commit

Permalink
Make sure we have a valid source for each file
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 20, 2022
1 parent f34b572 commit 30e704a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
9 changes: 2 additions & 7 deletions src/components/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
data-test="media"
:class="{selected}">
<a class="file"
:href="davPath"
:href="file.source"
:aria-label="ariaLabel"
@click.stop.prevent="emitClick">

Expand Down Expand Up @@ -71,8 +71,7 @@
import Star from 'vue-material-design-icons/Star'
import VideoIcon from 'vue-material-design-icons/Video.vue'
import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'
import { NcCheckboxRadioSwitch } from '@nextcloud/vue'
import UserConfig from '../mixins/UserConfig.js'
Expand Down Expand Up @@ -121,10 +120,6 @@ export default {
},
computed: {
/** @return {string} */
davPath() {
return generateRemoteUrl(`dav/files/${getCurrentUser().uid}`) + this.file.filename
},
/** @return {string} */
ariaDescription() {
return `image-description-${this.file.fileid}`
Expand Down
4 changes: 3 additions & 1 deletion src/services/DavClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import axios from '@nextcloud/axios'
import parseUrl from 'url-parse'
import { generateRemoteUrl } from '@nextcloud/router'

export const rootPath = 'dav'

// force our axios
const patcher = getPatcher()
patcher.patch('request', axios)

// init webdav client on default dav endpoint
const remote = generateRemoteUrl('dav')
const remote = generateRemoteUrl(rootPath)
const client = createClient(remote)

export const remotePath = parseUrl(remote).pathname
Expand Down
3 changes: 0 additions & 3 deletions src/services/PhotoSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { allMimes } from './AllowedMimes.js'
import client from './DavClient.js'
import { props } from './DavRequest.js'
import moment from '@nextcloud/moment'
import { generateRemoteUrl } from '@nextcloud/router'

/**
* List files from a folder and filter out unwanted mimes
Expand Down Expand Up @@ -147,6 +146,4 @@ export default async function(path = '', options = {}) {
const response = await client.getDirectoryContents('', options)

return response.data.map(data => genFileInfo(data))
.map(file => ({ ...file, source: generateRemoteUrl(`dav${file.filename}`) }))

}
13 changes: 11 additions & 2 deletions src/utils/fileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { generateRemoteUrl } from '@nextcloud/router'
import camelcase from 'camelcase'
import { isNumber } from './numberUtils'
import { rootPath } from '../services/DavClient.js'
import { isNumber } from './numberUtils.js'

/**
* Get an url encoded path
Expand Down Expand Up @@ -101,7 +103,7 @@ const sortCompare = function(fileInfo1, fileInfo2, key, asc = true) {
* @param {object} obj - object to flatten and format.
*/
function genFileInfo(obj) {
return Object.entries(obj).reduce((fileInfo, [key, data]) => {
const fileInfo = Object.entries(obj).reduce((fileInfo, [key, data]) => {
// flatten object if any
if (!!data && typeof data === 'object' && !Array.isArray(data)) {
return { ...fileInfo, ...genFileInfo(data) }
Expand All @@ -117,6 +119,13 @@ function genFileInfo(obj) {
return { ...fileInfo, [camelcase(key)]: isNumber(data) ? Number(data) : data }
}
}, {})

if (fileInfo.filename) {
// Adding context
fileInfo.source = generateRemoteUrl(rootPath) + '/' + fileInfo.filename
}

return fileInfo
}

export { encodeFilePath, extractFilePaths, sortCompare, genFileInfo }
3 changes: 0 additions & 3 deletions src/views/AlbumContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
<script>
// eslint-disable-next-line node/no-extraneous-import
import { addNewFileMenuEntry } from '@nextcloud/files'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { mapActions, mapGetters } from 'vuex'
import { NcActions, NcActionButton, NcButton, NcModal, NcEmptyContent, NcActionSeparator, NcLoadingIcon, isMobile } from '@nextcloud/vue'
Expand Down Expand Up @@ -340,8 +339,6 @@ export default {
const fetchedFiles = response.data
.map(file => genFileInfo(file))
// For the Viewer.
.map(file => ({ ...file, source: generateRemoteUrl(`dav${file.filename}`) }))
const fileIds = fetchedFiles
.map(file => file.fileid)
Expand Down
3 changes: 0 additions & 3 deletions src/views/SharedAlbumContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ import DownloadMultiple from 'vue-material-design-icons/DownloadMultiple'
import { NcActions, NcActionButton, NcButton, NcModal, NcEmptyContent, NcActionSeparator, isMobile } from '@nextcloud/vue'
import { getCurrentUser } from '@nextcloud/auth'
import { generateRemoteUrl } from '@nextcloud/router'
import FetchSharedAlbumsMixin from '../mixins/FetchSharedAlbumsMixin.js'
import FetchFilesMixin from '../mixins/FetchFilesMixin.js'
Expand Down Expand Up @@ -242,8 +241,6 @@ export default {
const fetchedFiles = response.data
.map(file => genFileInfo(file))
// For the Viewer.
.map(file => ({ ...file, source: generateRemoteUrl(`dav${file.filename}`) }))
const fileIds = fetchedFiles
.map(file => file.fileid)
Expand Down

0 comments on commit 30e704a

Please sign in to comment.