-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paths3contents.ts
818 lines (742 loc) · 20.3 KB
/
s3contents.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
import { Signal, ISignal } from '@lumino/signaling';
import { Contents, ServerConnection } from '@jupyterlab/services';
import { URLExt, PathExt } from '@jupyterlab/coreutils';
import { JupyterFrontEnd } from '@jupyterlab/application';
import {
S3Client,
GetBucketLocationCommand,
S3ClientConfig
} from '@aws-sdk/client-s3';
import {
checkS3Object,
createS3Object,
copyS3Objects,
countS3ObjectNameAppearances,
deleteS3Objects,
presignedS3Url,
renameS3Objects,
listS3Contents,
IRegisteredFileTypes,
getS3FileContents
} from './s3';
let data: Contents.IModel = {
name: '',
path: '',
last_modified: '',
created: '',
content: null,
format: null,
mimetype: '',
size: 0,
writable: true,
type: ''
};
export class Drive implements Contents.IDrive {
/**
* Construct a new drive object.
*
* @param options - The options used to initialize the object.
*/
constructor(options: Drive.IOptions) {
const { config, name, root } = options;
this._serverSettings = ServerConnection.makeSettings();
this._s3Client = new S3Client(config ?? {});
this._name = name;
this._baseUrl = URLExt.join(
(config?.endpoint as string) ?? 'https://s3.amazonaws.com/',
this._name
);
this._provider = 'S3';
const region = config?.region;
if (typeof region === 'string') {
this._region = region;
} else {
const regionPromise = region ?? this.getRegion;
regionPromise().then((region: string) => {
this._region = region!;
});
}
this.formatRoot(root ?? '').then((root: string) => {
this._root = root;
});
this._registeredFileTypes = {};
}
/**
* The Drive S3 client
*/
get s3Client(): S3Client {
return this._s3Client;
}
/**
* The Drive S3 client
*/
set s3Client(s3Client: S3Client) {
this._s3Client = s3Client;
}
/**
* The Drive base URL
*/
get baseUrl(): string {
return this._baseUrl;
}
/**
* The Drive base URL
*/
set baseUrl(url: string) {
this._baseUrl = url;
}
/**
* The Drive name getter
*/
get name(): string {
return this._name;
}
/**
* The Drive name setter
*/
set name(name: string) {
this._name = name;
// if name of drive is changed, the filebrowser needs to refresh its contents
// as we are switching to another bucket
this._fileChanged.emit({
type: 'new',
oldValue: null,
newValue: { path: '' }
});
}
/**
* The Drive root getter
*/
get root(): string {
return this._root;
}
/**
* The Drive root setter
*/
set root(root: string) {
this.formatRoot(root ?? '').then(root => (this._root = root));
}
/**
* The Drive provider getter
*/
get provider(): string {
return this._provider;
}
/**
* The Drive provider setter */
set provider(name: string) {
this._provider = name;
}
/**
* The Drive region getter
*/
get region(): string {
return this._region;
}
/**
* The Drive region setter
*/
set region(region: string) {
this._region = region;
}
/**
* The Drive creationDate getter
*/
get creationDate(): string {
return this._creationDate;
}
/**
* The Drive creationDate setter
*/
set creationDate(date: string) {
this._creationDate = date;
}
/**
* The registered file types
*/
get registeredFileTypes(): IRegisteredFileTypes {
return this._registeredFileTypes;
}
/**
* The registered file types
*/
set registeredFileTypes(fileTypes: IRegisteredFileTypes) {
this._registeredFileTypes = fileTypes;
}
/**
* Settings for the notebook server.
*/
get serverSettings(): ServerConnection.ISettings {
return this._serverSettings;
}
/**
* 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;
}
/**
* A signal emitted when the drive is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Dispose of the resources held by the manager.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this._disposed.emit();
Signal.clearData(this);
}
/**
* 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,
* use [[ContentsManager.getAbsolutePath]] to get an absolute
* path if necessary.
*/
async getDownloadUrl(path: string): Promise<string> {
const url = await presignedS3Url(this._s3Client, this._name, path);
return url;
}
/**
* 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.
*
* Uses the [Jupyter Notebook API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/contents) and validates the response model.
*/
async get(
path: string,
options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
path = path.replace(this._name + '/', '');
// getting the list of files from the root
if (!path) {
data = await listS3Contents(
this._s3Client,
this._name,
this._root,
this._registeredFileTypes
);
} else {
const currentPath = PathExt.basename(path);
// listing contents of a folder
if (PathExt.extname(currentPath) === '') {
data = await listS3Contents(
this._s3Client,
this._name,
this.root,
this.registeredFileTypes,
path
);
}
// getting the contents of a specific file
else {
data = await getS3FileContents(
this._s3Client,
this._name,
this._root,
path,
this.registeredFileTypes
);
}
}
Contents.validateContentsModel(data);
return data;
}
/**
* 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.
*/
async newUntitled(
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
// get current list of contents of drive
const old_data = await listS3Contents(
this._s3Client,
this._name,
this._root,
this.registeredFileTypes,
options.path
);
if (options.type !== undefined) {
// get incremented untitled name
const name = this.incrementUntitledName(old_data, options);
data = await createS3Object(
this._s3Client,
this._name,
this._root,
name,
options.path ? PathExt.join(options.path, name) : name,
'', // create new file with empty body,
this.registeredFileTypes
);
} else {
console.warn('Type of new element is undefined');
}
Contents.validateContentsModel(data);
this._fileChanged.emit({
type: 'new',
oldValue: null,
newValue: data
});
return data;
}
incrementUntitledName(
contents: Contents.IModel,
options: Contents.ICreateOptions
): string {
const content: Array<Contents.IModel> = contents.content;
let name: string = '';
let countText = 0;
let countDir = 0;
let countNotebook = 0;
if (options.type === 'notebook') {
options.ext = 'ipynb';
}
content.forEach(item => {
if (options.ext !== undefined) {
if (item.name.includes('untitled') && item.name.includes('.txt')) {
countText = countText + 1;
} else if (
item.name.includes('Untitled') &&
item.name.includes('.ipynb')
) {
countNotebook = countNotebook + 1;
}
} else if (item.name.includes('Untitled Folder')) {
countDir = countDir + 1;
}
});
if (options.ext === 'txt') {
if (countText === 0) {
name = 'untitled' + '.' + options.ext;
} else {
name = 'untitled' + countText + '.' + options.ext;
}
}
if (options.ext === 'ipynb') {
if (countNotebook === 0) {
name = 'Untitled' + '.' + options.ext;
} else {
name = 'Untitled' + countNotebook + '.' + options.ext;
}
} else if (options.type === 'directory') {
if (countDir === 0) {
name = 'Untitled Folder';
} else {
name = 'Untitled Folder ' + countDir;
}
}
return name;
}
/**
* Delete a file.
*
* @param path - The path to the file.
*
* @returns A promise which resolves when the file is deleted.
*/
async delete(localPath: string): Promise<void> {
await deleteS3Objects(this._s3Client, this._name, this._root, localPath);
this._fileChanged.emit({
type: 'delete',
oldValue: { path: localPath },
newValue: { path: undefined }
});
}
/**
* 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 contents model when
* the file is renamed.
*
* #### Notes
* Uses the [Jupyter Notebook API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/contents) and validates the response model.
*/
async rename(
oldLocalPath: string,
newLocalPath: string,
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
let newFileName = PathExt.basename(newLocalPath);
try {
await checkS3Object(this._s3Client, this._name, this._root, newLocalPath);
newFileName = await this.incrementName(newLocalPath, this._name);
} catch (error) {
// HEAD request failed for this file name, continue, as name doesn't already exist.
} finally {
data = await renameS3Objects(
this._s3Client,
this._name,
this._root,
oldLocalPath,
newLocalPath,
newFileName,
this._registeredFileTypes
);
}
Contents.validateContentsModel(data);
this._fileChanged.emit({
type: 'rename',
oldValue: { path: oldLocalPath },
newValue: data
});
return data;
}
/**
* Helping function to increment name of existing files or directorties.
*
* @param localPath - Path to file.
*
* @param bucketName - The name of the bucket where content is moved.
*
* @param root - The root of the bucket, if it exists.
*/
async incrementName(localPath: string, bucketName: string) {
const isDir: boolean = PathExt.extname(localPath) === '';
let fileExtension: string = '';
let originalName: string = '';
// check if we are dealing with a directory
if (isDir) {
localPath = localPath.substring(0, localPath.length - 1);
originalName = PathExt.basename(localPath);
}
// dealing with a file
else {
// extract name from path
originalName = PathExt.basename(localPath);
// eliminate file extension
fileExtension = PathExt.extname(originalName);
originalName =
originalName.split('.')[originalName.split('.').length - 2];
}
const counter = await countS3ObjectNameAppearances(
this._s3Client,
bucketName,
this._root,
localPath,
originalName
);
let newName = counter ? originalName + counter : originalName;
newName = isDir ? newName + '/' : newName + fileExtension;
return newName;
}
/**
* 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.
*
* #### Notes
* Ensure that `model.content` is populated for the file.
*
* Uses the [Jupyter Notebook API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/contents) and validates the response model.
*/
async save(
localPath: string,
options: Partial<Contents.IModel> = {}
): Promise<Contents.IModel> {
const fileName = PathExt.basename(localPath);
data = await createS3Object(
this._s3Client,
this._name,
this._root,
fileName,
localPath,
options.content,
this._registeredFileTypes,
options
);
Contents.validateContentsModel(data);
this._fileChanged.emit({
type: 'save',
oldValue: null,
newValue: data
});
return data;
}
/**
* Copy a file into a given directory.
*
* @param path - The original file path.
*
* @param bucketName - The name of the bucket where content is moved.
*
* @returns A promise which resolves with the new name when the
* file is copied.
*/
async incrementCopyName(copiedItemPath: string, bucketName: string) {
const isDir: boolean = PathExt.extname(copiedItemPath) === '';
// extracting original file name
const originalFileName = PathExt.basename(copiedItemPath);
// constructing new file name and path with -Copy string
const newFileName = isDir
? originalFileName + '-Copy'
: originalFileName.split('.')[0] +
'-Copy.' +
originalFileName.split('.')[1];
const newFilePath =
copiedItemPath.substring(0, copiedItemPath.lastIndexOf('/') + 1) +
newFileName +
(isDir ? '/' : '');
// getting incremented name of Copy in case of duplicates
const incrementedName = await this.incrementName(newFilePath, bucketName);
return incrementedName;
}
/**
* 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.
*/
async copy(
path: string,
toDir: string,
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
// construct new file or directory name for the copy
const newFileName = await this.incrementCopyName(path, this._name);
data = await copyS3Objects(
this._s3Client,
this._name,
this._root,
newFileName,
path,
toDir,
this._registeredFileTypes
);
Contents.validateContentsModel(data);
this._fileChanged.emit({
type: 'new',
oldValue: null,
newValue: data
});
return data;
}
/**
* Copy a file into another bucket.
*
* @param path - The original file path.
*
* @param toDir - The destination directory path.
*
* @param bucketName - The name of the bucket where content is moved.
*
* @returns A promise which resolves with the new contents model when the
* file is copied.
*/
async copyToAnotherBucket(
path: string,
toDir: string,
bucketName: string,
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
// construct new file or directory name for the copy
const newFileName = await this.incrementCopyName(path, bucketName);
data = await copyS3Objects(
this._s3Client,
this._name,
this._root,
newFileName,
path,
toDir,
this._registeredFileTypes,
bucketName
);
this._fileChanged.emit({
type: 'new',
oldValue: null,
newValue: data
});
Contents.validateContentsModel(data);
return data;
}
/**
* 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.
*/
async createCheckpoint(path: string): Promise<Contents.ICheckpointModel> {
const emptyCheckpoint: Contents.ICheckpointModel = {
id: '',
last_modified: ''
};
return Promise.resolve(emptyCheckpoint);
}
/**
* 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<Contents.ICheckpointModel[]> {
return Promise.resolve([]);
}
/**
* 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> {
return Promise.reject('Repository is read only');
}
/**
* 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> {
return Promise.reject('Read only');
}
/**
* Helping function for extracting region of bucket.
* @returns region of Bucket
*/
private async getRegion() {
const response = await this._s3Client.send(
new GetBucketLocationCommand({
Bucket: this._name
})
);
return (response?.LocationConstraint as string) ?? '';
}
/**
* Get all registered file types and store them accordingly with their file
* extension (e.g.: .txt, .pdf, .jpeg), file mimetype (e.g.: text/plain, application/pdf)
* and file format (e.g.: base64, text).
*
* @param app
*/
getRegisteredFileTypes(app: JupyterFrontEnd) {
// get called when instating the toolbar
const registeredFileTypes = app.docRegistry.fileTypes();
for (const fileType of registeredFileTypes) {
// check if we are dealing with a directory
if (fileType.extensions.length === 0) {
this._registeredFileTypes[''] = {
fileType: 'directory',
fileFormat: 'json',
fileMimeTypes: ['text/directory']
};
}
// store the mimetype and fileformat for each file extension
fileType.extensions.forEach(extension => {
if (!this._registeredFileTypes[extension]) {
this._registeredFileTypes[extension] = {
fileType: fileType.name,
fileMimeTypes: [...fileType.mimeTypes],
fileFormat: fileType.fileFormat ? fileType.fileFormat : ''
};
}
});
}
}
/**
* Helping function which formats root by removing all leading or trailing
* backslashes and checking if given path to directory exists.
*
* @param root
* @returns formatted root
*/
private async formatRoot(root: string) {
// if root is empty, no formatting needed
if (root === '') {
return root;
}
// reformat the path to arbitrary root so it has no leading or trailing /
root = PathExt.removeSlash(PathExt.normalize(root));
// check if directory exists within bucket
try {
await checkS3Object(this._s3Client, this._name, root);
// the directory exists, root is formatted correctly
return root;
} catch (error) {
console.log("Given path to root directory doesn't exist within bucket.");
return '';
}
}
private _serverSettings: ServerConnection.ISettings;
private _s3Client: S3Client;
private _name: string = '';
private _root: string = '';
private _provider: string = '';
private _baseUrl: string = '';
private _region: string = '';
private _creationDate: string = '';
private _fileChanged = new Signal<this, Contents.IChangedArgs>(this);
private _isDisposed: boolean = false;
private _disposed = new Signal<this, void>(this);
private _registeredFileTypes: IRegisteredFileTypes = {};
}
export namespace Drive {
/**
* The options used to initialize a `Drive`.
*/
export interface IOptions {
/**
* S3 client configuration if available
*/
config?: S3ClientConfig;
/**
* The name for the `Drive`, which is used in file
* paths to disambiguate it from other drives.
*/
name: string;
/**
* Path to directory from drive, which acts as root.
*/
root: string;
/**
* The server settings for the server.
*/
serverSettings?: ServerConnection.ISettings;
}
}