-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathindex.ts
1883 lines (1728 loc) · 52.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import type { DocumentChange, ISharedDocument, YDocument } from '@jupyter/ydoc';
import { PathExt, URLExt } from '@jupyterlab/coreutils';
import { PartialJSONObject } from '@lumino/coreutils';
import { DisposableDelegate, IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { ServerConnection } from '..';
import * as validate from './validate';
/**
* The url for the default drive service.
*/
const SERVICE_DRIVE_URL = 'api/contents';
/**
* The url for the file access.
*/
const FILES_URL = 'files';
/**
* A document factory for registering shared models
*/
export type SharedDocumentFactory = (
options: Contents.ISharedFactoryOptions
) => YDocument<DocumentChange>;
/**
* A namespace for contents interfaces.
*/
export namespace Contents {
/**
* A contents model.
*/
export interface IModel {
/**
* Name of the contents file.
*
* #### Notes
* Equivalent to the last part of the `path` field.
*/
readonly name: string;
/**
* The full file path.
*
* #### Notes
* It will *not* start with `/`, and it will be `/`-delimited.
*/
readonly path: string;
/**
* The path as returned by the server contents API.
*
* #### Notes
* Differently to `path` it does not include IDrive API prefix.
*/
readonly serverPath?: string;
/**
* The type of file.
*/
readonly type: ContentType;
/**
* Whether the requester has permission to edit the file.
*/
readonly writable: boolean;
/**
* File creation timestamp.
*/
readonly created: string;
/**
* Last modified timestamp.
*/
readonly last_modified: string;
/**
* Specify the mime-type of file contents.
*
* #### Notes
* Only non-`null` when `content` is present and `type` is `"file"`.
*/
readonly mimetype: string;
/**
* The optional file content.
*/
readonly content: any;
/**
* The chunk of the file upload.
*/
readonly chunk?: number;
/**
* The format of the file `content`.
*
* #### Notes
* Only relevant for type: 'file'
*/
readonly format: FileFormat;
/**
* The size of then file in bytes.
*/
readonly size?: number;
/**
* The indices of the matched characters in the name.
*/
indices?: ReadonlyArray<number> | null;
/**
* The hexdigest hash string of content, if requested (otherwise null).
* It cannot be null if hash_algorithm is defined.
*/
readonly hash?: string;
/**
* The algorithm used to compute hash value. It cannot be null if hash is defined.
*/
readonly hash_algorithm?: string;
}
/**
* Validates an IModel, throwing an error if it does not pass.
*/
export function validateContentsModel(contents: IModel): void {
validate.validateContentsModel(contents);
}
/**
* A contents file type. It can be anything but `jupyter-server`
* has special treatment for `notebook` and `directory` types.
* Anything else is considered as `file` type.
*/
export type ContentType = string;
/**
* A contents file format. Always `json` for `notebook` and
* `directory` types. It should be set to either `text` or
* `base64` for `file` type.
* See the
* [jupyter server data model for filesystem entities](https://jupyter-server.readthedocs.io/en/latest/developers/contents.html#filesystem-entities)
* for more details.
*/
export type FileFormat = 'json' | 'text' | 'base64' | null;
/**
* The options used to decode which provider to use.
*/
export interface IContentProvisionOptions {
/**
* The override for the content provider.
* @experimental
*/
contentProviderId?: string;
}
/**
* The options used to fetch a file.
*/
export interface IFetchOptions extends IContentProvisionOptions {
/**
* The override file type for the request.
*/
type?: ContentType;
/**
* The override file format for the request.
*/
format?: FileFormat;
/**
* Whether to include the file content.
*
* The default is `true`.
*/
content?: boolean;
/**
* Whether to include a hash in the response.
*
* The default is `false`.
*/
hash?: boolean;
}
/**
* The options used to create a file.
*/
export interface ICreateOptions {
/**
* The directory in which to create the file.
*/
path?: string;
/**
* The optional file extension for the new file (e.g. `".txt"`).
*
* #### Notes
* This ignored if `type` is `'notebook'`.
*/
ext?: string;
/**
* The file type.
*/
type?: ContentType;
}
/**
* Checkpoint model.
*/
export interface ICheckpointModel {
/**
* The unique identifier for the checkpoint.
*/
readonly id: string;
/**
* Last modified timestamp.
*/
readonly last_modified: string;
}
/**
* Validates an ICheckpointModel, throwing an error if it does not pass.
*/
export function validateCheckpointModel(checkpoint: ICheckpointModel): void {
validate.validateCheckpointModel(checkpoint);
}
/**
* The change args for a file change.
*/
export interface IChangedArgs {
/**
* The type of change.
*/
type: 'new' | 'delete' | 'rename' | 'save';
/**
* The old contents.
*/
oldValue: Partial<IModel> | null;
/**
* The new contents.
*/
newValue: Partial<IModel> | null;
}
/**
* A factory interface for creating `ISharedDocument` objects.
*/
export interface ISharedFactory {
/**
* Whether the IDrive supports real-time collaboration or not.
* Note: If it is not provided, it is false by default.
*/
readonly collaborative?: boolean;
/**
* Create a new `ISharedDocument` instance.
*
* It should return `undefined` if the factory is not able to create a `ISharedDocument`.
*/
createNew(options: ISharedFactoryOptions): ISharedDocument | undefined;
/**
* Register a SharedDocumentFactory.
*
* @param type Document type
* @param factory Document factory
*/
registerDocumentFactory?(
type: Contents.ContentType,
factory: SharedDocumentFactory
): void;
}
/**
* The options used to instantiate a ISharedDocument
*/
export interface ISharedFactoryOptions {
/**
* The path of the file.
*/
path: string;
/**
* The format of the document. If null, the document won't be
* collaborative.
*/
format: FileFormat;
/**
* The content type of the document.
*/
contentType: ContentType;
/**
* Whether the document is collaborative or not.
*
* The default value is `true`.
*/
collaborative?: boolean;
}
/**
* The interface for a contents manager.
*/
export interface IManager extends IDisposable {
/**
* A signal emitted when a file operation takes place.
*/
readonly fileChanged: ISignal<IManager, IChangedArgs>;
/**
* The server settings associated with the manager.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* Add an `IDrive` to the manager.
*/
addDrive(drive: IDrive): void;
/**
* Given a path of the form `drive:local/portion/of/it.txt`
* get the local part of it.
*
* @param path the path.
*
* @returns The local part of the path.
*/
localPath(path: string): string;
/**
* Normalize a global path. Reduces '..' and '.' parts, and removes
* leading slashes from the local part of the path, while retaining
* the drive name if it exists.
*
* @param path the path.
*
* @returns The normalized path.
*/
normalize(path: string): string;
/**
* Resolve a global path, starting from the root path. Behaves like
* posix-path.resolve, with 3 differences:
* - will never prepend cwd
* - if root has a drive name, the result is prefixed with "<drive>:"
* - before adding drive name, leading slashes are removed
*
* @param path the path.
*
* @returns The normalized path.
*/
resolvePath(root: string, path: string): string;
/**
* Given a path of the form `drive:local/portion/of/it.txt`
* get the name of the drive. If the path is missing
* a drive portion, returns an empty string.
*
* @param path the path.
*
* @returns The drive name for the path, or the empty string.
*/
driveName(path: string): string;
/**
* Given a path, get a shared model IFactory from the
* relevant backend. Returns `null` if the backend
* does not provide one.
*/
getSharedModelFactory(
path: string,
options?: IContentProvisionOptions
): ISharedFactory | null;
/**
* Get a file or directory.
*
* @param path The path to the file.
*
* @param options The options used to fetch the file.
*
* @returns A promise which resolves with the file content.
*/
get(path: string, options?: IFetchOptions): Promise<IModel>;
/**
* Get an encoded download url given a file path.
*
* @param A promise which resolves with the absolute POSIX
* file path on the server.
*
* #### Notes
* The returned URL may include a query parameter.
*/
getDownloadUrl(path: string): Promise<string>;
/**
* Create a new untitled file or directory in the specified directory path.
*
* @param options The options used to create the file.
*
* @returns A promise which resolves with the created file content when the
* file is created.
*/
newUntitled(options?: ICreateOptions): Promise<IModel>;
/**
* Delete a file.
*
* @param path - The path to the file.
*
* @returns A promise which resolves when the file is deleted.
*/
delete(path: string): Promise<void>;
/**
* Rename a file or directory.
*
* @param path - The original file path.
*
* @param newPath - The new file path.
*
* @returns A promise which resolves with the new file content model when the
* file is renamed.
*/
rename(path: string, newPath: string): Promise<IModel>;
/**
* Save a file.
*
* @param path - The desired file path.
*
* @param options - Optional overrides to the model.
*
* @returns A promise which resolves with the file content model when the
* file is saved.
*/
save(
path: string,
options?: Partial<IModel> & Contents.IContentProvisionOptions
): Promise<IModel>;
/**
* Copy a file into a given directory.
*
* @param path - The original file path.
*
* @param toDir - The destination directory path.
*
* @returns A promise which resolves with the new content model when the
* file is copied.
*/
copy(path: string, toDir: string): Promise<IModel>;
/**
* Create a checkpoint for a file.
*
* @param path - The path of the file.
*
* @returns A promise which resolves with the new checkpoint model when the
* checkpoint is created.
*/
createCheckpoint(path: string): Promise<ICheckpointModel>;
/**
* List available checkpoints for a file.
*
* @param path - The path of the file.
*
* @returns A promise which resolves with a list of checkpoint models for
* the file.
*/
listCheckpoints(path: string): Promise<ICheckpointModel[]>;
/**
* Restore a file to a known checkpoint state.
*
* @param path - The path of the file.
*
* @param checkpointID - The id of the checkpoint to restore.
*
* @returns A promise which resolves when the checkpoint is restored.
*/
restoreCheckpoint(path: string, checkpointID: string): Promise<void>;
/**
* Delete a checkpoint for a file.
*
* @param path - The path of the file.
*
* @param checkpointID - The id of the checkpoint to delete.
*
* @returns A promise which resolves when the checkpoint is deleted.
*/
deleteCheckpoint(path: string, checkpointID: string): Promise<void>;
}
/**
* The interface for a network drive that can be mounted
* in the contents manager.
*/
export interface IDrive extends IDisposable {
/**
* An optional content provider registry, consisting of all the
* content providers that this drive can use to access files.
*/
readonly contentProviderRegistry?: IContentProviderRegistry;
/**
* The name of the drive, which is used at the leading
* component of file paths.
*/
readonly name: string;
/**
* The server settings of the manager.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* An optional shared model factory instance for the
* drive.
*/
readonly sharedModelFactory?: ISharedFactory;
/**
* A signal emitted when a file operation takes place.
*/
fileChanged: ISignal<IDrive, IChangedArgs>;
/**
* Get a file or directory.
*
* @param localPath The path to the file.
*
* @param options The options used to fetch the file.
*
* @returns A promise which resolves with the file content.
*/
get(localPath: string, options?: IFetchOptions): Promise<IModel>;
/**
* Get an encoded download url given a file path.
*
* @returns A promise which resolves with the absolute POSIX
* file path on the server.
*
* #### Notes
* The returned URL may include a query parameter.
*/
getDownloadUrl(localPath: string): Promise<string>;
/**
* Create a new untitled file or directory in the specified directory path.
*
* @param options The options used to create the file.
*
* @returns A promise which resolves with the created file content when the
* file is created.
*/
newUntitled(options?: ICreateOptions): Promise<IModel>;
/**
* Delete a file.
*
* @param localPath - The path to the file.
*
* @returns A promise which resolves when the file is deleted.
*/
delete(localPath: string): Promise<void>;
/**
* Rename a file or directory.
*
* @param oldLocalPath - The original file path.
*
* @param newLocalPath - The new file path.
*
* @returns A promise which resolves with the new file content model when the
* file is renamed.
*/
rename(oldLocalPath: string, newLocalPath: string): Promise<IModel>;
/**
* Save a file.
*
* @param localPath - The desired file path.
*
* @param options - Optional overrides to the model.
*
* @returns A promise which resolves with the file content model when the
* file is saved.
*/
save(localPath: string, options?: Partial<IModel>): Promise<IModel>;
/**
* Copy a file into a given directory.
*
* @param localPath - The original file path.
*
* @param toLocalDir - The destination directory path.
*
* @returns A promise which resolves with the new content model when the
* file is copied.
*/
copy(localPath: string, toLocalDir: string): Promise<IModel>;
/**
* Create a checkpoint for a file.
*
* @param localPath - The path of the file.
*
* @returns A promise which resolves with the new checkpoint model when the
* checkpoint is created.
*/
createCheckpoint(localPath: string): Promise<ICheckpointModel>;
/**
* List available checkpoints for a file.
*
* @param localPath - The path of the file.
*
* @returns A promise which resolves with a list of checkpoint models for
* the file.
*/
listCheckpoints(localPath: string): Promise<ICheckpointModel[]>;
/**
* Restore a file to a known checkpoint state.
*
* @param localPath - The path of the file.
*
* @param checkpointID - The id of the checkpoint to restore.
*
* @returns A promise which resolves when the checkpoint is restored.
*/
restoreCheckpoint(localPath: string, checkpointID: string): Promise<void>;
/**
* Delete a checkpoint for a file.
*
* @param localPath - The path of the file.
*
* @param checkpointID - The id of the checkpoint to delete.
*
* @returns A promise which resolves when the checkpoint is deleted.
*/
deleteCheckpoint(localPath: string, checkpointID: string): Promise<void>;
}
}
/**
* A contents manager that passes file operations to the server.
* Multiple servers implementing the `IDrive` interface can be
* attached to the contents manager, so that the same session can
* perform file operations on multiple backends.
*
* This includes checkpointing with the normal file operations.
*/
export class ContentsManager implements Contents.IManager {
/**
* Construct a new contents manager object.
*
* @param options - The options used to initialize the object.
*/
constructor(options: ContentsManager.IOptions = {}) {
const serverSettings = (this.serverSettings =
options.serverSettings ?? ServerConnection.makeSettings());
this._defaultDrive = options.defaultDrive ?? new Drive({ serverSettings });
this._defaultDrive.fileChanged.connect(this._onFileChanged, this);
}
/**
* The default drive associated with the manager.
*/
get defaultDrive(): Contents.IDrive {
return this._defaultDrive;
}
/**
* The server settings associated with the manager.
*/
readonly serverSettings: ServerConnection.ISettings;
/**
* A signal emitted when a file operation takes place.
*/
get fileChanged(): ISignal<this, Contents.IChangedArgs> {
return this._fileChanged;
}
/**
* Test whether the manager has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the manager.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
}
/**
* Add an `IDrive` to the manager.
*/
addDrive(drive: Contents.IDrive): void {
this._additionalDrives.set(drive.name, drive);
drive.fileChanged.connect(this._onFileChanged, this);
}
/**
* Given a path, get a shared model factory from the relevant backend.
* The factory defined on content provider best matching the given path
* takes precedence over the factory defined on the drive as a whole.
* Returns `null` if the backend does not provide one.
*/
getSharedModelFactory(
path: string,
options?: Contents.IContentProvisionOptions
): Contents.ISharedFactory | null {
const [drive] = this._driveForPath(path);
const provider = drive.contentProviderRegistry?.getProvider(
options?.contentProviderId
);
if (provider?.sharedModelFactory) {
return provider.sharedModelFactory;
}
return drive.sharedModelFactory ?? null;
}
/**
* Given a path of the form `drive:local/portion/of/it.txt`
* get the local part of it.
*
* @param path the path.
*
* @returns The local part of the path.
*/
localPath(path: string): string {
const parts = path.split('/');
const firstParts = parts[0].split(':');
if (firstParts.length === 1 || !this._additionalDrives.has(firstParts[0])) {
return PathExt.removeSlash(path);
}
return PathExt.join(firstParts.slice(1).join(':'), ...parts.slice(1));
}
/**
* Normalize a global path. Reduces '..' and '.' parts, and removes
* leading slashes from the local part of the path, while retaining
* the drive name if it exists.
*
* @param path the path.
*
* @returns The normalized path.
*/
normalize(path: string): string {
const parts = path.split(':');
if (parts.length === 1) {
return PathExt.normalize(path);
}
return `${parts[0]}:${PathExt.normalize(parts.slice(1).join(':'))}`;
}
/**
* Resolve a global path, starting from the root path. Behaves like
* posix-path.resolve, with 3 differences:
* - will never prepend cwd
* - if root has a drive name, the result is prefixed with "<drive>:"
* - before adding drive name, leading slashes are removed
*
* @param path the path.
*
* @returns The normalized path.
*/
resolvePath(root: string, path: string): string {
const driveName = this.driveName(root);
const localPath = this.localPath(root);
const resolved = PathExt.resolve('/', localPath, path);
return driveName ? `${driveName}:${resolved}` : resolved;
}
/**
* Given a path of the form `drive:local/portion/of/it.txt`
* get the name of the drive. If the path is missing
* a drive portion, returns an empty string.
*
* @param path the path.
*
* @returns The drive name for the path, or the empty string.
*/
driveName(path: string): string {
const parts = path.split('/');
const firstParts = parts[0].split(':');
if (firstParts.length === 1) {
return '';
}
if (this._additionalDrives.has(firstParts[0])) {
return firstParts[0];
}
return '';
}
/**
* Get a file or directory.
*
* @param path The path to the file.
*
* @param options The options used to fetch the file.
*
* @returns A promise which resolves with the file content.
*/
get(
path: string,
options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
const [drive, localPath] = this._driveForPath(path);
return drive.get(localPath, options).then(contentsModel => {
const listing: Contents.IModel[] = [];
if (contentsModel.type === 'directory' && contentsModel.content) {
for (const item of contentsModel.content) {
listing.push({ ...item, path: this._toGlobalPath(drive, item.path) });
}
return {
...contentsModel,
path: this._toGlobalPath(drive, localPath),
content: listing,
serverPath: contentsModel.path
} as Contents.IModel;
} else {
return {
...contentsModel,
path: this._toGlobalPath(drive, localPath),
serverPath: contentsModel.path
} as Contents.IModel;
}
});
}
/**
* Get an encoded download url given a file path.
*
* @param path - An absolute POSIX file path on the server.
*
* #### Notes
* It is expected that the path contains no relative paths.
*
* The returned URL may include a query parameter.
*/
getDownloadUrl(path: string): Promise<string> {
const [drive, localPath] = this._driveForPath(path);
return drive.getDownloadUrl(localPath);
}
/**
* Create a new untitled file or directory in the specified directory path.
*
* @param options The options used to create the file.
*
* @returns A promise which resolves with the created file content when the
* file is created.
*/
newUntitled(options: Contents.ICreateOptions = {}): Promise<Contents.IModel> {
if (options.path) {
const globalPath = this.normalize(options.path);
const [drive, localPath] = this._driveForPath(globalPath);
return drive
.newUntitled({ ...options, path: localPath })
.then(contentsModel => {
return {
...contentsModel,
path: PathExt.join(globalPath, contentsModel.name),
serverPath: contentsModel.path
} as Contents.IModel;
});
} else {
return this._defaultDrive.newUntitled(options);
}
}
/**
* Delete a file.
*
* @param path - The path to the file.
*
* @returns A promise which resolves when the file is deleted.
*/
delete(path: string): Promise<void> {
const [drive, localPath] = this._driveForPath(path);
return drive.delete(localPath);
}
/**
* Rename a file or directory.
*
* @param path - The original file path.
*
* @param newPath - The new file path.
*
* @returns A promise which resolves with the new file contents model when
* the file is renamed.
*/
rename(path: string, newPath: string): Promise<Contents.IModel> {
const [drive1, path1] = this._driveForPath(path);
const [drive2, path2] = this._driveForPath(newPath);
if (drive1 !== drive2) {
throw Error('ContentsManager: renaming files must occur within a Drive');
}
return drive1.rename(path1, path2).then(contentsModel => {
return {
...contentsModel,
path: this._toGlobalPath(drive1, path2),
serverPath: contentsModel.path
} as Contents.IModel;
});
}
/**
* Save a file.
*
* @param path - The desired file path.
*
* @param options - Optional overrides to the model.
*
* @returns A promise which resolves with the file content model when the
* file is saved.
*
* #### Notes
* Ensure that `model.content` is populated for the file.
*/
save(
path: string,
options: Partial<Contents.IModel> = {}
): Promise<Contents.IModel> {
const globalPath = this.normalize(path);
const [drive, localPath] = this._driveForPath(path);
return drive
.save(localPath, { ...options, path: localPath })
.then(contentsModel => {
return {
...contentsModel,
path: globalPath,
serverPath: contentsModel.path
} as Contents.IModel;
});
}
/**
* Copy a file into a given directory.
*
* @param path - The original file path.
*
* @param toDir - The destination directory path.
*
* @returns A promise which resolves with the new contents model when the
* file is copied.
*
* #### Notes
* The server will select the name of the copied file.
*/
copy(fromFile: string, toDir: string): Promise<Contents.IModel> {
const [drive1, path1] = this._driveForPath(fromFile);
const [drive2, path2] = this._driveForPath(toDir);
if (drive1 === drive2) {
return drive1.copy(path1, path2).then(contentsModel => {
return {
...contentsModel,
path: this._toGlobalPath(drive1, contentsModel.path),
serverPath: contentsModel.path
} as Contents.IModel;
});
} else {
throw Error('Copying files between drives is not currently implemented');
}
}