Skip to content

Commit 73cae8a

Browse files
committed
Fix minor LDP issues with new ResourceMapper
1 parent f9e1b70 commit 73cae8a

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

lib/handlers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ async function handler (req, res, next) {
99
const ldp = req.app.locals.ldp
1010
const negotiator = new Negotiator(req)
1111
const requestedType = negotiator.mediaType()
12-
const { path: filename } = await req.app.locals.ldp.resourceMapper.mapUrlToFile({ url: req })
1312

1413
try {
14+
const { path: filename } = await req.app.locals.ldp.resourceMapper.mapUrlToFile({ url: req })
15+
1516
const stats = await ldp.stat(filename)
1617
if (!stats.isDirectory()) {
1718
return next()

lib/ldp.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const mime = require('mime-types')
21
const path = require('path')
32
const url = require('url')
43
const fs = require('fs')
54
const $rdf = require('rdflib')
65
const mkdirp = require('fs-extra').mkdirp
76
const uuid = require('uuid')
87
const debug = require('./debug')
9-
const utils = require('./utils')
108
const error = require('./http-error')
119
const stringToStream = require('./utils').stringToStream
1210
const serialize = require('./utils').serialize
@@ -102,16 +100,12 @@ class LDP {
102100
}
103101

104102
async readResource (url) {
105-
const { path } = await this.resourceMapper.mapUrlToFile({ url })
106-
return new Promise((resolve, reject) => {
107-
fs.readFile(
108-
path,
109-
{ 'encoding': 'utf8' },
110-
(err, data) => {
111-
if (err) return reject(error(err, "Can't read file"))
112-
resolve(data)
113-
})
114-
})
103+
try {
104+
const { path } = await this.resourceMapper.mapUrlToFile({ url })
105+
return await promisify(fs.readFile)(path, {'encoding': 'utf8'})
106+
} catch (err) {
107+
throw error(404, "Can't read file")
108+
}
115109
}
116110

117111
async readContainerMeta (url) {
@@ -330,15 +324,15 @@ class LDP {
330324
}
331325

332326
async get (options) {
333-
const { path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options })
334-
const baseUri = this.resourceMapper.resolveUrl(options.hostname)
335-
336-
let stats
327+
let filename, contentType, stats
337328
try {
329+
({ path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options }))
338330
stats = await this.stat(filename)
339331
} catch (err) {
340-
throw error(err, 'Can\'t find file requested: ' + filename)
332+
throw error(404, 'Can\'t find file requested: ' + options)
341333
}
334+
const baseUri = this.resourceMapper.getBaseUrl(options.hostname)
335+
342336
// Just return, since resource exists
343337
if (!options.includeBody) {
344338
return { 'stream': stats, 'contentType': contentType, 'container': stats.isDirectory() }
@@ -386,32 +380,27 @@ class LDP {
386380
})
387381
.on('open', function () {
388382
debug.handlers(`GET -- Reading ${filename}`)
389-
let contentType = mime.lookup(filename) || DEFAULT_CONTENT_TYPE
390-
if (utils.hasSuffix(filename, this.turtleExtensions)) {
391-
contentType = 'text/turtle'
392-
}
393383
return resolve({ stream, contentType, container: false, contentRange, chunksize })
394384
})
395385
})
396386
}
397387
}
398388

399389
async delete (url) {
400-
const { path } = await this.resourceMapper.mapUrlToFile({ url })
401-
402390
// First check if the path points to a valid file
403-
let stats
391+
let filePath, stats
404392
try {
405-
stats = await this.stat(path)
393+
({ path: filePath } = await this.resourceMapper.mapUrlToFile({ url }))
394+
stats = await this.stat(filePath)
406395
} catch (err) {
407396
throw error(404, "Can't find " + err)
408397
}
409398

410399
// If so, delete the directory or file
411400
if (stats.isDirectory()) {
412-
return this.deleteContainer(path)
401+
return this.deleteContainer(filePath)
413402
} else {
414-
return this.deleteResource(path)
403+
return this.deleteResource(filePath)
415404
}
416405
}
417406

0 commit comments

Comments
 (0)