Skip to content

Commit

Permalink
Merge pull request #10877 from nextcloud/fix/10851/oc-request-token-u…
Browse files Browse the repository at this point in the history
…pdate

fix(DAVClient): wrap DAV client creation in a function
  • Loading branch information
Antreesy authored Nov 13, 2023
2 parents c78abeb + d9ec8c3 commit 7374563
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/components/MediaSettings/VideoBackgroundEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import { imagePath, generateUrl } from '@nextcloud/router'

import { VIRTUAL_BACKGROUND } from '../../constants.js'
import BrowserStorage from '../../services/BrowserStorage.js'
import client from '../../services/DavClient.js'
import { getDavClient } from '../../services/DavClient.js'
import { findUniquePath } from '../../utils/fileUpload.js'

const canUploadBackgrounds = getCapabilities()?.spreed?.config?.call?.['can-upload-background']
Expand Down Expand Up @@ -176,6 +176,7 @@ export default {

try {
// Create the backgrounds folder if it doesn't exist
const client = getDavClient()
if (await client.exists(absoluteBackgroundsFolderPath) === false) {
await client.createDirectory(absoluteBackgroundsFolderPath)
}
Expand Down Expand Up @@ -219,6 +220,7 @@ export default {

const filePath = this.$store.getters.getAttachmentFolder() + '/Backgrounds/' + file.name

const client = getDavClient()
// Get a unique relative path based on the previous path variable
const uniquePath = await findUniquePath(client, userRoot, filePath)

Expand Down
10 changes: 5 additions & 5 deletions src/services/DavClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { getRequestToken } from '@nextcloud/auth'
import { generateRemoteUrl } from '@nextcloud/router'

// init webdav client on default dav endpoint
const client = createClient(generateRemoteUrl('dav'),
{ headers: { requesttoken: getRequestToken() || '' } },
)

export default client
export const getDavClient = () => {
return createClient(generateRemoteUrl('dav'),
{ headers: { requesttoken: getRequestToken() || '' } },
)
}
3 changes: 2 additions & 1 deletion src/store/fileUploadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import moment from '@nextcloud/moment'

import client from '../services/DavClient.js'
import { getDavClient } from '../services/DavClient.js'
import { EventBus } from '../services/EventBus.js'
import {
getFileTemplates,
Expand Down Expand Up @@ -312,6 +312,7 @@ const actions = {
const fileName = (currentFile.newName || currentFile.name)
// Candidate rest of the path
const path = getters.getAttachmentFolder() + '/' + fileName
const client = getDavClient()
// Get a unique relative path based on the previous path variable
const uniquePath = await findUniquePath(client, userRoot, path)
try {
Expand Down
11 changes: 6 additions & 5 deletions src/store/fileUploadStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import Vuex from 'vuex'

import { showError } from '@nextcloud/dialogs'

import client from '../services/DavClient.js'
import { getDavClient } from '../services/DavClient.js'
import { shareFile } from '../services/filesSharingServices.js'
import { setAttachmentFolder } from '../services/settingsService.js'
import { findUniquePath, getFileExtension } from '../utils/fileUpload.js'
import fileUploadStore from './fileUploadStore.js'

jest.mock('../services/DavClient', () => ({
putFileContents: jest.fn(),
getDavClient: jest.fn(),
}))
jest.mock('../utils/fileUpload', () => ({
findUniquePath: jest.fn(),
Expand Down Expand Up @@ -79,11 +79,15 @@ describe('fileUploadStore', () => {

describe('uploading', () => {
let restoreConsole
const client = {
putFileContents: jest.fn(),
}

beforeEach(() => {
storeConfig.getters.getAttachmentFolder = jest.fn().mockReturnValue(() => '/Talk')
store = new Vuex.Store(storeConfig)
restoreConsole = mockConsole(['error', 'debug'])
getDavClient.mockReturnValue(client)
})

afterEach(() => {
Expand Down Expand Up @@ -153,7 +157,6 @@ describe('fileUploadStore', () => {

const uniqueFileName = '/Talk/' + file.name + 'uniq'
findUniquePath.mockResolvedValueOnce(uniqueFileName)
client.putFileContents.mockResolvedValue()
shareFile.mockResolvedValue()

await store.dispatch('uploadFiles', { uploadId: 'upload-id1', caption: 'text-caption' })
Expand Down Expand Up @@ -201,7 +204,6 @@ describe('fileUploadStore', () => {
findUniquePath
.mockResolvedValueOnce('/Talk/' + files[0].name + 'uniq')
.mockResolvedValueOnce('/Talk/' + files[1].name + 'uniq')
client.putFileContents.mockResolvedValue()
shareFile
.mockResolvedValueOnce({ data: { ocs: { data: { id: '1' } } } })
.mockResolvedValueOnce({ data: { ocs: { data: { id: '2' } } } })
Expand Down Expand Up @@ -280,7 +282,6 @@ describe('fileUploadStore', () => {

findUniquePath
.mockResolvedValueOnce('/Talk/' + files[0].name + 'uniq')
client.putFileContents.mockResolvedValue()
shareFile.mockRejectedValueOnce({
response: {
status: 403,
Expand Down

0 comments on commit 7374563

Please sign in to comment.