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
/
utils.ts
141 lines (131 loc) · 3.32 KB
/
utils.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
/* istanbul ignore file */
import deepCopy from 'ts-deepcopy';
import { rdflib } from './rdf-manager';
import { ResourceConfig, SolidResourceType } from './storage-manager';
function guessFileType(url: string): string {
const ext = url.replace(/.*\./, '');
if (ext.match(/\/$/)) {
return 'dir';
}
if (ext.match(/(md|markdown)/)) {
return 'text/markdown';
}
if (ext.match(/html/)) {
return 'text/html';
}
if (ext.match(/xml/)) {
return 'text/xml';
}
if (ext.match(/ttl/)) {
return 'text/turtle';
}
if (ext.match(/n3/)) {
return 'text/n3';
}
if (ext.match(/rq/)) {
return 'application/sparql';
}
if (ext.match(/css/)) {
return 'text/css';
}
if (ext.match(/txt/)) {
return 'text/plain';
}
if (ext.match(/json/)) {
return 'application/json';
}
if (ext.match(/js/)) {
return 'application/javascript';
}
if (ext.match(/(png|gif|jpeg|tif)/)) {
return 'image';
}
if (ext.match(/(mp3|aif|ogg)/)) {
return 'audio';
}
if (ext.match(/(avi|mp4|mpeg)/)) {
return 'video';
}
/* default */
return 'text/turtle';
}
function textToGraph(
text: string,
baseUrl: string,
contentType: string = ''
): Promise<rdflib.IndexedFormula> {
const textContentType = contentType || guessFileType(baseUrl);
const graph = rdflib.graph();
// eslint-disable-next-line no-unused-vars
return new Promise(resolve => {
rdflib.parse(text, graph, baseUrl, textContentType);
resolve(graph);
});
}
async function isFolder(
graph: rdflib.IndexedFormula,
baseUrl: string
): Promise<boolean> {
const folderNode = rdflib.sym(baseUrl);
const isAnInstanceOfClass = rdflib.sym(
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
);
const types = graph.each(
folderNode,
isAnInstanceOfClass,
undefined,
undefined
);
return Object.values(types).some(
({ value }) => value.match('ldp#BasicContainer') !== null
);
}
async function extractFolderItems(
graph: rdflib.IndexedFormula,
subj: ResourceConfig
): Promise<{
readonly files: ResourceConfig[];
readonly folders: ResourceConfig[];
}> {
const files: ResourceConfig[] = [];
const folders: ResourceConfig[] = [];
graph
.each(
rdflib.sym(subj.fullPathWithAppendix()),
rdflib.sym('http://www.w3.org/ns/ldp#contains'),
undefined,
undefined
)
.forEach(async (item: any) => {
const url = item.value;
let processedUrl = item.value;
if (url.substr(-1) === '/') {
processedUrl = url.substr(0, url.length - 1);
}
const titleIndex = processedUrl.lastIndexOf('/');
const resourceTitle = processedUrl.substr(titleIndex + 1);
const resourcePartialPath = processedUrl
.substr(0, titleIndex + 1)
.replace(/\/$/, '');
const resourceIsFolder = await isFolder(graph, url);
if (resourceIsFolder) {
folders.push(
new ResourceConfig({
title: resourceTitle,
path: resourcePartialPath,
type: SolidResourceType.Folder
})
);
} else {
files.push(
new ResourceConfig({
title: resourceTitle,
path: resourcePartialPath,
type: SolidResourceType.File
})
);
}
});
return { files, folders };
}
export { textToGraph, extractFolderItems, deepCopy };