Skip to content

Commit 661ceda

Browse files
committed
Expose getPublicUrl.
1 parent b6cb614 commit 661ceda

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

lib/handlers/get.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,14 @@ async function globHandler (req, res, next) {
136136
// TODO: This is a hack, that does not check if the target file exists, as this is quite complex with globbing.
137137
// TODO: Proper support for this is not implemented, as globbing support might be removed in the future.
138138
const filename = ldp.resourceMapper.getFilePath(req)
139-
const requestUri = (await ldp.resourceMapper.mapFileToUrl({ path: filename, hostname: req.hostname })).url
140139

141140
const globOptions = {
142141
noext: true,
143142
nobrace: true,
144143
nodir: true
145144
}
146145

147-
glob(filename, globOptions, function (err, matches) {
146+
glob(filename, globOptions, async (err, matches) => {
148147
if (err || matches.length === 0) {
149148
debugGlob('No files matching the pattern')
150149
return next(error(404, 'No files matching glob pattern'))
@@ -154,7 +153,7 @@ async function globHandler (req, res, next) {
154153
const globGraph = $rdf.graph()
155154

156155
debugGlob('found matches ' + matches)
157-
Promise.all(matches.map(match => new Promise(async (resolve, reject) => {
156+
await Promise.all(matches.map(match => new Promise(async (resolve, reject) => {
158157
const urlData = await ldp.resourceMapper.mapFileToUrl({ path: match, hostname: req.hostname })
159158
fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) {
160159
if (err) {
@@ -178,15 +177,15 @@ async function globHandler (req, res, next) {
178177
})
179178
})
180179
})))
181-
.then(() => {
182-
const data = $rdf.serialize(undefined, globGraph, requestUri, 'text/turtle')
183-
// TODO this should be added as a middleware in the routes
184-
res.setHeader('Content-Type', 'text/turtle')
185-
debugGlob('returning turtle')
186-
187-
res.send(data)
188-
return next()
189-
})
180+
181+
const requestUri = await ldp.resourceMapper.getPublicUrl(req)
182+
const data = $rdf.serialize(undefined, globGraph, requestUri, 'text/turtle')
183+
// TODO this should be added as a middleware in the routes
184+
res.setHeader('Content-Type', 'text/turtle')
185+
debugGlob('returning turtle')
186+
187+
res.send(data)
188+
next()
190189
})
191190
}
192191

lib/handlers/put.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ async function putStream (req, res, next, stream = req) {
3636
async function putAcl (req, res, next) {
3737
const ldp = req.app.locals.ldp
3838
const contentType = req.get('content-type')
39-
const filename = ldp.resourceMapper.getFilePath(req) // @@ Note: weird to go through filename @@
40-
const requestUri = (await ldp.resourceMapper.mapFileToUrl({ path: filename, hostname: req.hostname })).url
39+
const requestUri = await ldp.resourceMapper.getPublicUrl(req)
4140

4241
if (ldp.isValidRdf(req.body, requestUri, contentType)) {
4342
const stream = stringToStream(req.body)

lib/resource-mapper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class ResourceMapper {
128128
return { url, contentType: this._getContentTypeByExtension(path) }
129129
}
130130

131+
// Returns the public URL for the given HTTP request
132+
async getPublicUrl (req) {
133+
const filename = this.getFilePath(req)
134+
const { url } = await this.mapFileToUrl({ path: filename, hostname: req.hostname })
135+
return url
136+
}
137+
131138
// Gets the base file path for the given hostname
132139
getBasePath (hostname) {
133140
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`

0 commit comments

Comments
 (0)