@nextcloud/files 3.0.0-beta.3 → 3.0.0-beta.5

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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @copyright Copyright (c) 2022 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 { FileType } from './fileType';
23
+ import Node from './node';
24
+ export declare class File extends Node {
25
+ get type(): FileType;
26
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @copyright Copyright (c) 2022 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
+ export declare enum FileType {
23
+ Folder = "folder",
24
+ File = "file"
25
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @copyright Copyright (c) 2022 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 { FileType } from './fileType';
23
+ import Node from './node';
24
+ import NodeData from './nodeData';
25
+ export declare class Folder extends Node {
26
+ constructor(data: NodeData);
27
+ get type(): FileType;
28
+ get extension(): string | null;
29
+ get mime(): string;
30
+ }
@@ -0,0 +1,69 @@
1
+ import { Permission } from '../permissions';
2
+ import { FileType } from './fileType';
3
+ import NodeData, { Attribute } from './nodeData';
4
+ export default abstract class Node {
5
+ private _data;
6
+ private _attributes;
7
+ private _knownDavService;
8
+ constructor(data: NodeData, davService?: RegExp);
9
+ /**
10
+ * Get the source url to this object
11
+ */
12
+ get source(): string;
13
+ /**
14
+ * Get this object name
15
+ */
16
+ get basename(): string;
17
+ /**
18
+ * Get this object's extension
19
+ */
20
+ get extension(): string | null;
21
+ /**
22
+ * Get the directory path leading to this object
23
+ */
24
+ get dirname(): string;
25
+ /**
26
+ * Is it a file or a folder ?
27
+ */
28
+ abstract get type(): FileType;
29
+ /**
30
+ * Get the file mime
31
+ */
32
+ get mime(): string | undefined;
33
+ /**
34
+ * Get the file size
35
+ */
36
+ get size(): number | undefined;
37
+ /**
38
+ * Get the file attribute
39
+ */
40
+ get attributes(): Attribute;
41
+ /**
42
+ * Get the file permissions
43
+ */
44
+ get permissions(): Permission;
45
+ /**
46
+ * Get the file owner
47
+ */
48
+ get owner(): string | null;
49
+ /**
50
+ * Is this a dav-related ressource ?
51
+ */
52
+ get isDavRessource(): boolean;
53
+ /**
54
+ * Get the dav root of this object
55
+ */
56
+ get root(): string | null;
57
+ /**
58
+ * Move the node to a new destination
59
+ *
60
+ * @param {string} destination the new source.
61
+ * e.g. https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg
62
+ */
63
+ move(destination: string): void;
64
+ /**
65
+ * Rename the node
66
+ * This aliases the move method for easier usage
67
+ */
68
+ rename(basename: any): void;
69
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @copyright Copyright (c) 2022 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 { Permission } from "../permissions";
23
+ export interface Attribute {
24
+ [key: string]: any;
25
+ }
26
+ export default interface NodeData {
27
+ /** Unique ID */
28
+ id?: number;
29
+ /** URL to the ressource
30
+ * e.g. https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg
31
+ * or https://domain.com/Photos/picture.jpg
32
+ */
33
+ source: string;
34
+ /** Last modified time */
35
+ mtime?: Date;
36
+ /** Creation time */
37
+ crtime?: Date;
38
+ /** The mime type */
39
+ mime?: string;
40
+ /** The node size type */
41
+ size?: number;
42
+ /** The node permissions */
43
+ permissions?: Permission;
44
+ /** The owner UID of this node */
45
+ owner: string | null;
46
+ attributes?: Attribute;
47
+ }
48
+ /**
49
+ * Validate Node construct data
50
+ */
51
+ export declare const validateData: (data: NodeData) => void;
package/dist/index.d.ts CHANGED
@@ -23,6 +23,10 @@
23
23
  export { formatFileSize } from './humanfilesize';
24
24
  export { type Entry } from './newFileMenu';
25
25
  import { type Entry, NewFileMenu } from './newFileMenu';
26
+ export { FileType } from './files/fileType';
27
+ export { File } from './files/file';
28
+ export { Folder } from './files/folder';
29
+ export { Permission } from './permissions';
26
30
  declare global {
27
31
  interface Window {
28
32
  OC: any;
package/dist/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { getCanonicalLocale } from '@nextcloud/l10n';
2
2
  import { getCurrentUser } from '@nextcloud/auth';
3
3
  import { getLoggerBuilder } from '@nextcloud/logger';
4
+ import { basename, extname, dirname } from 'path';
4
5
 
5
6
  /**
6
7
  * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
@@ -145,21 +146,28 @@ class NewFileMenu {
145
146
  return this._entries.findIndex(entry => entry.id === id);
146
147
  }
147
148
  validateEntry(entry) {
148
- if (!entry.id || !entry.displayName || !entry.templateName || !(entry.iconSvgInline || entry.iconClass) || !entry.handler) {
149
+ if (!entry.id || !entry.displayName || !(entry.iconSvgInline || entry.iconClass)) {
149
150
  throw new Error('Invalid entry');
150
151
  }
151
152
  if (typeof entry.id !== 'string'
152
- || typeof entry.displayName !== 'string'
153
- || typeof entry.templateName !== 'string'
154
- || (entry.iconClass && typeof entry.iconClass !== 'string')
153
+ || typeof entry.displayName !== 'string') {
154
+ throw new Error('Invalid id or displayName property');
155
+ }
156
+ if ((entry.iconClass && typeof entry.iconClass !== 'string')
155
157
  || (entry.iconSvgInline && typeof entry.iconSvgInline !== 'string')) {
156
- throw new Error('Invalid entry property');
158
+ throw new Error('Invalid icon provided');
157
159
  }
158
160
  if (entry.if !== undefined && typeof entry.if !== 'function') {
159
- throw new Error('Invalid entry, if must be a valid function');
161
+ throw new Error('Invalid if property');
162
+ }
163
+ if (entry.templateName && typeof entry.templateName !== 'string') {
164
+ throw new Error('Invalid templateName property');
160
165
  }
161
- if (typeof entry.handler !== 'function') {
162
- throw new Error('Invalid entry handler');
166
+ if (entry.handler && typeof entry.handler !== 'function') {
167
+ throw new Error('Invalid handler property');
168
+ }
169
+ if (!entry.templateName && !entry.handler) {
170
+ throw new Error('At least a templateName or a handler must be provided');
163
171
  }
164
172
  if (this.getEntryIndex(entry.id) !== -1) {
165
173
  throw new Error('Duplicate entry');
@@ -174,6 +182,328 @@ const getNewFileMenu = function () {
174
182
  return window._nc_newfilemenu;
175
183
  };
176
184
 
185
+ /**
186
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
187
+ *
188
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
189
+ *
190
+ * @license AGPL-3.0-or-later
191
+ *
192
+ * This program is free software: you can redistribute it and/or modify
193
+ * it under the terms of the GNU Affero General Public License as
194
+ * published by the Free Software Foundation, either version 3 of the
195
+ * License, or (at your option) any later version.
196
+ *
197
+ * This program is distributed in the hope that it will be useful,
198
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
199
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
200
+ * GNU Affero General Public License for more details.
201
+ *
202
+ * You should have received a copy of the GNU Affero General Public License
203
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
204
+ *
205
+ */
206
+ var FileType;
207
+ (function (FileType) {
208
+ FileType["Folder"] = "folder";
209
+ FileType["File"] = "file";
210
+ })(FileType || (FileType = {}));
211
+
212
+ /**
213
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
214
+ *
215
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
216
+ *
217
+ * @license AGPL-3.0-or-later
218
+ *
219
+ * This program is free software: you can redistribute it and/or modify
220
+ * it under the terms of the GNU Affero General Public License as
221
+ * published by the Free Software Foundation, either version 3 of the
222
+ * License, or (at your option) any later version.
223
+ *
224
+ * This program is distributed in the hope that it will be useful,
225
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
226
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
227
+ * GNU Affero General Public License for more details.
228
+ *
229
+ * You should have received a copy of the GNU Affero General Public License
230
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
231
+ *
232
+ */
233
+ var Permission;
234
+ (function (Permission) {
235
+ Permission[Permission["NONE"] = 0] = "NONE";
236
+ Permission[Permission["CREATE"] = 4] = "CREATE";
237
+ Permission[Permission["READ"] = 1] = "READ";
238
+ Permission[Permission["UPDATE"] = 2] = "UPDATE";
239
+ Permission[Permission["DELETE"] = 8] = "DELETE";
240
+ Permission[Permission["SHARE"] = 16] = "SHARE";
241
+ Permission[Permission["ALL"] = 31] = "ALL";
242
+ })(Permission || (Permission = {}));
243
+
244
+ /**
245
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
246
+ *
247
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
248
+ *
249
+ * @license AGPL-3.0-or-later
250
+ *
251
+ * This program is free software: you can redistribute it and/or modify
252
+ * it under the terms of the GNU Affero General Public License as
253
+ * published by the Free Software Foundation, either version 3 of the
254
+ * License, or (at your option) any later version.
255
+ *
256
+ * This program is distributed in the hope that it will be useful,
257
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
258
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
259
+ * GNU Affero General Public License for more details.
260
+ *
261
+ * You should have received a copy of the GNU Affero General Public License
262
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
263
+ *
264
+ */
265
+ /**
266
+ * Validate Node construct data
267
+ */
268
+ const validateData = (data) => {
269
+ if ('id' in data && (typeof data.id !== 'number' || data.id < 0)) {
270
+ throw new Error('Invalid id type of value');
271
+ }
272
+ if (!data.source) {
273
+ throw new Error('Missing mandatory source');
274
+ }
275
+ if (!data.source.startsWith('http')) {
276
+ throw new Error('Invalid source format');
277
+ }
278
+ if ('mtime' in data && !(data.mtime instanceof Date)) {
279
+ throw new Error('Invalid mtime type');
280
+ }
281
+ if ('crtime' in data && !(data.crtime instanceof Date)) {
282
+ throw new Error('Invalid crtime type');
283
+ }
284
+ if (!data.mime || typeof data.mime !== 'string'
285
+ || !data.mime.match(/^[-\w.]+\/[-+\w.]+$/gi)) {
286
+ throw new Error('Missing or invalid mandatory mime');
287
+ }
288
+ if ('size' in data && typeof data.size !== 'number') {
289
+ throw new Error('Invalid size type');
290
+ }
291
+ if ('permissions' in data && !(typeof data.permissions === 'number'
292
+ && data.permissions >= Permission.NONE
293
+ && data.permissions <= Permission.ALL)) {
294
+ throw new Error('Invalid permissions');
295
+ }
296
+ if ('owner' in data
297
+ && data.owner !== null
298
+ && typeof data.owner !== 'string') {
299
+ throw new Error('Invalid owner type');
300
+ }
301
+ if ('attributes' in data && typeof data.attributes !== 'object') {
302
+ throw new Error('Invalid attributes format');
303
+ }
304
+ };
305
+
306
+ /**
307
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
308
+ *
309
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
310
+ *
311
+ * @license AGPL-3.0-or-later
312
+ *
313
+ * This program is free software: you can redistribute it and/or modify
314
+ * it under the terms of the GNU Affero General Public License as
315
+ * published by the Free Software Foundation, either version 3 of the
316
+ * License, or (at your option) any later version.
317
+ *
318
+ * This program is distributed in the hope that it will be useful,
319
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
320
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
321
+ * GNU Affero General Public License for more details.
322
+ *
323
+ * You should have received a copy of the GNU Affero General Public License
324
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
325
+ *
326
+ */
327
+ class Node {
328
+ _data;
329
+ _attributes;
330
+ _knownDavService = /(remote|public)\.php\/(web)?dav/i;
331
+ constructor(data, davService) {
332
+ // Validate data
333
+ validateData(data);
334
+ this._data = data;
335
+ this._attributes = data.attributes || {};
336
+ delete this._data.attributes;
337
+ if (davService) {
338
+ this._knownDavService = davService;
339
+ }
340
+ }
341
+ /**
342
+ * Get the source url to this object
343
+ */
344
+ get source() {
345
+ // strip any ending slash
346
+ return this._data.source.replace(/\/$/i, '');
347
+ }
348
+ /**
349
+ * Get this object name
350
+ */
351
+ get basename() {
352
+ return basename(this.source);
353
+ }
354
+ /**
355
+ * Get this object's extension
356
+ */
357
+ get extension() {
358
+ return extname(this.source);
359
+ }
360
+ /**
361
+ * Get the directory path leading to this object
362
+ */
363
+ get dirname() {
364
+ return dirname(this.source);
365
+ }
366
+ /**
367
+ * Get the file mime
368
+ */
369
+ get mime() {
370
+ return this._data.mime;
371
+ }
372
+ /**
373
+ * Get the file size
374
+ */
375
+ get size() {
376
+ return this._data.size;
377
+ }
378
+ /**
379
+ * Get the file attribute
380
+ */
381
+ get attributes() {
382
+ return this._attributes;
383
+ }
384
+ /**
385
+ * Get the file permissions
386
+ */
387
+ get permissions() {
388
+ // If this is not a dav ressource, we can only read it
389
+ if (this.owner === null && !this.isDavRessource) {
390
+ return Permission.READ;
391
+ }
392
+ return this._data.permissions || Permission.READ;
393
+ }
394
+ /**
395
+ * Get the file owner
396
+ */
397
+ get owner() {
398
+ // Remote ressources have no owner
399
+ if (!this.isDavRessource) {
400
+ return null;
401
+ }
402
+ return this._data.owner;
403
+ }
404
+ /**
405
+ * Is this a dav-related ressource ?
406
+ */
407
+ get isDavRessource() {
408
+ return this.source.match(this._knownDavService) !== null;
409
+ }
410
+ /**
411
+ * Get the dav root of this object
412
+ */
413
+ get root() {
414
+ if (this.isDavRessource) {
415
+ return this.dirname.split(this._knownDavService).pop() || null;
416
+ }
417
+ return null;
418
+ }
419
+ /**
420
+ * Move the node to a new destination
421
+ *
422
+ * @param {string} destination the new source.
423
+ * e.g. https://cloud.domain.com/remote.php/dav/files/emma/Photos/picture.jpg
424
+ */
425
+ move(destination) {
426
+ this._data.source = destination;
427
+ }
428
+ /**
429
+ * Rename the node
430
+ * This aliases the move method for easier usage
431
+ */
432
+ rename(basename) {
433
+ if (basename.includes('/')) {
434
+ throw new Error('Invalid basename');
435
+ }
436
+ this.move(this.dirname + '/' + basename);
437
+ }
438
+ }
439
+
440
+ /**
441
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
442
+ *
443
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
444
+ *
445
+ * @license AGPL-3.0-or-later
446
+ *
447
+ * This program is free software: you can redistribute it and/or modify
448
+ * it under the terms of the GNU Affero General Public License as
449
+ * published by the Free Software Foundation, either version 3 of the
450
+ * License, or (at your option) any later version.
451
+ *
452
+ * This program is distributed in the hope that it will be useful,
453
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
454
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
455
+ * GNU Affero General Public License for more details.
456
+ *
457
+ * You should have received a copy of the GNU Affero General Public License
458
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
459
+ *
460
+ */
461
+ class File extends Node {
462
+ get type() {
463
+ return FileType.File;
464
+ }
465
+ }
466
+
467
+ /**
468
+ * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
469
+ *
470
+ * @author John Molakvoæ <skjnldsv@protonmail.com>
471
+ *
472
+ * @license AGPL-3.0-or-later
473
+ *
474
+ * This program is free software: you can redistribute it and/or modify
475
+ * it under the terms of the GNU Affero General Public License as
476
+ * published by the Free Software Foundation, either version 3 of the
477
+ * License, or (at your option) any later version.
478
+ *
479
+ * This program is distributed in the hope that it will be useful,
480
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
481
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
482
+ * GNU Affero General Public License for more details.
483
+ *
484
+ * You should have received a copy of the GNU Affero General Public License
485
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
486
+ *
487
+ */
488
+ class Folder extends Node {
489
+ constructor(data) {
490
+ // enforcing mimes
491
+ super({
492
+ ...data,
493
+ mime: 'httpd/unix-directory'
494
+ });
495
+ }
496
+ get type() {
497
+ return FileType.Folder;
498
+ }
499
+ get extension() {
500
+ return null;
501
+ }
502
+ get mime() {
503
+ return 'httpd/unix-directory';
504
+ }
505
+ }
506
+
177
507
  /**
178
508
  * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
179
509
  *
@@ -220,5 +550,5 @@ const getNewFileMenuEntries = function (context) {
220
550
  return newFileMenu.getEntries(context);
221
551
  };
222
552
 
223
- export { addNewFileMenuEntry, formatFileSize, getNewFileMenuEntries, removeNewFileMenuEntry };
553
+ export { File, FileType, Folder, Permission, addNewFileMenuEntry, formatFileSize, getNewFileMenuEntries, removeNewFileMenuEntry };
224
554
  //# sourceMappingURL=index.esm.js.map