Skip to content

Commit

Permalink
fix(dav): Set status in davResultToNode when fileid is negative
Browse files Browse the repository at this point in the history
A negative fileid is returned by Nextcloud when something internally went wrong.
In this case we should set the Node.status by default.

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jun 13, 2024
1 parent d80cc46 commit 128305f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 15 additions & 1 deletion __tests__/dav/dav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { afterAll, afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { readFile } from 'node:fs/promises'

import { File, Folder, davRemoteURL, davGetFavoritesReport, davRootPath, getFavoriteNodes, davResultToNode } from '../../lib'
import { File, Folder, davRemoteURL, davGetFavoritesReport, davRootPath, getFavoriteNodes, davResultToNode, NodeStatus } from '../../lib'
import { FileStat } from 'webdav'
import * as auth from '@nextcloud/auth'

Expand Down Expand Up @@ -135,6 +135,20 @@ describe('davResultToNode', () => {
expect(node.isDavRessource).toBe(true)
expect(node.owner).toBe('anonymous')
})

test('by default no status is set', () => {
const remoteResult = { ...result }
remoteResult.props!.fileid = 1
const node = davResultToNode(remoteResult)
expect(node.status).toBeUndefined()
})

test('sets node status on invalid fileid', () => {
const remoteResult = { ...result }
remoteResult.props!.fileid = -1
const node = davResultToNode(remoteResult)
expect(node.status).toBe(NodeStatus.FAILED)
})
})

describe('DAV requests', () => {
Expand Down
7 changes: 5 additions & 2 deletions lib/dav/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { DAVResultResponseProps, FileStat, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { Node } from '../files/node'
import { NodeStatus, type Node } from '../files/node'

import { File } from '../files/file'
import { Folder } from '../files/folder'
Expand Down Expand Up @@ -148,13 +148,16 @@ export const davResultToNode = function(node: FileStat, filesRoot = davRootPath,
const props = node.props as ResponseProps
const permissions = davParsePermissions(props?.permissions)
const owner = String(props?.['owner-id'] || userId)
const id = props.fileid || 0

const nodeData: NodeData = {
id: props?.fileid || 0,
id,
source: `${remoteURL}${node.filename}`,
mtime: new Date(Date.parse(node.lastmod)),
mime: node.mime || 'application/octet-stream',
size: props?.size || Number.parseInt(props.getcontentlength || '0'),
// The fileid is set to -1 for failed requests
status: id < 0 ? NodeStatus.FAILED : undefined,
permissions,
owner,
root: filesRoot,
Expand Down

0 comments on commit 128305f

Please sign in to comment.