Skip to content

Commit

Permalink
Introduce options.homeFolder template in config
Browse files Browse the repository at this point in the history
  • Loading branch information
kulmann committed Dec 4, 2020
1 parent 532a0af commit 59032f0
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
18 changes: 16 additions & 2 deletions apps/files/src/components/AllFilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
</template>
<script>
import { mapGetters, mapActions, mapState } from 'vuex'
import isNil from 'lodash/isNil'
import Mixins from '../mixins'
import MixinRoutes from '../mixins/routes'
import FileList from './FileList.vue'
Expand Down Expand Up @@ -152,25 +153,38 @@ export default {
'fileSortField',
'fileSortDirectionDesc'
]),
...mapGetters(['configuration']),
...mapGetters(['configuration', 'homeFolder']),
item() {
return this.$route.params.item
}
},
watch: {
$route() {
this.$_allFilesList_goToHome()
this.$_allFilesList_getFolder()
}
},
beforeMount() {
this.$_allFilesList_goToHome()
this.$_allFilesList_getFolder()
},
methods: {
...mapActions('Files', ['loadFolder', 'setHighlightedFile']),
$_allFilesList_goToHome() {
if (this.isListRoute && isNil(this.item)) {
this.$router.push({
name: 'files-list',
params: {
item: this.homeFolder
}
})
}
},
$_allFilesList_getFolder() {
const absolutePath = this.item || this.configuration.rootFolder
const absolutePath = this.item
this.loadFolder({
client: this.$client,
Expand Down
4 changes: 2 additions & 2 deletions apps/files/src/components/FileList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default {
},
folderNotFound() {
return this.currentFolder === null
return (this.isListRoute || this.isPublicFilesRoute) && this.currentFolder === null
}
},
watch: {
Expand Down Expand Up @@ -337,7 +337,7 @@ export default {
},
resourceIndicators(resource) {
if (!this.currentFolder) {
if (this.folderNotFound) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/components/FilesAppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export default {
{
index: 0,
text: this.$gettext('Home'),
to: '/files/list'
to: '/files/list/%2F'
}
]
Expand Down
50 changes: 42 additions & 8 deletions apps/files/src/components/FilesLists/NotFoundMessage.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@
<template>
<div class="uk-text-center">
<oc-icon name="wb_cloudy" type="div" size="xxlarge" variation="system" />
<oc-icon :url="iconPath" type="div" size="xxlarge" variation="system" />
<div class="uk-text-muted uk-text-large">
<translate>Resource not found</translate>
</div>
<div class="uk-text-muted">
<translate>We went looking everywhere, but were unable to find this resource.</translate>
</div>
<div class="oc-mt-s">
<oc-button v-if="showHomeButton" type="a" variation="raw" :href="route">
<oc-button v-if="showHomeButton" type="router-link" variation="raw" :to="homeRoute">
<translate>Go to »All files«</translate>
</oc-button>
<oc-button
v-if="showPublicLinkButton"
type="router-link"
variation="raw"
:to="publicLinkRoute"
>
<translate>Reload public link</translate>
</oc-button>
</div>
</div>
</template>

<script>
import { mapGetters } from 'vuex'
import MixinRoutes from '../../mixins/routes'
export default {
name: 'NotFoundMessage',
mixins: [MixinRoutes],
computed: {
route() {
// TODO: use config.json variable
return '/'
...mapGetters(['homeFolder', 'configuration']),
showHomeButton() {
return this.isListRoute
},
showHomeButton() {
return !this.publicPage()
homeRoute() {
return {
name: 'files-list',
params: {
item: this.homeFolder
}
}
},
showPublicLinkButton() {
return this.isPublicFilesRoute
},
publicLinkRoute() {
const item = this.$route.params.item
return {
name: 'public-files',
params: {
item: item.split('/')[1]
}
}
},
iconPath() {
return this.configuration.theme.logo.notFound
}
}
}
Expand Down
63 changes: 61 additions & 2 deletions src/store/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isEmpty from 'lodash/isEmpty'

const state = {
state: null,
server: '',
Expand Down Expand Up @@ -25,7 +27,8 @@ const state = {
logo: {
sidebar: '',
favicon: '',
login: ''
login: '',
notFound: ''
},
filesList: {
hideDefaultStatusIndicators: false
Expand All @@ -37,7 +40,8 @@ const state = {
},
options: {
defaultExtension: 'files',
hideSearchBar: false
hideSearchBar: false,
homeFolder: ''
}
}

Expand Down Expand Up @@ -88,6 +92,18 @@ const mutations = {
const getters = {
configuration: state => {
return state
},
homeFolder: (state, rootGetters) => {
if (isEmpty(state.options.homeFolder)) {
return '/'
}
const parsed = parseHomeFolder(state.options.homeFolder, rootGetters.user)
if (parsed.indexOf('//') > -1) {
// if there are parts of the template that cannot be filled given the current user, we fall back to '/'
// because we assume that the path would be broken anyway.
return '/'
}
return parsed
}
}

Expand All @@ -97,3 +113,46 @@ export default {
mutations,
getters
}

/**
* The given home folder is allowed to contain twig style variables for user specific parts of the folder.
* This function injects user specific data into it.
*
* Examples:
* `/home/{{.email}}/`
* `/home/{{substr 0 3 .Id}}/{{.Id}}/`
*
* @param tpl The home folder template.
* @param user The user object from the store.
* @returns string
*/
function parseHomeFolder(tpl, user) {
const regex = /{{(.*?)}}/g
const parts = tpl.match(regex)
if (parts) {
parts.forEach(part => {
// check if part is a substring of a user value
const substringRegex = /{{substr\s([0-9]+)\s([0-9]+)\s\.(.*)}}/
const substringMatches = part.match(substringRegex)
if (!isEmpty(substringMatches) && substringMatches.length >= 4) {
const start = parseInt(substringMatches[1], 10)
const length = parseInt(substringMatches[2], 10)
const userValue = getUserValue(substringMatches[3], user)
tpl = tpl.replace(part, userValue.substring(start, start + length))
return
}

// none of the supported types, so we fall back to plain user value
const plainRegex = /{{\.(.*)}}/
const plainMatches = part.match(plainRegex)
if (!isEmpty(plainMatches) && plainMatches.length >= 2) {
tpl = tpl.replace(part, getUserValue(plainMatches[1], user))
}
})
}
return tpl
}

function getUserValue(key, user) {
return user[key.toLowerCase()] || ''
}
3 changes: 2 additions & 1 deletion themes/owncloud.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"logo": {
"sidebar": "themes/owncloud/assets/logo.svg",
"favicon": "themes/owncloud/favicon.jpg",
"login": "themes/owncloud/assets/logo.svg"
"login": "themes/owncloud/assets/logo.svg",
"notFound": "themes/owncloud/assets/cloud.svg"
},
"filesList": {
"hideDefaultStatusIndicators": false
Expand Down
3 changes: 3 additions & 0 deletions themes/owncloud/assets/cloud.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 59032f0

Please sign in to comment.