@nextcloud/files

Nextcloud Files helpers for Nextcloud apps and libraries.
This library provides three kinds of utils:
- WebDAV helper functions to work with the Nextcloud WebDAV interface.
Those functions are available in
@nextcloud/files/dav
- Geneal purpose function related to files or folders, like filename validation.
- Functions and classes to interact with the Nextcloud files app, like registering a new view or a file action.
Compatibility
| 4.x | ✅ | 33+ |
| 3.x | ✅ | 26-32 |
| 2.x | ❌ | 23-25 |
| 1.x | ❌ | 20-22 |
Usage examples
Files app
The "New"-menu allows to create new entries or upload files, it is also possible for other apps to register their own actions here.
import type { Entry } from '@nextcloud/files'
import { addNewFileMenuEntry } from '@nextcloud/files'
import { t } from '@nextcloud/l10n'
const myEntry: Entry = {
id: 'my-app',
displayName: t('my-app', 'New something'),
iconSvgInline: importedSVGFile,
handler(context: Folder, content: Node[]): void {
}
}
addNewFileMenuEntry(myEntry)
It is possible to provide your own sidebar tabs for the files app.
For this you need to create a custom web component,
which can either be done without any framework by using vanilla JavaScript but is also possible with Vue.
This example will make use of the Vue framework for building a sidebar tab as this is the official UI framework for Nextcloud apps.
The sidebar tab consists of two parts:
- The web component which will be rendered within the sidebar.
- A definition object that provides all information needed by the files app.
This object provides the requires information such as:
- The order (to ensure a consistent tabs order)
- The display name for the tab navigation
- An icon, to be used in the tab navigation
- A callback to check if the sidebar tab is enabled for the current node shown in the sidebar.
- The web component tag name
The registration must happen in an initScript.
import type { ISidebarTab } from '@nextcloud/files'
import { getSidebar } from '@nextcloud/files'
import { t } from '@nextcloud/l10n'
const MyTab: ISidebarTab = {
id: 'my_app',
displayName: t('my_app', 'Sharing'),
iconSvgInline: '<svg>...</svg>',
order: 50,
tagName: 'my_app-files_sidebar_tab',
enabled({ node, folder, view }) {
return true
},
async onInit() {
},
}
getSidebar()
.registerTab(MyTab)
The web component needs to have those properties:
- node of type
INode
- folder of type
IFolder
- view of type
IView
- active of type
boolean
When using Vue you need to first create the Vue component:
<script setup lang="ts">
import type { IFolder, INode, IView } from '@nextcloud/files'
defineProps<{
node: INode
folder: IFolder
view: IView
active: boolean
}>()
</script>
<template>
<div>
<div>Showing node: {{ node.source }}</div>
<div>... in folder: {{ folder.source }}</div>
<div>... with view: {{ view.id }}</div>
</div>
</template>
Which then can be wrapped in a web component and registered.
import { getSidebar } from '@nextcloud/files'
import { defineAsyncComponent, defineCustomElement } from 'vue'
getSidebar().registerTab({
tagName: `my_app-files_sidebar_tab`,
onInit() {
const MySidebarTab = defineAsyncComponent(() => import('./views/MySidebarTab.vue'))
const MySidebarTabWebComponent = defineCustomElement(MySidebarTab, { shadowRoot: false })
customElements.define('my_app-files_sidebar_tab', MySidebarTabWebComponent)
},
})
WebDAV
The getClient exported function returns a webDAV client that's a wrapper around webdav's webDAV client.
All its methods are available here.
Using WebDAV to query favorite nodes
import { getClient, defaultRootPath, getFavoriteNodes } from '@nextcloud/files/dav'
const client = getClient()
const favorites = await getFavoriteNodes(client)
const favorites = await getFavoriteNodes(client, '/', defaultRootPath)
Using WebDAV to list all nodes in directory
import {
getClient,
getDefaultPropfind,
resultToNode,
defaultRootPath,
defaultRemoteURL
} from '@nextcloud/files/dav'
const client = getClient()
const client = getClient(defaultRemoteURL)
const client = getClient('https://example.com/dav')
const path = '/my-folder/'
const results = client.getDirectoryContents(`${defaultRootPath}${path}`, {
details: true,
data: getDefaultPropfind()
})
const nodes = results.data.map((result) => resultToNode(r))
const nodes = results.data.map((result) => resultToNode(r, myRoot))
const nodes = results.data.map((result) => resultToNode(r, myRoot, myRemoteURL))
Using WebDAV to get a Node from a file's name
import { getClient, davGetDefaultPropfind, resultToNode, davRootPath } from '@nextcloud/files'
import { emit } from '@nextcloud/event-bus'
const client = getClient()
client.stat(`${davRootPath}${filename}`, {
details: true,
data: davGetDefaultPropfind(),
}).then((result) => {
const node = resultToNode(result.data)
emit('files:node:updated', node)
})