forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldp-container.js
More file actions
167 lines (146 loc) · 4.46 KB
/
Copy pathldp-container.js
File metadata and controls
167 lines (146 loc) · 4.46 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
module.exports.addContainerStats = addContainerStats
module.exports.addFile = addFile
module.exports.addStats = addStats
module.exports.getMetadataGraph = getMetadataGraph
module.exports.readdir = readdir
var $rdf = require('rdflib')
var debug = require('./debug')
var error = require('./http-error')
var fs = require('fs')
var ns = require('solid-namespace')($rdf)
var S = require('string')
var turtleExtension = '.ttl'
function addContainerStats (ldp, filename, resourceGraph, next) {
ldp.stat(filename, function (err, containerStats) {
if (!err) {
addStats(resourceGraph, '', containerStats)
resourceGraph.add(
resourceGraph.sym(''),
ns.rdf('type'),
ns.ldp('BasicContainer'))
resourceGraph.add(
resourceGraph.sym(''),
ns.rdf('type'),
ns.ldp('Container'))
}
next()
})
}
function addFile (ldp, resourceGraph, baseUri, uri, container, file, callback) {
// Skip .meta and .acl
if (S(file).endsWith(ldp.suffixMeta) || S(file).endsWith(ldp.suffixAcl)) {
return callback(null)
}
// Get file stats
ldp.stat(container + file, function (err, stats) {
if (err) {
// File does not exist, skip
return callback(null)
}
var fileSubject = file + (stats.isDirectory() ? '/' : '')
// var fileBaseUri = utils.filenameToBaseUri(fileSubject, uri, root)
// Add fileStats to resource Graph
addStats(resourceGraph, fileSubject, stats)
// Add to `contains` list
resourceGraph.add(
resourceGraph.sym(''),
ns.ldp('contains'),
resourceGraph.sym(fileSubject))
// Set up a metaFile path
var metaFile = container + file +
(stats.isDirectory() ? '/' : '') +
(S(file).endsWith(turtleExtension) ? '' : ldp.suffixMeta)
getMetadataGraph(ldp, metaFile, baseUri, function (err, metadataGraph) {
if (err) {
metadataGraph = $rdf.graph()
}
// Add Container or BasicContainer types
if (stats.isDirectory()) {
resourceGraph.add(
metadataGraph.sym(fileSubject),
ns.rdf('type'),
ns.ldp('BasicContainer'))
resourceGraph.add(
metadataGraph.sym(fileSubject),
ns.rdf('type'),
ns.ldp('Container'))
}
// Add generic LDP type
resourceGraph.add(
metadataGraph.sym(fileSubject),
ns.rdf('type'),
ns.ldp('Resource'))
// Add type from metadataGraph
metadataGraph
.statementsMatching(
metadataGraph.sym(baseUri),
ns.rdf('type'),
undefined)
.forEach(function (typeStatement) {
// If the current is a file and its type is BasicContainer,
// This is not possible, so do not infer its type!
if (
(
typeStatement.object.uri !== ns.ldp('BasicContainer').uri &&
typeStatement.object.uri !== ns.ldp('Container').uri
) ||
!stats.isFile()
) {
resourceGraph.add(
resourceGraph.sym(fileSubject),
typeStatement.predicate,
typeStatement.object)
}
})
return callback(null)
})
})
}
function addStats (resourceGraph, baseUri, stats) {
resourceGraph.add(
resourceGraph.sym(baseUri),
ns.stat('mtime'),
stats.mtime.getTime() / 1000)
resourceGraph.add(
resourceGraph.sym(baseUri),
ns.stat('size'),
stats.size)
}
function readdir (filename, callback) {
debug.handlers('GET -- Reading directory')
fs.readdir(filename, function (err, files) {
if (err) {
debug.handlers('GET -- Error reading files: ' + err)
return callback(error(err, 'Can\'t read container'))
}
debug.handlers('Files in directory: ' + files)
return callback(null, files)
})
}
function getMetadataGraph (ldp, metaFile, fileBaseUri, callback) {
ldp.stat(metaFile, function (err, metaStats) {
if (err) {
return callback(err)
}
if (metaStats && metaStats.isFile()) {
ldp.readFile(metaFile, function (err, rawMetadata) {
if (err) {
return callback(err)
}
var metadataGraph = $rdf.graph()
try {
$rdf.parse(
rawMetadata,
metadataGraph,
fileBaseUri,
'text/turtle')
} catch (dirErr) {
return callback(error(err, 'Can\'t parse container metadata'))
}
return callback(null, metadataGraph)
})
} else {
return callback(null, $rdf.graph())
}
})
}