Skip to content

Commit

Permalink
fix(dav): crtime parsing and invalid date handling
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 12, 2024
1 parent 92d3a2c commit 5bfaa47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions __tests__/dav/dav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ describe('davResultToNode', () => {
const node = davResultToNode(remoteResult)
expect(node.status).toBe(NodeStatus.FAILED)
})

test('Ignore invalid times', () => {
vi.spyOn(auth, 'getCurrentUser').mockReturnValue({ uid: 'user1', displayName: 'User 1', isAdmin: false })

// Invalid dates
const remoteResult = { ...result }
remoteResult.lastmod = 'invalid'
remoteResult.props!.creationdate = 'invalid'
const node = davResultToNode(remoteResult)
expect(node.mtime).toBeUndefined()
expect(node.crtime).toBeUndefined()

// Zero dates
remoteResult.lastmod = 'Thu, 01 Jan 1970 00:00:00 GMT'
remoteResult.props!.creationdate = 'Thu, 01 Jan 1970 00:00:00 GMT'
const node2 = davResultToNode(remoteResult)
expect(node2.mtime).toBeUndefined()
expect(node2.crtime).toBeUndefined()
})
})

describe('DAV requests', () => {
Expand Down
7 changes: 6 additions & 1 deletion lib/dav/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public'
* Nextcloud DAV result response
*/
interface ResponseProps extends DAVResultResponseProps {
creationdate: string
permissions: string
mime: string
fileid: number
Expand Down Expand Up @@ -174,10 +175,14 @@ export const davResultToNode = function(node: FileStat, filesRoot = davRootPath,
const owner = String(props?.['owner-id'] || userId)
const id = props.fileid || 0

const mtime = new Date(Date.parse(node.lastmod))
const crtime = new Date(Date.parse(props.creationdate))

const nodeData: NodeData = {
id,
source: `${remoteURL}${node.filename}`,
mtime: new Date(Date.parse(node.lastmod)),
mtime: !isNaN(mtime.getTime()) && mtime.getTime() !== 0 ? mtime : undefined,
crtime: !isNaN(crtime.getTime()) && crtime.getTime() !== 0 ? crtime : undefined,
mime: node.mime || 'application/octet-stream',
// Manually cast to work around for https://github.com/perry-mitchell/webdav-client/pull/380
displayname: props.displayname !== undefined ? String(props.displayname) : undefined,
Expand Down

0 comments on commit 5bfaa47

Please sign in to comment.