Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable28] fix: Refresh file picker on folder creation #45575

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions apps/comments/src/services/DavClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@

import { createClient } from 'webdav'
import { getRootPath } from '../utils/davUtils.js'
import { getRequestToken } from '@nextcloud/auth'
import { getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

// init webdav client
const client = createClient(getRootPath(), {
headers: {
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: getRequestToken() ?? '',
},
})
const client = createClient(getRootPath())

// set CSRF token header
const setHeaders = (token) => {
client.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

export default client
10 changes: 4 additions & 6 deletions apps/comments/src/services/GetComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import { parseXML, type DAVResult, type FileStat, type ResponseDataDetailed } from 'webdav'

// https://github.com/perry-mitchell/webdav-client/issues/339
import { processResponsePayload } from '../../../../node_modules/webdav/dist/node/response.js'
import { prepareFileFromProps } from '../../../../node_modules/webdav/dist/node/tools/dav.js'
import { processResponsePayload } from 'webdav/dist/node/response.js'
import { prepareFileFromProps } from 'webdav/dist/node/tools/dav.js'
import client from './DavClient.js'

export const DEFAULT_LIMIT = 20
Expand Down Expand Up @@ -77,10 +77,8 @@ const getDirectoryFiles = function(
// Map all items to a consistent output structure (results)
return responseItems.map(item => {
// Each item should contain a stat object
const {
propstat: { prop: props },
} = item
const props = item.propstat!.prop!;

return prepareFileFromProps(props, props.id.toString(), isDetailed)
return prepareFileFromProps(props, props.id!.toString(), isDetailed)
})
}
30 changes: 19 additions & 11 deletions apps/dav/src/dav/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import * as webdav from 'webdav'
import axios from '@nextcloud/axios'
import { createClient } from 'webdav'
import memoize from 'lodash/fp/memoize.js'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

export const getClient = memoize((service) => {
// Add this so the server knows it is an request from the browser
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
// init webdav client
const remote = generateRemoteUrl(`dav/${service}/${getCurrentUser().uid}`)
const client = createClient(remote)

// force our axios
const patcher = webdav.getPatcher()
patcher.patch('request', axios)
// set CSRF token header
const setHeaders = (token) => {
client.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

return webdav.createClient(
generateRemoteUrl(`dav/${service}/${getCurrentUser().uid}`)
)
// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

return client;
})
2 changes: 1 addition & 1 deletion apps/files/src/actions/deleteAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './deleteAction'
import { expect } from '@jest/globals'
import { File, Folder, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import axios from '@nextcloud/axios'

import logger from '../logger'
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/actions/favoriteAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './favoriteAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import * as favoriteAction from './favoriteAction'
import axios from '@nextcloud/axios'
import logger from '../logger'
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/actions/renameAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './renameAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'

const view = {
id: 'files',
Expand Down
11 changes: 11 additions & 0 deletions apps/files/src/eventbus.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Node } from '@nextcloud/files'

declare module '@nextcloud/event-bus' {
export interface NextcloudEvents {
// mapping of 'event name' => 'event type'
'files:favorites:removed': Node
'files:favorites:added': Node
}
}

export {}
38 changes: 24 additions & 14 deletions apps/files/src/services/WebdavClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import type { RequestOptions, Response } from 'webdav'

import { createClient, getPatcher } from 'webdav'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser, getRequestToken } from '@nextcloud/auth'
import { request } from 'webdav/dist/node/request.js'
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

export const rootPath = `/files/${getCurrentUser()?.uid}`
export const defaultRootUrl = generateRemoteUrl('dav' + rootPath)

export const getClient = (rootUrl = defaultRootUrl) => {
const client = createClient(rootUrl, {
headers: {
requesttoken: getRequestToken() || '',
},
})
const client = createClient(rootUrl)

// set CSRF token header
const setHeaders = (token: string | null) => {
client?.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
});
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

/**
* Allow to override the METHOD to support dav REPORT
Expand All @@ -45,12 +53,14 @@ export const getClient = (rootUrl = defaultRootUrl) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// https://github.com/perry-mitchell/hot-patcher/issues/6
patcher.patch('request', (options: RequestOptions): Promise<Response> => {
if (options.headers?.method) {
options.method = options.headers.method
delete options.headers.method
patcher.patch('fetch', (url: string, options: RequestInit): Promise<Response> => {
const headers = options.headers as Record<string, string>
if (headers?.method) {
options.method = headers.method
delete headers.method
}
return request(options)
return fetch(url, options)
})
return client

return client;
}
3 changes: 2 additions & 1 deletion apps/files/src/views/favorites.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-named-as-default-member */
/**
* @copyright Copyright (c) 2023 John Molakvoæ <[email protected]>
*
Expand All @@ -22,7 +23,7 @@
import { basename } from 'path'
import { expect } from '@jest/globals'
import { Folder, Navigation, getNavigation } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import * as initialState from '@nextcloud/initial-state'

import { action } from '../actions/favoriteAction'
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/src/actions/acceptShareAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './acceptShareAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import axios from '@nextcloud/axios'
import '../main'

Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/src/actions/rejectShareAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './rejectShareAction'
import { expect } from '@jest/globals'
import { File, Folder, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import axios from '@nextcloud/axios'
import '../main'

Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/src/actions/restoreShareAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { action } from './restoreShareAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as eventBus from '@nextcloud/event-bus'
import eventBus from '@nextcloud/event-bus'
import axios from '@nextcloud/axios'
import '../main'

Expand Down
25 changes: 19 additions & 6 deletions apps/files_trashbin/src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { createClient } from 'webdav'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser, getRequestToken } from '@nextcloud/auth'
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

// init webdav client
export const rootPath = `/trashbin/${getCurrentUser()?.uid}/trash`
export const rootUrl = generateRemoteUrl('dav' + rootPath)
const client = createClient(rootUrl, {
headers: {
requesttoken: getRequestToken(),
},
})
const client = createClient(rootUrl)

// set CSRF token header
const setHeaders = (token: string | null) => {
client.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

export default client
30 changes: 19 additions & 11 deletions apps/files_versions/src/utils/davClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@

import { createClient } from 'webdav'
import { generateRemoteUrl } from '@nextcloud/router'
import { getRequestToken } from '@nextcloud/auth'
import { getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

// init webdav client
const rootPath = 'dav'

// init webdav client on default dav endpoint
const remote = generateRemoteUrl(rootPath)
export default createClient(remote, {
headers: {
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: getRequestToken() ?? '',
},
})
const client = createClient(remote)

// set CSRF token header
const setHeaders = (token) => {
client.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

export default client
22 changes: 16 additions & 6 deletions apps/systemtags/src/services/davClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@

import { createClient } from 'webdav'
import { generateRemoteUrl } from '@nextcloud/router'
import { getRequestToken } from '@nextcloud/auth'
import { getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'

// init webdav client
const rootUrl = generateRemoteUrl('dav')
export const davClient = createClient(rootUrl)

export const davClient = createClient(rootUrl, {
headers: {
requesttoken: getRequestToken() ?? '',
},
})
// set CSRF token header
const setHeaders = (token: string | null) => {
davClient.setHeaders({
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())
2 changes: 1 addition & 1 deletion core/css/server.css

Large diffs are not rendered by default.

Loading
Loading