diff --git a/__tests__/files/file.spec.ts b/__tests__/files/file.spec.ts index 70902e27..54be3c58 100644 --- a/__tests__/files/file.spec.ts +++ b/__tests__/files/file.spec.ts @@ -3,6 +3,7 @@ import { describe, expect, test } from 'vitest' import { File } from '../../lib/files/file' import { FileType } from '../../lib/files/fileType' import { Permission } from '../../lib/permissions' +import { NodeStatus } from '../../lib/files/node' describe('File creation', () => { test('Valid dav file', () => { @@ -12,6 +13,7 @@ describe('File creation', () => { owner: 'emma', mtime: new Date(Date.UTC(2023, 0, 1, 0, 0, 0)), crtime: new Date(Date.UTC(1990, 0, 1, 0, 0, 0)), + status: NodeStatus.NEW, }) expect(file).toBeInstanceOf(File) @@ -35,6 +37,7 @@ describe('File creation', () => { expect(file.path).toBe('/picture.jpg') expect(file.isDavRessource).toBe(true) expect(file.permissions).toBe(Permission.NONE) + expect(file.status).toBe(NodeStatus.NEW) }) test('Valid dav file with root', () => { diff --git a/__tests__/files/node.spec.ts b/__tests__/files/node.spec.ts index 89fe1917..c77a14b6 100644 --- a/__tests__/files/node.spec.ts +++ b/__tests__/files/node.spec.ts @@ -4,6 +4,7 @@ import { File } from '../../lib/files/file' import { Folder } from '../../lib/files/folder' import { Attribute, NodeData } from '../../lib/files/nodeData' import { Permission } from '../../lib/permissions' +import { NodeStatus } from '../../lib/files/node' describe('Node testing', () => { test('Root null fallback', () => { @@ -195,6 +196,15 @@ describe('Sanity checks', () => { root: '/remote.php/dav/files/emma', })).toThrowError('The root must be relative to the service. e.g /files/emma') }) + + test('Invalid status', () => { + expect(() => new File({ + source: 'https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg', + mime: 'image/jpeg', + owner: 'emma', + status: 'invalid' as unknown as NodeStatus, + })).toThrowError('Status must be a valid NodeStatus') + }) }) describe('Dav service detection', () => { diff --git a/lib/files/node.ts b/lib/files/node.ts index a42629e2..e3dbc66b 100644 --- a/lib/files/node.ts +++ b/lib/files/node.ts @@ -24,6 +24,17 @@ import { Permission } from '../permissions' import { FileType } from './fileType' import { Attribute, NodeData, isDavRessource, validateData } from './nodeData' +export enum NodeStatus { + /** This is a new node and it doesn't exists on the filesystem yet */ + NEW = 'new', + /** This node has failed and is unavailable */ + FAILED = 'failed', + /** This node is currently loading or have an operation in progress */ + LOADING = 'loading', + /** This node is locked and cannot be modified */ + LOCKED = 'locked', +} + export abstract class Node { private _data: NodeData @@ -213,6 +224,20 @@ export abstract class Node { return this._data?.id || this.attributes?.fileid } + /** + * Get the node status. + */ + get status(): NodeStatus|undefined { + return this._data?.status + } + + /** + * Set the node status. + */ + set status(status: NodeStatus|undefined) { + this._data.status = status + } + /** * Move the node to a new destination * diff --git a/lib/files/nodeData.ts b/lib/files/nodeData.ts index 90865edb..b0d241bf 100644 --- a/lib/files/nodeData.ts +++ b/lib/files/nodeData.ts @@ -22,6 +22,7 @@ import { join } from 'path' import { Permission } from '../permissions' +import { NodeStatus } from './node' export interface Attribute { [key: string]: any } @@ -54,6 +55,7 @@ export interface NodeData { /** The owner UID of this node */ owner: string|null + /** The node attributes */ attributes?: Attribute /** @@ -62,6 +64,9 @@ export interface NodeData { * e.g. /files/emma */ root?: string + + /** The node status */ + status?: NodeStatus } /** @@ -156,4 +161,8 @@ export const validateData = (data: NodeData, davService: RegExp) => { throw new Error('The root must be relative to the service. e.g /files/emma') } } + + if (data.status && !Object.values(NodeStatus).includes(data.status)) { + throw new Error('Status must be a valid NodeStatus') + } }