@nextcloud/files 3.0.0-beta.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,55 @@
1
- # @nextcloud/files [![npm last version](https://img.shields.io/npm/v/@nextcloud/files.svg?style=flat-square)](https://www.npmjs.com/package/@nextcloud/files) ![Codecov](https://img.shields.io/codecov/c/github/nextcloud/nextcloud-files?style=flat-square)
1
+ # @nextcloud/files
2
+ [![npm last version](https://img.shields.io/npm/v/@nextcloud/files.svg?style=flat-square)](https://www.npmjs.com/package/@nextcloud/files) [![Code coverage](https://img.shields.io/codecov/c/github/nextcloud-libraries/nextcloud-files?style=flat-square)](https://app.codecov.io/gh/nextcloud-libraries/nextcloud-files) [![Project documentation](https://img.shields.io/badge/documentation-online-blue?style=flat-square)](https://nextcloud-libraries.github.io/nextcloud-files/)
2
3
 
3
4
  Nextcloud Files helpers for Nextcloud apps and libraries
5
+
6
+ ## Usage example
7
+
8
+ ### Using WebDAV to query favorite nodes
9
+
10
+ ```ts
11
+ import { davGetClient, davRootPath, getFavoriteNodes } from '@nextcloud/files'
12
+
13
+ const client = davGetClient()
14
+ // query favorites for the root folder (meaning all favorites)
15
+ const favorites = await getFavoriteNodes(client)
16
+ // which is the same as writing:
17
+ const favorites = await getFavoriteNodes(client, '/', davRootPath)
18
+ ```
19
+
20
+ ### Using WebDAV to list all nodes in directory
21
+
22
+ ```ts
23
+ import {
24
+ davGetClient,
25
+ davGetDefaultPropfind,
26
+ davResultToNode,
27
+ davRootPath,
28
+ davRemoteURL
29
+ } from '@nextcloud/files'
30
+
31
+ // Get the DAV client for the default remote
32
+ const client = davGetClient()
33
+ // which is the same as writing
34
+ const client = davGetClient(davRemoteURL)
35
+ // of cause you can also configure another WebDAV remote
36
+ const client = davGetClient('https://example.com/dav')
37
+
38
+ const path = '/my-folder/' // the directory you want to list
39
+
40
+ // Query the directory content using the webdav library
41
+ // `davRootPath` is the files root, for Nextcloud this is '/files/USERID', by default the current user is used
42
+ const results = client.getDirectoryContents(`${davRootPath}${path}`, {
43
+ details: true,
44
+ // Query all required properties for a Node
45
+ data: davGetDefaultPropfind()
46
+ })
47
+
48
+ // Convert the result to an array of Node
49
+ const nodes = results.data.map((result) => davResultToNode(r))
50
+ // If you specified a different root in the `getDirectoryContents` you must add this also on the `davResultToNode` call:
51
+ const nodes = results.data.map((result) => davResultToNode(r, myRoot))
52
+ // Same if you used a different remote URL:
53
+ const nodes = results.data.map((result) => davResultToNode(r, myRoot, myRemoteURL))
54
+
55
+ ```
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
3
+ *
4
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
5
+ * @author Ferdinand Thiessen <opensource@fthiessen.de>
6
+ *
7
+ * @license AGPL-3.0-or-later
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU Affero General Public License as
11
+ * published by the Free Software Foundation, either version 3 of the
12
+ * License, or (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU Affero General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public License
20
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+ import type { FileStat, WebDAVClient } from 'webdav';
24
+ import type { Node } from '../files/node';
25
+ /**
26
+ * The DAV root path for the current user
27
+ */
28
+ export declare const davRootPath: string;
29
+ /**
30
+ * The DAV remote URL used as base URL for the WebDAV client
31
+ */
32
+ export declare const davRemoteURL: string;
33
+ /**
34
+ * Get a WebDAV client configured to include the Nextcloud request token
35
+ *
36
+ * @param remoteURL The DAV server remote URL
37
+ */
38
+ export declare const davGetClient: (remoteURL?: string) => WebDAVClient;
39
+ /**
40
+ * Use WebDAV to query for favorite Nodes
41
+ *
42
+ * @param davClient The WebDAV client to use for performing the request
43
+ * @param path Base path for the favorites, if unset all favorites are queried
44
+ * @param davRoot The root path for the DAV user (defaults to `davRootPath`)
45
+ * @example
46
+ * ```js
47
+ * import { davGetClient, davRootPath, getFavoriteNodes } from '@nextcloud/files'
48
+ *
49
+ * const client = davGetClient()
50
+ * // query favorites for the root
51
+ * const favorites = await getFavoriteNodes(client)
52
+ * // which is the same as writing:
53
+ * const favorites = await getFavoriteNodes(client, '/', davRootPath)
54
+ * ```
55
+ */
56
+ export declare const getFavoriteNodes: (davClient: WebDAVClient, path?: string, davRoot?: string) => Promise<Node[]>;
57
+ /**
58
+ * Covert DAV result `FileStat` to `Node`
59
+ *
60
+ * @param node The DAV result
61
+ * @param filesRoot The DAV files root path
62
+ * @param remoteURL The DAV server remote URL (same as on `davGetClient`)
63
+ */
64
+ export declare const davResultToNode: (node: FileStat, filesRoot?: string, remoteURL?: string) => Node;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Parse the webdav permission string to a permission enum
3
+ *
4
+ * @param permString The DAV permission string
5
+ */
6
+ export declare const davParsePermissions: (permString?: string) => number;
@@ -0,0 +1,57 @@
1
+ export type DavProperty = {
2
+ [key: string]: string;
3
+ };
4
+ export declare const defaultDavProperties: string[];
5
+ export declare const defaultDavNamespaces: {
6
+ d: string;
7
+ nc: string;
8
+ oc: string;
9
+ ocs: string;
10
+ };
11
+ /**
12
+ * Register custom DAV properties
13
+ *
14
+ * Can be used if your app introduces custom DAV properties, so e.g. the files app can make use of it.
15
+ *
16
+ * @param prop The property
17
+ * @param namespace The namespace of the property
18
+ */
19
+ export declare const registerDavProperty: (prop: string, namespace?: DavProperty) => boolean;
20
+ /**
21
+ * Get the registered dav properties
22
+ */
23
+ export declare const getDavProperties: () => string;
24
+ /**
25
+ * Get the registered dav namespaces
26
+ */
27
+ export declare const getDavNameSpaces: () => string;
28
+ /**
29
+ * Get the default PROPFIND request body
30
+ */
31
+ export declare const davGetDefaultPropfind: () => string;
32
+ /**
33
+ * Get the REPORT body to filter for favorite nodes
34
+ */
35
+ export declare const davGetFavoritesReport: () => string;
36
+ /**
37
+ * Get the SEARCH body to search for recently modified files
38
+ *
39
+ * @param lastModified Oldest timestamp to include (Unix timestamp)
40
+ * @example
41
+ * ```ts
42
+ * // SEARCH for recent files need a different DAV endpoint
43
+ * const client = davGetClient(generateRemoteUrl('dav'))
44
+ * // Timestamp of last week
45
+ * const lastWeek = Math.round(Date.now() / 1000) - (60 * 60 * 24 * 7)
46
+ * const contentsResponse = await client.getDirectoryContents(path, {
47
+ * details: true,
48
+ * data: davGetRecentSearch(lastWeek),
49
+ * headers: {
50
+ * method: 'SEARCH',
51
+ * 'Content-Type': 'application/xml; charset=utf-8',
52
+ * },
53
+ * deep: true,
54
+ * }) as ResponseDataDetailed<FileStat[]>
55
+ * ```
56
+ */
57
+ export declare const davGetRecentSearch: (lastModified: number) => string;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
2
+ * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
3
3
  *
4
4
  * @author John Molakvoæ <skjnldsv@protonmail.com>
5
5
  *
@@ -19,57 +19,79 @@
19
19
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
20
  *
21
21
  */
22
- import { Node } from "./files/node";
22
+ import { Node } from './files/node';
23
+ import { View } from './navigation/view';
24
+ export declare enum DefaultType {
25
+ DEFAULT = "default",
26
+ HIDDEN = "hidden"
27
+ }
23
28
  interface FileActionData {
24
29
  /** Unique ID */
25
30
  id: string;
26
31
  /** Translatable string displayed in the menu */
27
- displayName: (files: Node[], view: any) => string;
32
+ displayName: (files: Node[], view: View) => string;
33
+ /** Translatable title for of the action */
34
+ title?: (files: Node[], view: View) => string;
28
35
  /** Svg as inline string. <svg><path fill="..." /></svg> */
29
- iconSvgInline: (files: Node[], view: any) => string;
36
+ iconSvgInline: (files: Node[], view: View) => string;
30
37
  /** Condition wether this action is shown or not */
31
- enabled?: (files: Node[], view: any) => boolean;
38
+ enabled?: (files: Node[], view: View) => boolean;
32
39
  /**
33
40
  * Function executed on single file action
34
- * @returns true if the action was executed successfully,
41
+ * @return true if the action was executed successfully,
35
42
  * false otherwise and null if the action is silent/undefined.
36
43
  * @throws Error if the action failed
37
44
  */
38
- exec: (file: Node, view: any, dir: string) => Promise<boolean | null>;
45
+ exec: (file: Node, view: View, dir: string) => Promise<boolean | null>;
39
46
  /**
40
47
  * Function executed on multiple files action
41
- * @returns true if the action was executed successfully,
48
+ * @return true if the action was executed successfully,
42
49
  * false otherwise and null if the action is silent/undefined.
43
50
  * @throws Error if the action failed
44
51
  */
45
- execBatch?: (files: Node[], view: any, dir: string) => Promise<(boolean | null)[]>;
52
+ execBatch?: (files: Node[], view: View, dir: string) => Promise<(boolean | null)[]>;
46
53
  /** This action order in the list */
47
54
  order?: number;
48
- /** Make this action the default */
49
- default?: boolean;
55
+ /**
56
+ * This action's parent id in the list.
57
+ * If none found, will be displayed as a top-level action.
58
+ */
59
+ parent?: string;
60
+ /**
61
+ * Make this action the default.
62
+ * If multiple actions are default, the first one
63
+ * will be used. The other ones will be put as first
64
+ * entries in the actions menu iff DefaultType.Hidden is not used.
65
+ * A DefaultType.Hidden action will never be shown
66
+ * in the actions menu even if another action takes
67
+ * its place as default.
68
+ */
69
+ default?: DefaultType;
50
70
  /**
51
71
  * If true, the renderInline function will be called
52
72
  */
53
- inline?: (file: Node, view: any) => boolean;
73
+ inline?: (file: Node, view: View) => boolean;
54
74
  /**
55
75
  * If defined, the returned html element will be
56
76
  * appended before the actions menu.
57
77
  */
58
- renderInline?: (file: Node, view: any) => HTMLElement;
78
+ renderInline?: (file: Node, view: View) => Promise<HTMLElement | null>;
59
79
  }
60
80
  export declare class FileAction {
61
81
  private _action;
62
82
  constructor(action: FileActionData);
63
83
  get id(): string;
64
- get displayName(): (files: Node[], view: any) => string;
65
- get iconSvgInline(): (files: Node[], view: any) => string;
66
- get enabled(): ((files: Node[], view: any) => boolean) | undefined;
67
- get exec(): (file: Node, view: any, dir: string) => Promise<boolean | null>;
68
- get execBatch(): ((files: Node[], view: any, dir: string) => Promise<(boolean | null)[]>) | undefined;
84
+ get displayName(): (files: Node[], view: View) => string;
85
+ get title(): ((files: Node[], view: View) => string) | undefined;
86
+ get iconSvgInline(): (files: Node[], view: View) => string;
87
+ get enabled(): ((files: Node[], view: View) => boolean) | undefined;
88
+ get exec(): (file: Node, view: View, dir: string) => Promise<boolean | null>;
89
+ get execBatch(): ((files: Node[], view: View, dir: string) => Promise<(boolean | null)[]>) | undefined;
69
90
  get order(): number | undefined;
70
- get default(): boolean | undefined;
71
- get inline(): ((file: Node, view: any) => boolean) | undefined;
72
- get renderInline(): ((file: Node, view: any) => HTMLElement) | undefined;
91
+ get parent(): string | undefined;
92
+ get default(): DefaultType | undefined;
93
+ get inline(): ((file: Node, view: View) => boolean) | undefined;
94
+ get renderInline(): ((file: Node, view: View) => Promise<HTMLElement | null>) | undefined;
73
95
  private validateAction;
74
96
  }
75
97
  export declare const registerFileAction: (action: FileAction) => void;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
3
+ *
4
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
5
+ *
6
+ * @license AGPL-3.0-or-later
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU Affero General Public License as
10
+ * published by the Free Software Foundation, either version 3 of the
11
+ * License, or (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU Affero General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Affero General Public License
19
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+ import { Folder } from './files/folder';
23
+ import { View } from './navigation/view';
24
+ export interface HeaderData {
25
+ /** Unique ID */
26
+ id: string;
27
+ /** Order */
28
+ order: number;
29
+ /** Condition wether this header is shown or not */
30
+ enabled?: (folder: Folder, view: View) => boolean;
31
+ /** Executed when file list is initialized */
32
+ render: (el: HTMLElement, folder: Folder, view: View) => void;
33
+ /** Executed when root folder changed */
34
+ updated(folder: Folder, view: View): any;
35
+ }
36
+ export declare class Header {
37
+ private _header;
38
+ constructor(header: HeaderData);
39
+ get id(): string;
40
+ get order(): number;
41
+ get enabled(): ((folder: Folder, view: View) => boolean) | undefined;
42
+ get render(): (el: HTMLElement, folder: Folder, view: View) => void;
43
+ get updated(): (folder: Folder, view: View) => any;
44
+ private validateHeader;
45
+ }
46
+ export declare const registerFileListHeaders: (header: Header) => void;
47
+ export declare const getFileListHeaders: () => Header[];
@@ -21,7 +21,7 @@
21
21
  */
22
22
  import { FileType } from './fileType';
23
23
  import { Node } from './node';
24
- import NodeData from './nodeData';
24
+ import { NodeData } from './nodeData';
25
25
  export declare class Folder extends Node {
26
26
  constructor(data: NodeData);
27
27
  get type(): FileType;
@@ -1,6 +1,16 @@
1
1
  import { Permission } from '../permissions';
2
2
  import { FileType } from './fileType';
3
- import NodeData, { Attribute } from './nodeData';
3
+ import { Attribute, NodeData } from './nodeData';
4
+ export declare enum NodeStatus {
5
+ /** This is a new node and it doesn't exists on the filesystem yet */
6
+ NEW = "new",
7
+ /** This node has failed and is unavailable */
8
+ FAILED = "failed",
9
+ /** This node is currently loading or have an operation in progress */
10
+ LOADING = "loading",
11
+ /** This node is locked and cannot be modified */
12
+ LOCKED = "locked"
13
+ }
4
14
  export declare abstract class Node {
5
15
  private _data;
6
16
  private _attributes;
@@ -10,6 +20,10 @@ export declare abstract class Node {
10
20
  * Get the source url to this object
11
21
  */
12
22
  get source(): string;
23
+ /**
24
+ * Get the encoded source url to this object for requests purposes
25
+ */
26
+ get encodedSource(): string;
13
27
  /**
14
28
  * Get this object name
15
29
  */
@@ -68,9 +82,18 @@ export declare abstract class Node {
68
82
  */
69
83
  get path(): string;
70
84
  /**
71
- * Get the file id if defined in attributes
85
+ * Get the node id if defined.
86
+ * Will look for the fileid in attributes if undefined.
72
87
  */
73
88
  get fileid(): number | undefined;
89
+ /**
90
+ * Get the node status.
91
+ */
92
+ get status(): NodeStatus | undefined;
93
+ /**
94
+ * Set the node status.
95
+ */
96
+ set status(status: NodeStatus | undefined);
74
97
  /**
75
98
  * Move the node to a new destination
76
99
  *
@@ -81,6 +104,12 @@ export declare abstract class Node {
81
104
  /**
82
105
  * Rename the node
83
106
  * This aliases the move method for easier usage
107
+ *
108
+ * @param basename The new name of the node
109
+ */
110
+ rename(basename: string): void;
111
+ /**
112
+ * Update the mtime if exists.
84
113
  */
85
- rename(basename: any): void;
114
+ private updateMtime;
86
115
  }
@@ -19,11 +19,12 @@
19
19
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
20
  *
21
21
  */
22
- import { Permission } from "../permissions";
22
+ import { Permission } from '../permissions';
23
+ import { NodeStatus } from './node';
23
24
  export interface Attribute {
24
25
  [key: string]: any;
25
26
  }
26
- export default interface NodeData {
27
+ export interface NodeData {
27
28
  /** Unique ID */
28
29
  id?: number;
29
30
  /**
@@ -36,7 +37,7 @@ export default interface NodeData {
36
37
  mtime?: Date;
37
38
  /** Creation time */
38
39
  crtime?: Date;
39
- /** The mime type */
40
+ /** The mime type Optional for folders only */
40
41
  mime?: string;
41
42
  /** The node size type */
42
43
  size?: number;
@@ -44,6 +45,7 @@ export default interface NodeData {
44
45
  permissions?: Permission;
45
46
  /** The owner UID of this node */
46
47
  owner: string | null;
48
+ /** The node attributes */
47
49
  attributes?: Attribute;
48
50
  /**
49
51
  * The absolute root of the home relative to the service.
@@ -51,9 +53,20 @@ export default interface NodeData {
51
53
  * e.g. /files/emma
52
54
  */
53
55
  root?: string;
56
+ /** The node status */
57
+ status?: NodeStatus;
54
58
  }
59
+ /**
60
+ * Check if a node source is from a specific DAV service
61
+ *
62
+ * @param source The source to check
63
+ * @param davService Pattern to check if source is DAV ressource
64
+ */
55
65
  export declare const isDavRessource: (source: string, davService: RegExp) => boolean;
56
66
  /**
57
67
  * Validate Node construct data
68
+ *
69
+ * @param data The node data
70
+ * @param davService Pattern to check if source is DAV ressource
58
71
  */
59
72
  export declare const validateData: (data: NodeData, davService: RegExp) => void;
@@ -23,7 +23,21 @@
23
23
  /**
24
24
  * Format a file size in a human-like format. e.g. 42GB
25
25
  *
26
+ * The default for Nextcloud is like Windows using binary sizes but showing decimal units,
27
+ * meaning 1024 bytes will show as 1KB instead of 1KiB or like on Apple 1.02 KB
28
+ *
26
29
  * @param size in bytes
27
30
  * @param skipSmallSizes avoid rendering tiny sizes and return '< 1 KB' instead
31
+ * @param binaryPrefixes True if size binary prefixes like `KiB` should be used as per IEC 80000-13
32
+ * @param base1000 Set to true to use base 1000 as per SI or used by Apple (default is base 1024 like Linux or Windows)
33
+ */
34
+ export declare function formatFileSize(size: number | string, skipSmallSizes?: boolean, binaryPrefixes?: boolean, base1000?: boolean): string;
35
+ /**
36
+ * Returns a file size in bytes from a humanly readable string
37
+ * Note: `b` and `B` are both parsed as bytes and not as bit or byte.
38
+ *
39
+ * @param {string} value file size in human-readable format
40
+ * @param {boolean} forceBinary for backwards compatibility this allows values to be base 2 (so 2KB means 2048 bytes instead of 2000 bytes)
41
+ * @return {number} or null if string could not be parsed
28
42
  */
29
- export declare function formatFileSize(size: number | string, skipSmallSizes?: boolean, binaryPrefixes?: boolean): string;
43
+ export declare function parseFileSize(value: string, forceBinary?: boolean): number | null;