This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage-manager.ts
557 lines (514 loc) · 15.1 KB
/
storage-manager.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
import {
StorageAuthenticationManager,
StorageTestAuthenticationManager
} from './auth-manager';
import { ENVIRONMENT } from './constants';
import { AccessControlList } from './helpers';
import { rdflib as $rdf } from './rdf-manager';
// import { StorageRdfManager } from './rdf-manager';
import * as Utils from './utils';
const authClient =
ENVIRONMENT === 'TEST'
? StorageTestAuthenticationManager
: StorageAuthenticationManager;
const RDF = $rdf.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
const FOAF = $rdf.Namespace('http://xmlns.com/foaf/0.1/');
/**
* Enum representing different solid resource types
*/
enum SolidResourceType {
Folder = '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"',
File = '<http://www.w3.org/ns/ldp#Resource>; rel="type"'
}
/**
* Interface representing the SolidResource
*/
interface SolidResource {
readonly type: SolidResourceType;
readonly path: string;
readonly title: string;
readonly contentType?: string;
readonly body?: string;
readonly isPublic?: boolean;
}
/**
* Class representing the solid resoruce configuration description
*/
class ResourceConfig {
public readonly webID?: string;
public readonly resource: SolidResource;
constructor(resource: SolidResource, webID?: string) {
this.webID = webID;
this.resource = resource;
}
/**
* Assembles the full path to resource based on path and title
*/
public fullPath(): string {
return this.resource.path + '/' + this.resource.title;
}
/**
* Assembles the full path and appends the appendix if resource is a folder
*/
public fullPathWithAppendix(): string {
const folderAppendix =
this.resource.type === SolidResourceType.Folder ? '/' : '';
return this.resource.path + '/' + this.resource.title + folderAppendix;
}
}
/**
* Interface representing Privileges of the resource
*/
interface Privileges {
agents: null | string[];
modes: string[];
}
/**
* Subclass of ResourceConfig for ACL files
*/
class AccessControlConfig extends ResourceConfig {
public readonly privileges: Privileges[];
constructor(
resource: SolidResource,
privileges: Privileges[],
webID?: string
) {
super(resource, webID);
this.privileges = privileges;
}
/**
* Assembles full path to acl file
*/
public fullACLPath(): string {
const aclAppendix =
this.resource.type === SolidResourceType.Folder ? '/' : '';
return (
this.resource.path + '/' + this.resource.title + aclAppendix + '.acl'
);
}
/**
* Assembles full title acl resource
*/
public fullACLTitle(): string {
const aclAppendix =
this.resource.type === SolidResourceType.Folder ? '/' : '';
return this.resource.title + aclAppendix + '.acl';
}
}
// LEGACY CODE for v4 NSS editions, was replaced by implementation from
// inrupt solid react components package for v5 NSS releases.
// ======================================================
//
// class AccessControlStatementConfig extends AccessControlConfig {
// public readonly ownerNode: $rdf.NamedNode;
// public readonly userNode?: $rdf.NamedNode;
// public readonly resourceNode: $rdf.NamedNode;
// public readonly aclResourceNode: $rdf.NamedNode;
// constructor(
// resource: SolidResource,
// privileges: Privileges[],
// ownerNode: $rdf.NamedNode,
// resourceNode: $rdf.NamedNode,
// aclResourceNode: $rdf.NamedNode,
// userNode?: $rdf.NamedNode,
// webID?: string
// ) {
// super(resource, privileges, webID);
// this.ownerNode = ownerNode;
// this.resourceNode = resourceNode;
// this.aclResourceNode = aclResourceNode;
// this.userNode = userNode;
// }
// }
// class AccessControlNamespace {
// public static readonly Control = ACL('Control');
// public static readonly Read = ACL('Read');
// public static readonly Write = ACL('Write');
// public static readonly Append = ACL('Append');
// public static readonly Authorization = ACL('Authorization');
// public static readonly accessTo = ACL('accessTo');
// public static readonly agent = ACL('agent');
// public static readonly agentClass = ACL('agentClass');
// public static readonly mode = ACL('mode');
// public static readonly defaultForNew = ACL('default');
// }
// END OF LEGACY CODE
// ======================================================
//
/**
* FOAF namespace helper class
*/
class FOAFNamespace {
public static readonly Agent = FOAF('Agent');
}
/**
* RDF namespace helper class
*/
class RDFNamespace {
public static readonly type = RDF('type');
}
/**
* StorageFileManager class responsible for CRUD opearations and ACL files
*/
class StorageFileManager {
/**
* Updated or creates the acl file based on provided configuration
* @param accessControlConfig configuration desribing the acl to be updated
*/
public static async updateACL(
accessControlConfig: AccessControlConfig
): Promise<any> {
try {
const documentURI = accessControlConfig.fullPathWithAppendix();
const { MODES } = AccessControlList;
const permissions = [
...accessControlConfig.privileges,
{
modes: [MODES.READ, MODES.WRITE, MODES.CONTROL],
agents: [accessControlConfig.webID]
}
];
if (accessControlConfig.resource.isPublic) {
permissions.push({ modes: [MODES.READ], agents: undefined });
}
const aclInstance = new AccessControlList(
accessControlConfig.webID,
documentURI
);
return await aclInstance.createACL(permissions);
} catch (e) {
throw e;
}
}
/**
* Creates solid resource based on configuration
* @param resourceConfig Configuration of resource to be created
*/
public static async createResource(
resourceConfig: ResourceConfig
): Promise<any> {
try {
const options = {
body: resourceConfig.resource.body,
headers: {
Link: resourceConfig.resource.type,
Slug: resourceConfig.resource.title,
'Content-Type': resourceConfig.resource.contentType || 'text/turtle'
},
method: 'POST'
};
return await authClient.fetch(resourceConfig.resource.path, options);
} catch (e) {
throw e;
}
}
/**
* Deletes folder contents
* @param resourceConfig Configuration of resource to be created
*/
public static async deleteFolderContents(
resourceConfig: ResourceConfig
): Promise<any> {
try {
const { files, folders } = await StorageFileManager.getFolder(
resourceConfig
);
/* tslint:disable */
for (const file of files) {
await StorageFileManager.deleteResource(file);
}
for (const folder of folders) {
const response = await authClient.fetch(folder.fullPath(), {
method: 'DELETE'
});
if (response.status === 409 || response.status === 301) {
// Solid pod returns 409 if the item is a folder and is not empty
// Solid pod returns 301 if is attempted to read a folder url without
// '/' at the end (from buildFileUrl)
await StorageFileManager.deleteFolderRecursively(folder);
}
/* tslint:enable */
}
return Promise.resolve({ status: 200 });
} catch (e) {
throw e;
}
}
/**
* Generic resource deletion method
* @param resourceConfig Configuration of resource to be deleted
*/
public static async deleteResource(
resourceConfig: ResourceConfig
): Promise<any> {
try {
if (resourceConfig.resource.type === SolidResourceType.File) {
return authClient.fetch(resourceConfig.fullPath(), {
method: 'DELETE'
});
} else {
const response = await authClient.fetch(resourceConfig.fullPath(), {
method: 'DELETE'
});
if (response.status === 409 || response.status === 301) {
// Solid pod returns 409 if the item is a folder and is not empty
// Solid pod returns 301 if is attempted to read a folder url without
// '/' at the end (from buildFileUrl)
return StorageFileManager.deleteFolderRecursively(resourceConfig);
} else {
return response;
}
}
} catch (e) {
throw e;
}
}
/**
* Method to load the resource at specific path
* @param path Path to resource
* @param parameters Additional parameters for fetch request
*/
public static async getResource(
path: string,
parameters?: object
): Promise<any> {
try {
const response = await authClient.fetch(path, {
method: 'GET',
...parameters
});
return await response.text();
} catch (e) {
throw e;
}
}
/**
* Copies resource from one path to another
* @param originResource Configuration of original resource
* @param destinationResource Configuration of resource to be copied into
*/
public static async copyFile(
originResource: ResourceConfig,
destinationResource: ResourceConfig
): Promise<any> {
try {
const originPath = originResource.fullPath();
const destinationPath = destinationResource.fullPath();
const response = await authClient.fetch(originPath, {
method: 'GET'
});
const content = await response.text();
// tslint:disable
const contentType = response.headers['contentType'];
// tslint:enable
await authClient
.fetch(destinationPath, {
method: 'PUT',
headers: {
'Content-Type': contentType
},
body: content
})
.then(
res => {
return Promise.resolve({ status: 201, response: res });
},
e => {
throw e;
}
);
} catch (e) {
throw e;
}
}
/**
* Copies individual resource into a folder
* @param originResource Configuration of original path
* @param folderDestinationResource Configuration of destination folder
*/
public static async copyFileToFolder(
originResource: ResourceConfig,
folderDestinationResource: ResourceConfig
): Promise<any> {
try {
const destinationResource = new ResourceConfig(
{
path: folderDestinationResource.fullPath(),
title: originResource.resource.title,
type: SolidResourceType.File
},
folderDestinationResource.webID
);
return StorageFileManager.copyFile(originResource, destinationResource);
} catch (e) {
throw e;
}
}
/**
* Copies folder from original to destination configuration
* @param originConfig Original configuration
* @param destinationConfig Destination configuration
*/
public static async copyFolder(
originConfig: ResourceConfig,
destinationConfig: ResourceConfig
): Promise<any> {
try {
const { files, folders } = await StorageFileManager.getFolder(
originConfig
);
/* tslint:disable */
for (const file of files) {
await StorageFileManager.copyFileToFolder(file, destinationConfig);
}
for (const folder of folders) {
const destinationFolderConfig = new ResourceConfig(
{
path: destinationConfig.fullPath(),
title: folder.resource.title,
type: SolidResourceType.Folder
},
destinationConfig.webID
);
await StorageFileManager.createResource(destinationFolderConfig);
await StorageFileManager.copyFolder(folder, destinationFolderConfig);
/* tslint:enable */
}
return Promise.resolve({ status: 200 });
} catch (e) {
throw e;
}
}
/**
* Generic copy resource method
* @param resourceConfig Original resource
* @param destinationConfig Destination resource
*/
public static async copyResource(
resourceConfig: ResourceConfig,
destinationConfig: ResourceConfig
): Promise<any> {
try {
if (resourceConfig.resource.type === SolidResourceType.File) {
return StorageFileManager.copyFile(resourceConfig, destinationConfig);
} else {
await StorageFileManager.createResource(destinationConfig);
return StorageFileManager.copyFolder(resourceConfig, destinationConfig);
}
} catch (e) {
throw e;
}
}
/**
* Updates resource based on configuration
* @param oldResourceConfig Old resource
* @param newResourceConfig New resource
*/
public static async renameResource(
oldResourceConfig: ResourceConfig,
newResourceConfig: ResourceConfig
): Promise<any> {
try {
if (oldResourceConfig.fullPath() !== newResourceConfig.fullPath()) {
await StorageFileManager.copyResource(
oldResourceConfig,
newResourceConfig
);
return StorageFileManager.deleteResource(oldResourceConfig);
} else {
return Promise.resolve({ status: 200 });
}
} catch (e) {
throw e;
}
}
/**
* Updates resource
* @param resourceConfig Original resource config
*/
public static async updateResource(
resourceConfig: ResourceConfig
): Promise<any> {
try {
return await authClient.fetch(resourceConfig.fullPath(), {
method: 'PUT',
headers: {
'Content-Type': resourceConfig.resource.contentType || 'text/turtle'
},
body: resourceConfig.resource.body
});
} catch (e) {
throw e;
}
}
/**
* Creates or updates the resource configuration
* @param resourceConfig Resource configuration
*/
public static async createOrUpdateResource(
resourceConfig: ResourceConfig
): Promise<any> {
try {
const result = await StorageFileManager.resourceExists(
resourceConfig.fullPath()
);
return result.status === 404
? StorageFileManager.createResource(resourceConfig)
: StorageFileManager.updateResource(resourceConfig);
} catch (e) {
throw e;
}
}
/**
* Checks if resource exist at particular path
* @param resourceURL Original resource url
*/
public static async resourceExists(resourceURL: string): Promise<any> {
return authClient.fetch(resourceURL, {
headers: {
'Content-Type': 'text/turtle'
}
});
}
/**
* Returns the folder and its content
* @param folderConfig Configuration of the folder
*/
public static async getFolder(
folderConfig: ResourceConfig
): Promise<{
readonly files: ResourceConfig[];
readonly folders: ResourceConfig[];
}> {
const response = await StorageFileManager.getResource(
folderConfig.fullPath(),
{
headers: { Accept: 'text/turtle' }
}
);
const folderRDF = response;
const graph = await Utils.textToGraph(
folderRDF,
folderConfig.fullPathWithAppendix(),
'text/turtle'
);
const folderItems = Utils.extractFolderItems(graph, folderConfig);
return folderItems;
}
/**
* Generic method triggering recursive folder content deletion
*/
public static readonly deleteFolderRecursively = async resourceConfig => {
await StorageFileManager.deleteFolderContents(resourceConfig);
return StorageFileManager.deleteResource(resourceConfig);
};
}
export {
SolidResourceType,
SolidResource,
AccessControlConfig,
ResourceConfig,
FOAFNamespace,
RDFNamespace,
StorageFileManager
};