Skip to content

Commit

Permalink
feat(node): cleanup attributes and add size and permissions setters
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed May 15, 2024
1 parent b62f8e4 commit 8adaab1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
26 changes: 24 additions & 2 deletions __tests__/files/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('FileId attribute', () => {
expect(file.fileid).toBe(-1234)
})

test('FileId attributes fallback', () => {
test('FileId attributes no fallback', () => {
const file = new Folder({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/',
mime: 'image/jpeg',
Expand All @@ -73,7 +73,7 @@ describe('FileId attribute', () => {
fileid: 1234,
},
})
expect(file.fileid).toBe(1234)
expect(file.fileid).toBeUndefined()
})
})

Expand Down Expand Up @@ -466,4 +466,26 @@ describe('Attributes update', () => {
expect(file.attributes?.['owner-display-name']).toBeUndefined()
expect(file.attributes?.['owner-id']).toBeUndefined()
})

test('Update attributes with protected getters', () => {
const file = new File({
source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg',
mime: 'image/jpeg',
owner: 'emma',
attributes: {
etag: '1234',
},
})

file.update({
etag: '5678',
owner: 'admin',
fileid: 1234,
})

expect(file.attributes?.etag).toBe('5678')
expect(file.attributes?.owner).toBeUndefined()
expect(file.attributes?.fileid).toBeUndefined()
})

})
57 changes: 54 additions & 3 deletions lib/files/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { encodePath } from '@nextcloud/paths'
import { Permission } from '../permissions'
import { FileType } from './fileType'
import { Attribute, NodeData, isDavRessource, validateData } from './nodeData'
import logger from '../utils/logger'

export enum NodeStatus {
/** This is a new node and it doesn't exists on the filesystem yet */
Expand Down Expand Up @@ -68,7 +69,7 @@ export abstract class Node {

// Proxy the attributes to update the mtime on change
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._attributes = new Proxy(data.attributes || {} as any, this.handler)
this._attributes = new Proxy(this.cleanAttributes(data.attributes || {}) as any, this.handler)
delete this._data.attributes

if (davService) {
Expand All @@ -78,6 +79,8 @@ export abstract class Node {

/**
* Get the source url to this object
* There is no setter as the source is not meant to be changed manually.
* You can use the rename or move method to change the source.
*/
get source(): string {
// strip any ending slash
Expand All @@ -94,13 +97,17 @@ export abstract class Node {

/**
* Get this object name
* There is no setter as the source is not meant to be changed manually.
* You can use the rename or move method to change the source.
*/
get basename(): string {
return basename(this.source)
}

/**
* Get this object's extension
* There is no setter as the source is not meant to be changed manually.
* You can use the rename or move method to change the source.
*/
get extension(): string|null {
return extname(this.source)
Expand All @@ -109,6 +116,9 @@ export abstract class Node {
/**
* Get the directory path leading to this object
* Will use the relative path to root if available
*
* There is no setter as the source is not meant to be changed manually.
* You can use the rename or move method to change the source.
*/
get dirname(): string {
if (this.root) {
Expand Down Expand Up @@ -137,20 +147,24 @@ export abstract class Node {

/**
* Get the file mime
* There is no setter as the mime is not meant to be changed
*/
get mime(): string|undefined {
return this._data.mime
}

/**
* Get the file modification time
* There is no setter as the modification time is not meant to be changed manually.
* It will be automatically updated when the attributes are changed.
*/
get mtime(): Date|undefined {
return this._data.mtime
}

/**
* Get the file creation time
* There is no setter as the creation time is not meant to be changed
*/
get crtime(): Date|undefined {
return this._data.crtime
Expand All @@ -163,6 +177,14 @@ export abstract class Node {
return this._data.size
}

/**
* Set the file size
*/
set size(size: number|undefined) {
this.updateMtime()
this._data.size = size
}

/**
* Get the file attribute
*/
Expand All @@ -185,8 +207,17 @@ export abstract class Node {
: Permission.NONE
}

/**
* Set the file permissions
*/
set permissions(permissions: Permission) {
this.updateMtime()
this._data.permissions = permissions
}

/**
* Get the file owner
* There is no setter as the owner is not meant to be changed
*/
get owner(): string|null {
// Remote ressources have no owner
Expand All @@ -205,6 +236,7 @@ export abstract class Node {

/**
* Get the dav root of this object
* There is no setter as the root is not meant to be changed
*/
get root(): string|null {
// If provided (recommended), use the root and strip away the ending slash
Expand Down Expand Up @@ -245,7 +277,7 @@ export abstract class Node {
* Will look for the fileid in attributes if undefined.
*/
get fileid(): number|undefined {
return this._data?.id || this.attributes?.fileid
return this._data?.id
}

/**
Expand Down Expand Up @@ -304,7 +336,26 @@ export abstract class Node {
update(attributes: Attribute) {
// Proxy the attributes to update the mtime on change
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._attributes = new Proxy(attributes || {} as any, this.handler)
this._attributes = new Proxy(this.cleanAttributes(attributes) || {} as any, this.handler)
}

private cleanAttributes(attributes: Attribute): Attribute {
const getters = Object.entries(Object.getOwnPropertyDescriptors(Node.prototype))
.filter(e => typeof e[1].get === 'function' && e[0] !== '__proto__')
.map(e => e[0])

// Remove protected getters from the attributes
// you cannot update them
const clean: Attribute = {}
for (const key in attributes) {
if (getters.includes(key)) {
logger.debug(`Discarding protected attribute ${key}`, { node: this, attributes })
continue
}
clean[key] = attributes[key]
}

return clean
}

}

0 comments on commit 8adaab1

Please sign in to comment.