Skip to content
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
47 changes: 46 additions & 1 deletion __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,51 @@ describe('FileId attribute', () => {
})
})

describe('Id attribute', () => {
test('Id undefined', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
root: '/files/emma',
mime: 'image/jpeg',
owner: 'emma',
})
expect(file.id).toBeUndefined()
})

test('id definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
root: '/files/emma',
mime: 'image/jpeg',
owner: 'emma',
id: '1234',
})
expect(file.id).toBe('1234')
})

test('Legacy id definition', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
root: '/files/emma',
mime: 'image/jpeg',
owner: 'emma',
id: 1234,
})
expect(file.id).toBe('1234')
})

test('Legacy failed node id', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/picture.jpg',
root: '/files/emma',
mime: 'image/jpeg',
owner: 'emma',
id: -1,
})
expect(file.id).toBeUndefined()
})
})

describe('Mime attribute', () => {
test('Mime definition', () => {
const file = new File({
Expand Down Expand Up @@ -304,7 +349,7 @@ describe('Sanity checks', () => {
root: '/files/emma',
mime: 'image/jpeg',
owner: 'emma',
id: '1234' as unknown as number,
id: true as unknown as number,
})).toThrowError('Invalid id type of value')
})

Expand Down
24 changes: 21 additions & 3 deletions lib/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,29 @@ export abstract class Node {
}

/**
* Get the node id if defined.
* There is no setter as the fileid is not meant to be changed
* Get the nodes file id if defined.
* There is no setter as the fileid is not meant to be changed.
*
* @deprecated Nextcloud is migrating to snowflake ids which are strings. Use the `id` attribute instead.
*/
get fileid(): number | undefined {
return this._data?.id
return typeof this._data?.id === 'number' ? this._data.id : undefined
}

/**
* Get the nodes id - if defined.
*
* Note: As Nextcloud is migrating to snowflake ids the id has to be a string,
* due to limitations of the JavaScript number type (snowflake ids are 64bit JavaScript numbers can only accurately represent integers up to 53 bit).
*/
get id(): string | undefined {
if (typeof this._data?.id === 'undefined'
|| (typeof this._data.id === 'number' && this._data.id < 0)) {
// legacy fileid < 0 means the node failed, this should be communicated via the `status` attribute
// so in that case there is no id available
return undefined
}
return String(this._data.id)
}

/**
Expand Down
13 changes: 10 additions & 3 deletions lib/node/nodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ export interface NodeData {
*/
root: string

/** Unique ID */
id?: number
/**
* Unique ID
*
* This is usually the file id from the backend where this would be a number.
* For some type of nodes this is already a snowflake id and thus as string.
*
* Note: Passing a number is deprecated.
*/
id?: number | string

/** Last modified time */
mtime?: Date
Expand Down Expand Up @@ -91,7 +98,7 @@ export function isDavResource(source: string, davService: RegExp): boolean {
* @param davService Pattern to check if source is DAV ressource
*/
export function validateData(data: NodeData, davService: RegExp) {
if (data.id && typeof data.id !== 'number') {
if (data.id && typeof data.id !== 'number' && typeof data.id !== 'string') {
throw new Error('Invalid id type of value')
}

Expand Down