-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathutils.js
More file actions
159 lines (141 loc) · 4.06 KB
/
utils.js
File metadata and controls
159 lines (141 loc) · 4.06 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
exports.uriToFilename = uriToFilename
exports.uriToRelativeFilename = uriToRelativeFilename
exports.filenameToBaseUri = filenameToBaseUri
exports.uriAbs = uriAbs
exports.pathBasename = pathBasename
exports.uriBase = uriBase
exports.hasSuffix = hasSuffix
exports.getResourceLink = getResourceLink
exports.parse = parse
exports.serialize = serialize
exports.translate = translate
exports.stringToStream = stringToStream
exports.reqToPath = reqToPath
var fs = require('fs')
var path = require('path')
var S = require('string')
var $rdf = require('rdflib')
var from = require('from2')
function uriToFilename (uri, base) {
uri = decodeURIComponent(uri)
var filename = path.join(base, uri)
// Make sure filename ends with '/' if filename exists and is a directory.
// TODO this sync operation can be avoided and can be left
// to do, to other components, see `ldp.get`
try {
var fileStats = fs.statSync(filename)
if (fileStats.isDirectory() && !S(filename).endsWith('/')) {
filename += '/'
} else if (fileStats.isFile() && S(filename).endsWith('/')) {
filename = S(filename).chompRight('/').s
}
} catch (err) {}
return filename
}
function uriToRelativeFilename (uri, base) {
var filename = uriToFilename(uri, base)
var relative = path.relative(base, filename)
return relative
}
function filenameToBaseUri (filename, uri, base) {
var uriPath = S(filename).strip(base).toString()
return uri + '/' + uriPath
}
function uriAbs (req) {
return req.protocol + '://' + req.get('host')
}
function uriBase (req) {
return uriAbs(req) + (req.baseUrl || '')
}
function pathBasename (fullpath) {
var bname = ''
if (fullpath) {
bname = (fullpath.lastIndexOf('/') === fullpath.length - 1) ? '' : path.basename(fullpath)
}
return bname
}
function hasSuffix (path, suffixes) {
for (var i in suffixes) {
if (path.indexOf(suffixes[i], path.length - suffixes[i].length) !== -1) {
return true
}
}
return false
}
function getResourceLink (filename, uri, base, suffix, otherSuffix) {
var link = filenameToBaseUri(filename, uri, base)
if (S(link).endsWith(suffix)) {
return link
} else if (S(link).endsWith(otherSuffix)) {
return S(link).chompRight(otherSuffix).s + suffix
} else {
return link + suffix
}
}
function parse (data, baseUri, contentType, callback) {
var graph = $rdf.graph()
try {
$rdf.parse(data, graph, baseUri, contentType)
} catch (err) {
return callback(err)
}
return callback(null, graph)
}
function serialize (graph, baseUri, contentType, callback) {
try {
$rdf.serialize(null, graph, null, contentType, function (err, result) {
if (err) {
return callback(err)
}
if (result === undefined) {
return callback(new Error('Error serializing the graph to ' + contentType))
}
return callback(null, result)
})
} catch (err) {
callback(err)
}
}
function translate (stream, baseUri, from, to, callback) {
// Handle Turtle Accept header
if (to === 'text/turtle' ||
to === 'text/n3' ||
to === 'application/turtle' ||
to === 'application/n3') {
to = 'text/turtle'
}
var data = ''
stream
.on('data', function (chunk) {
data += chunk
})
.on('end', function () {
parse(data, baseUri, from, function (err, graph) {
if (err) return callback(err)
serialize(graph, baseUri, to, function (err, data) {
if (err) return callback(err)
callback(null, data)
})
})
})
}
function stringToStream (string) {
return from(function (size, next) {
// if there's no more content
// left in the string, close the stream.
if (!string || string.length <= 0) {
return next(null, null)
}
// Pull in a new chunk of text,
// removing it from the string.
var chunk = string.slice(0, size)
string = string.slice(size)
// Emit "chunk" from the stream.
next(null, chunk)
})
}
function reqToPath (req) {
var ldp = req.app.locals.ldp
var root = ldp.idp ? ldp.root + req.hostname + '/' : ldp.root
return uriToFilename(req.path, root)
}