-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
826 lines (755 loc) · 26 KB
/
Copy pathindex.ts
File metadata and controls
826 lines (755 loc) · 26 KB
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
import { Token } from '../../auth';
import { ApiSecurity, ApiUrl, AppDetails } from '../../shared';
import { CustomHeaders, addResourcesTokenToHeaders, headersWithToken } from '../../shared/headers';
import { HttpClient, RequestCanceler } from '../../shared/http/client';
import { UUID } from '../../shared/types/userSettings';
import { ItemType } from './../../workspaces/types';
import {
AddItemsToTrashPayload,
CheckDuplicatedFilesPayload,
CheckDuplicatedFilesResponse,
CheckDuplicatedFolderPayload,
CheckDuplicatedFoldersResponse,
CreateFolderByUuidPayload,
CreateFolderPayload,
CreateFolderResponse,
CreateThumbnailEntryPayload,
DeleteFilePayload,
DriveFileData,
FetchFolderContentResponse,
FetchLimitResponse,
FetchPaginatedFilesContent,
FetchPaginatedFolderContentResponse,
FetchPaginatedFoldersContent,
FileEntry,
FileEntryByUuid,
FileMeta,
FolderAncestor,
FolderAncestorWorkspace,
FolderMeta,
FolderTreeResponse,
MoveFilePayload,
MoveFileResponse,
MoveFileUuidPayload,
MoveFolderPayload,
MoveFolderResponse,
MoveFolderUuidPayload,
ReplaceFile,
SearchResultData,
Thumbnail,
ThumbnailEntry,
UpdateFilePayload,
UpdateFolderMetadataPayload,
UsageResponse,
UsageResponseV2,
} from './types';
export * as StorageTypes from './types';
export class Storage {
private readonly client: HttpClient;
private readonly appDetails: AppDetails;
private readonly apiSecurity: ApiSecurity;
public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
return new Storage(apiUrl, appDetails, apiSecurity);
}
private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity: ApiSecurity) {
this.client = HttpClient.create(apiUrl, apiSecurity.unauthorizedCallback);
this.appDetails = appDetails;
this.apiSecurity = apiSecurity;
}
/**
* Creates a new folder
* @param payload
*/
public createFolder(payload: CreateFolderPayload): [Promise<CreateFolderResponse>, RequestCanceler] {
const { promise, requestCanceler } = this.client.postCancellable<CreateFolderResponse>(
'/storage/folder',
{
parentFolderId: payload.parentFolderId,
folderName: payload.folderName,
},
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Creates a new folder
* @param payload
*/
public createFolderByUuid(payload: CreateFolderByUuidPayload): [Promise<CreateFolderResponse>, RequestCanceler] {
const { promise, requestCanceler } = this.client.postCancellable<CreateFolderResponse>(
'/folders',
{
plainName: payload.plainName,
parentFolderUuid: payload.parentFolderUuid,
},
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Moves a specific folder to a new location
* @param payload
*/
public async moveFolder(payload: MoveFolderPayload): Promise<MoveFolderResponse> {
return this.client.post(
'/storage/move/folder',
{
folderId: payload.folderId,
destination: payload.destinationFolderId,
},
this.headers(),
);
}
/**
* Moves a specific folder to a new location
* @param payload
*/
public async moveFolderByUuid(payload: MoveFolderUuidPayload): Promise<FolderMeta> {
return this.client.patch(
`/folders/${payload.folderUuid}`,
{
destinationFolder: payload.destinationFolderUuid,
},
this.headers(),
);
}
/**
* Updates the metadata of a folder
* @param payload
*/
public async updateFolder(payload: UpdateFolderMetadataPayload): Promise<void> {
await this.client.post(
`/storage/folder/${payload.folderId}/meta`,
{
metadata: payload.changes,
},
this.headers(),
);
}
/**
* Updates the name of a folder with the given UUID.
*
* @param {Object} payload - The payload containing the folder UUID and the new name.
* @param {string} payload.folderUuid - The UUID of the folder to update.
* @param {string} payload.name - The new name for the folder.
* @param {Token} [resourcesToken] - An optional token for authentication.
* @return {Promise<void>} A promise that resolves when the folder name is successfully updated.
*/
public updateFolderNameWithUUID(
payload: { folderUuid: string; name: string },
resourcesToken?: Token,
): Promise<void> {
const { folderUuid, name } = payload;
return this.client.put(
`/folders/${folderUuid}/meta`,
{
plainName: name,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Fetches & returns the contents of a specific folder
* @param folderId
*/
public getFolderContent(folderId: number, trash = false): [Promise<FetchFolderContentResponse>, RequestCanceler] {
const query = trash ? '/?trash=true' : '';
const { promise, requestCanceler } = this.client.getCancellable<FetchFolderContentResponse>(
`/storage/v2/folder/${folderId}${query}`,
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Fetches and returns the contents of a specific folder by its UUID.
*
* @param {string} folderUuid - The UUID of the folder.
* @param {boolean} [trash=false] - Whether to include trash items in the response.
* @param {boolean} [offset] - The position of the first file to return.
* @param {boolean} [limit] - The max number of files to be returned.
* @param {boolean} [workspacesToken] - Token for accessing workspaces.
* @return {[Promise<FetchFolderContentResponse>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getFolderContentByUuid({
folderUuid,
trash = false,
offset,
limit,
workspacesToken,
}: {
folderUuid: string;
trash?: boolean;
limit?: number;
offset?: number;
workspacesToken?: string;
}): [Promise<FetchFolderContentResponse>, RequestCanceler] {
const query = new URLSearchParams();
if (offset !== undefined) query.set('offset', String(offset));
if (limit !== undefined) query.set('limit', String(limit));
if (trash) query.set('trash', 'true');
const customHeaders = workspacesToken
? {
'x-internxt-workspace': workspacesToken,
}
: undefined;
const { promise, requestCanceler } = this.client.getCancellable<FetchFolderContentResponse>(
`/folders/content/${folderUuid}?${query}`,
this.headers(customHeaders),
);
return [promise, requestCanceler];
}
/**
* Retrieves a file with the specified fileId along with the associated workspacesToken.
*
* @param {string} fileId - The ID of the file to retrieve.
* @param {string} [workspacesToken] - Token for accessing workspaces.
* @return {[Promise<FileMeta>, RequestCanceler]} A promise with FileMeta and a canceler for the request.
*/
public getFile(fileId: string, workspacesToken?: string): [Promise<FileMeta>, RequestCanceler] {
const customHeaders = workspacesToken
? {
'x-internxt-workspace': workspacesToken,
}
: undefined;
const { promise, requestCanceler } = this.client.getCancellable<FileMeta>(
`/files/${fileId}/meta`,
this.headers(customHeaders),
);
return [promise, requestCanceler];
}
/**
* Gets the files in a folder.
*
* @param {number} folderId - The ID of the folder.
* @param {number} [offset=0] - The position of the first file to return.
* @param {number} [limit=50] - The max number of files to be returned.
* @param {string} [sort=plainName] - The reference column to sort it.
* @param {string} [order=ASC] - The order to be followed.
* @returns {[Promise<FetchPaginatedFolderContentResponse>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getFolderFiles(
folderId: number,
offset = 0,
limit = 50,
sort = '',
order = '',
): [Promise<FetchPaginatedFolderContentResponse>, RequestCanceler] {
const offsetQuery = `?offset=${offset}`;
const limitQuery = `&limit=${limit}`;
const sortQuery = sort !== '' ? `&sort=${sort}` : '';
const orderQuery = order !== '' ? `&order=${order}` : '';
const query = `${offsetQuery}${limitQuery}${sortQuery}${orderQuery}`;
const { promise, requestCanceler } = this.client.getCancellable<FetchPaginatedFolderContentResponse>(
`folders/${folderId}/files/${query}`,
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Gets the files in a folder by its UUID.
*
* @param {UUID} folderUuid - The UUID of the folder.
* @param {number} [offset=0] - The position of the first file to return.
* @param {number} [limit=50] - The max number of files to be returned.
* @param {string} [sort=plainName] - The reference column to sort it.
* @param {string} [order=ASC] - The order to be followed.
* @returns {[Promise<FetchPaginatedFilesContent>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getFolderFilesByUuid(
folderUuid: UUID,
offset = 0,
limit = 50,
sort = '',
order = '',
): [Promise<FetchPaginatedFilesContent>, RequestCanceler] {
const offsetQuery = `?offset=${offset}`;
const limitQuery = `&limit=${limit}`;
const sortQuery = sort !== '' ? `&sort=${sort}` : '';
const orderQuery = order !== '' ? `&order=${order}` : '';
const query = `${offsetQuery}${limitQuery}${sortQuery}${orderQuery}`;
const { promise, requestCanceler } = this.client.getCancellable<FetchPaginatedFilesContent>(
`folders/content/${folderUuid}/files/${query}`,
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Gets the subfolders of a folder.
*
* @param {number} folderId - The ID of the folder.
* @param {number} [offset=0] - The position of the first subfolder to return.
* @param {number} [limit=50] - The max number of subfolders to return.
* @param {string} [sort=plainName] - The reference column to sort it.
* @param {string} [order=ASC] - The order to be followed.
* @returns {[Promise<FetchPaginatedFolderContentResponse>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getFolderFolders(
folderId: number,
offset = 0,
limit = 50,
sort = '',
order = '',
): [Promise<FetchPaginatedFolderContentResponse>, RequestCanceler] {
const offsetQuery = `?offset=${offset}`;
const limitQuery = `&limit=${limit}`;
const sortQuery = sort !== '' ? `&sort=${sort}` : '';
const orderQuery = order !== '' ? `&order=${order}` : '';
const query = `${offsetQuery}${limitQuery}${sortQuery}${orderQuery}`;
const { promise, requestCanceler } = this.client.getCancellable<FetchPaginatedFolderContentResponse>(
`folders/${folderId}/folders/${query}`,
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Gets the subfolders of a folder by its UUID.
*
* @param {UUID} folderUuid - The UUID of the folder.
* @param {number} [offset=0] - The position of the first subfolder to return.
* @param {number} [limit=50] - The max number of subfolders to return.
* @param {string} [sort=plainName] - The reference column to sort it.
* @param {string} [order=ASC] - The order to be followed.
* @returns {[Promise<FetchPaginatedFoldersContent>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getFolderFoldersByUuid(
folderUuid: UUID,
offset = 0,
limit = 50,
sort = '',
order = '',
): [Promise<FetchPaginatedFoldersContent>, RequestCanceler] {
const offsetQuery = `?offset=${offset}`;
const limitQuery = `&limit=${limit}`;
const sortQuery = sort !== '' ? `&sort=${sort}` : '';
const orderQuery = order !== '' ? `&order=${order}` : '';
const query = `${offsetQuery}${limitQuery}${sortQuery}${orderQuery}`;
const { promise, requestCanceler } = this.client.getCancellable<FetchPaginatedFoldersContent>(
`folders/content/${folderUuid}/folders/${query}`,
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Removes a specific folder from the centralized persistence
* @param folderId
*/
public deleteFolder(folderId: number): Promise<unknown> {
return this.client.delete(`/storage/folder/${folderId}`, this.headers());
}
/**
* Returns the total size of a folder
* @param folderId
*/
public getFolderSize(folderId: number): Promise<number> {
return this.client
.get<{
size: number;
}>(`/storage/folder/size/${folderId}`, this.headers())
.then((response) => {
return response.size;
});
}
/**
* Creates a new file entry
* @param fileEntry
*/
public createFileEntry(fileEntry: FileEntry, resourcesToken?: Token): Promise<DriveFileData> {
return this.client.post(
'/storage/file',
{
file: {
fileId: fileEntry.id,
type: fileEntry.type,
bucket: fileEntry.bucket,
size: fileEntry.size,
folder_id: fileEntry.folder_id,
name: fileEntry.name,
plain_name: fileEntry.plain_name,
encrypt_version: fileEntry.encrypt_version,
},
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Creates a new file entry
* @param fileEntry
*/
public createFileEntryByUuid(fileEntry: FileEntryByUuid, resourcesToken?: string): Promise<DriveFileData> {
return this.client.post(
'/files',
{
name: fileEntry.name,
bucket: fileEntry.bucket,
fileId: fileEntry.id,
encryptVersion: fileEntry.encrypt_version,
folderUuid: fileEntry.folder_id,
size: fileEntry.size,
plainName: fileEntry.plain_name,
type: fileEntry.type,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Creates a new thumbnail entry
* @param thumbnailEntry
*/
public createThumbnailEntry(thumbnailEntry: ThumbnailEntry, resourcesToken?: Token): Promise<Thumbnail> {
return this.client.post(
'/storage/thumbnail',
{
thumbnail: thumbnailEntry,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Creates a new thumbnail entry using drive-server-wip
* @param CreateThumbnailEntryPayload
*/
public createThumbnailEntryWithUUID(
thumbnailEntry: CreateThumbnailEntryPayload,
resourcesToken?: string,
): Promise<Thumbnail> {
return this.client.post(
'/files/thumbnail',
{
...thumbnailEntry,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Updates the details of a file entry
* @param payload
*/
public updateFile(payload: UpdateFilePayload, resourcesToken?: Token): Promise<void> {
return this.client.post(
`/storage/file/${payload.fileId}/meta`,
{
metadata: payload.metadata,
bucketId: payload.bucketId,
relativePath: payload.destinationPath,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Updates the name of a file with the given UUID.
*
* @param {Object} payload - The payload containing the UUID and new name of the file.
* @param {string} payload.fileUuid - The UUID of the file.
* @param {string} payload.name - The new name of the file.
* @param {string} [resourcesToken] - The token for accessing resources.
* @return {Promise<void>} - A Promise that resolves when the file name is successfully updated.
*/
public updateFileNameWithUUID(payload: { fileUuid: string; name: string }, resourcesToken?: Token): Promise<void> {
const { fileUuid, name } = payload;
return this.client.put(
`/files/${fileUuid}/meta`,
{
plainName: name,
},
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Updates the name and the type from a given file UUID.
*
* @param {Object} payload - The payload containing the UUID and the new properties of the file.
* @param {string} payload.fileUuid - The UUID of the file.
* @param {string} payload.name - The new name of the file.
* @param {string} payload.type - The new type of the file.
* @param {string} [resourcesToken] - The token for accessing resources.
* @return {Promise<void>} - A Promise that resolves when the file name is successfully updated.
*/
public updateFileMetaByUUID(
fileUuid: string,
payload: { plainName?: string; type?: string | null },
resourcesToken?: Token,
): Promise<void> {
return this.client.put(
`/files/${fileUuid}/meta`,
payload,
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Deletes a specific file entry
* @param payload
*/
public deleteFile(payload: DeleteFilePayload): Promise<unknown> {
return this.client.delete(`/storage/folder/${payload.folderId}/file/${payload.fileId}`, this.headers());
}
/**
* Updates the persisted path of a file entry
* @param payload
*/
public moveFile(payload: MoveFilePayload): Promise<MoveFileResponse> {
return this.client.post(
'/storage/move/file',
{
fileId: payload.fileId,
destination: payload.destination,
relativePath: payload.destinationPath,
bucketId: payload.bucketId,
},
this.headers(),
);
}
/**
* Moves a specific file to a new location
* @param payload
*/
public async moveFileByUuid(payload: MoveFileUuidPayload): Promise<FileMeta> {
return this.client.patch(
`/files/${payload.fileUuid}`,
{
destinationFolder: payload.destinationFolderUuid,
},
this.headers(),
);
}
/**
* Returns a list of the n most recent files
* @param limit
* @deprecated use `getRecentFilesV2` call instead.
*/
public getRecentFiles(limit: number): Promise<DriveFileData[]> {
return this.client.get(`/storage/recents?limit=${limit}`, this.headers());
}
/**
* Returns a list of the n most recent files
* @param limit
*/
public async getRecentFilesV2(limit: number): Promise<DriveFileData[]> {
return this.client.get<DriveFileData[]>(`/files/recents?limit=${limit}`, this.headers());
}
/**
* Returns a list of items in trash
*/
public getTrash(): [Promise<FetchFolderContentResponse>, RequestCanceler] {
const { promise, requestCanceler } = this.client.getCancellable<FetchFolderContentResponse>(
'/storage/trash',
this.headers(),
);
return [promise, requestCanceler];
}
/**
* Add Items to Trash
* @param payload
*/
public addItemsToTrash(payload: AddItemsToTrashPayload): Promise<void> {
return this.client.post(
'/storage/trash/add',
{
items: payload.items,
},
this.headers(),
);
}
/**
* Returns a list of the n most recent files
* @param limit
*/
public searchItemsByName(plain_name: string): Promise<DriveFileData[]> {
return this.client.post('/users/search', { plain_name }, this.headers());
}
/**
* Returns the current space usage of the user
* @deprecated use `spaceUsageV2` call instead.
*/
public spaceUsage(): Promise<UsageResponse> {
return this.client.get('/usage', this.headers());
}
/**
* Returns the current space usage of the user
*/
public spaceUsageV2(): Promise<UsageResponseV2> {
return this.client.get('/users/usage', this.headers());
}
/**
* Returns the current space limit for the user
* @deprecated use `spaceLimitV2` call instead.
*/
public spaceLimit(): Promise<FetchLimitResponse> {
return this.client.get('/limit', this.headers());
}
/**
* Returns the current space limit for the user
*/
public spaceLimitV2(): Promise<FetchLimitResponse> {
return this.client.get('/users/limit', this.headers());
}
/**
* Get global search items.
*
* @param {string} search - The name of the item.
* @param {string} workspaceId - The ID of the workspace (optional).
* @returns {[Promise<SearchResultData>, RequestCanceler]} An array containing a promise to get the API response and a function to cancel the request.
*/
public getGlobalSearchItems(search: string, workspaceId?: string): [Promise<SearchResultData>, RequestCanceler] {
const { promise, requestCanceler } = workspaceId
? this.client.getCancellable<SearchResultData>(`workspaces/${workspaceId}/fuzzy/${search}`, this.headers())
: this.client.getCancellable<SearchResultData>(`fuzzy/${search}`, this.headers());
return [promise, requestCanceler];
}
/**
* Returns the needed headers for the module requests
* @private
*/
private headers(customHeaders?: CustomHeaders) {
return headersWithToken(
this.appDetails.clientName,
this.appDetails.clientVersion,
this.apiSecurity.token,
this.apiSecurity?.workspaceToken,
customHeaders,
);
}
/**
* Gets the ancestors of a given folder UUID
*
* @param {string} uuid - UUID of the folder.
* @returns {Promise<FolderAncestor[]>} A promise that resolves with an array of ancestors of the given folder.
*/
public getFolderAncestors(uuid: string): Promise<FolderAncestor[]> {
return this.client.get<FolderAncestor[]>(`folders/${uuid}/ancestors`, this.headers());
}
/**
* Gets the ancestors of an item with the given UUID and type in a Workspace
*
* @param {string} workspaceId - UUID of the workspace.
* @param {string} itemType - itemType to know if the item is file or folder
* @param {string} uuid - UUID of the item.
* @returns {Promise<FolderAncestor[]>} A promise that resolves with an array of ancestors of the given folder.
*/
public getFolderAncestorsInWorkspace(
workspaceId: string,
itemType: ItemType,
uuid: string,
resourcesToken?: Token,
): Promise<FolderAncestorWorkspace[]> {
return this.client.get<FolderAncestorWorkspace[]>(
`workspaces/${workspaceId}/${itemType}/${uuid}/ancestors`,
addResourcesTokenToHeaders(this.headers(), resourcesToken),
);
}
/**
* Gets the meta of a given folder UUID
*
* @param {string} folderUUID - UUID of the folder.
* @returns {Promise<FolderMeta>}
*/
public getFolderMeta(uuid: string, workspacesToken?: string, resourcesToken?: string): Promise<FolderMeta> {
const customHeaders = workspacesToken
? {
'x-internxt-workspace': workspacesToken,
}
: undefined;
return this.client.get<FolderMeta>(
`folders/${uuid}/meta`,
addResourcesTokenToHeaders(this.headers(customHeaders), resourcesToken),
);
}
/**
* Gets the meta of a given folder Id
*
* @param {number} folderId - Id of the folder.
* @returns {Promise<FolderMeta>}
*/
public getFolderMetaById(folderId: number): Promise<FolderMeta> {
return this.client.get<FolderMeta>(`folders/${folderId}/metadata`, this.headers());
}
/**
* Replaces a file with a new one.
*
* @param {string} uuid - UUID of the file.
* @param {ReplaceFile} payload
* @returns {Promise<DriveFileData>} - The replaced file data.
*/
public replaceFile(uuid: string, payload: ReplaceFile): Promise<DriveFileData> {
return this.client.put<DriveFileData>(`/files/${uuid}`, { ...payload }, this.headers());
}
/**
* Checks the size limit for a file.
*
* @param {number} size - The size of the file to check.
* @return {Promise<void>} - A promise that resolves when the size limit check is complete.
*/
public async checkSizeLimit(size: number): Promise<void> {
return this.client.post(
'/files/check-size-limit',
{
file: {
size,
},
},
this.headers(),
);
}
/**
* Retrieves the folder tree based on the UUID.
*
* @param {string} uuid - The UUID of the folder.
* @return {Promise<FolderTreeResponse>} The promise containing the folder tree response.
*/
public getFolderTree(uuid: string): Promise<FolderTreeResponse> {
return this.client.get(`/folders/${uuid}/tree`, this.headers());
}
/**
* Checks if the given files already exist in the given folder.
*
* @param {CheckDuplicatedFilesPayload} payload - Payload containing the folder UUID and the list of files to check.
* @return {Promise<CheckDuplicatedFilesResponse>} - Promise that contains the duplicated files in a list.
*/
public checkDuplicatedFiles({
folderUuid,
filesList,
}: CheckDuplicatedFilesPayload): Promise<CheckDuplicatedFilesResponse> {
return this.client.post(
`/folders/content/${folderUuid}/files/existence`,
{
files: filesList,
},
this.headers(),
);
}
/**
* Checks if the given folders names already exist in the given folder
*
* @param {CheckDuplicatedFolderPayload} payload - Payload containing the folder UUID and the list of folders to check.
* @return {Promise<CheckDuplicatedFoldersResponse>} - Promise that contains the duplicated folders in a list.
*/
public checkDuplicatedFolders({
folderUuid,
folderNamesList,
}: CheckDuplicatedFolderPayload): Promise<CheckDuplicatedFoldersResponse> {
return this.client.post(
`/folders/content/${folderUuid}/folders/existence`,
{
plainNames: folderNamesList,
},
this.headers(),
);
}
/**
* Gets the folder meta from a given path (e.g. "/folder1/folder2")
*
* @param {string} folderPath - The path of the folder.
* @returns {Promise<FolderMeta>} A promise that resolves the folder on that path.
*/
public getFolderByPath(folderPath: string): Promise<FolderMeta> {
return this.client.get<FolderMeta>(`folders/meta?path=${folderPath}`, this.headers());
}
/**
* Gets the file meta from a given path (e.g. "/folder1/folder2/file.png")
*
* @param {string} filePath - The path of the file.
* @returns {Promise<FileMeta>} A promise that resolves the file on that path.
*/
public getFileByPath(filePath: string): Promise<FileMeta> {
return this.client.get<FileMeta>(`files/meta?path=${filePath}`, this.headers());
}
}