Skip to content

Commit 3a7b199

Browse files
committed
Use ResourceMapper in ldp#get
1 parent 443eda4 commit 3a7b199

File tree

5 files changed

+313
-336
lines changed

5 files changed

+313
-336
lines changed

lib/handlers/get.js

Lines changed: 81 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const error = require('../http-error')
1919
const RDFs = require('../ldp').RDF_MIME_TYPES
2020
const LegacyResourceMapper = require('../legacy-resource-mapper')
2121

22-
function handler (req, res, next) {
22+
async function handler (req, res, next) {
2323
const ldp = req.app.locals.ldp
2424
const includeBody = req.method === 'GET'
2525
const negotiator = new Negotiator(req)
@@ -42,107 +42,105 @@ function handler (req, res, next) {
4242
const options = {
4343
'hostname': req.hostname,
4444
'path': path,
45-
'baseUri': baseUri,
4645
'includeBody': includeBody,
4746
'possibleRDFType': possibleRDFType,
4847
'range': req.headers.range
4948
}
50-
ldp.get(options, function (err, ret) {
49+
50+
let ret
51+
try {
52+
ret = await ldp.get(options)
53+
} catch (err) {
5154
// use globHandler if magic is detected
52-
if (err && err.status === 404 && glob.hasMagic(path)) {
55+
if (err.status === 404 && glob.hasMagic(path)) {
5356
debug('forwarding to glob request')
5457
return globHandler(req, res, next)
55-
}
56-
57-
// Handle error
58-
if (err) {
58+
} else {
5959
debug(req.method + ' -- Error: ' + err.status + ' ' + err.message)
6060
return next(err)
6161
}
62+
}
6263

63-
let stream
64-
let contentType
65-
let container
66-
let contentRange
67-
let chunksize
68-
69-
if (ret) {
70-
stream = ret.stream
71-
contentType = ret.contentType
72-
container = ret.container
73-
contentRange = ret.contentRange
74-
chunksize = ret.chunksize
75-
}
76-
77-
// Till here it must exist
78-
if (!includeBody) {
79-
debug('HEAD only')
80-
const rootUrl = utils.getBaseUri(req)
81-
const mapper = new LegacyResourceMapper({
82-
rootUrl,
83-
rootPath: ldp.root,
84-
includeHost: ldp.multiuser
85-
})
86-
return mapper.mapFileToUrl({ path }).then(mappedFile => {
87-
contentType = mappedFile.contentType
88-
res.setHeader('Content-Type', contentType)
89-
res.status(200).send('OK')
90-
return next()
91-
})
92-
}
64+
let stream
65+
let contentType
66+
let container
67+
let contentRange
68+
let chunksize
69+
70+
if (ret) {
71+
stream = ret.stream
72+
contentType = ret.contentType
73+
container = ret.container
74+
contentRange = ret.contentRange
75+
chunksize = ret.chunksize
76+
}
9377

94-
// Handle dataBrowser
95-
if (requestedType && requestedType.includes('text/html')) {
96-
let mimeTypeByExt = mime.lookup(_path.basename(path))
97-
let isHtmlResource = mimeTypeByExt && mimeTypeByExt.includes('html')
98-
let useDataBrowser = ldp.dataBrowserPath && (
99-
container ||
100-
RDFs.includes(contentType) && !isHtmlResource && !ldp.suppressDataBrowser)
101-
102-
if (useDataBrowser) {
103-
res.set('Content-Type', 'text/html')
104-
const defaultDataBrowser = _path.join(__dirname, '../../static/databrowser.html')
105-
const dataBrowserPath = ldp.dataBrowserPath === 'default' ? defaultDataBrowser : ldp.dataBrowserPath
106-
debug(' sending data browser file: ' + dataBrowserPath)
107-
res.sendFile(dataBrowserPath)
108-
return
109-
} else if (stream) {
110-
res.setHeader('Content-Type', contentType)
111-
return stream.pipe(res)
112-
}
113-
}
78+
// Till here it must exist
79+
if (!includeBody) {
80+
debug('HEAD only')
81+
const rootUrl = utils.getBaseUri(req)
82+
const mapper = new LegacyResourceMapper({
83+
rootUrl,
84+
rootPath: ldp.root,
85+
includeHost: ldp.multiuser
86+
})
87+
const mappedFile = await mapper.mapFileToUrl({ path })
88+
contentType = mappedFile.contentType
89+
res.setHeader('Content-Type', contentType)
90+
res.status(200).send('OK')
91+
return next()
92+
}
11493

115-
// If request accepts the content-type we found
116-
if (stream && negotiator.mediaType([contentType])) {
94+
// Handle dataBrowser
95+
if (requestedType && requestedType.includes('text/html')) {
96+
let mimeTypeByExt = mime.lookup(_path.basename(path))
97+
let isHtmlResource = mimeTypeByExt && mimeTypeByExt.includes('html')
98+
let useDataBrowser = ldp.dataBrowserPath && (
99+
container ||
100+
RDFs.includes(contentType) && !isHtmlResource && !ldp.suppressDataBrowser)
101+
102+
if (useDataBrowser) {
103+
res.set('Content-Type', 'text/html')
104+
const defaultDataBrowser = _path.join(__dirname, '../../static/databrowser.html')
105+
const dataBrowserPath = ldp.dataBrowserPath === 'default' ? defaultDataBrowser : ldp.dataBrowserPath
106+
debug(' sending data browser file: ' + dataBrowserPath)
107+
res.sendFile(dataBrowserPath)
108+
return
109+
} else if (stream) {
117110
res.setHeader('Content-Type', contentType)
118-
if (contentRange) {
119-
const headers = { 'Content-Range': contentRange, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize }
120-
res.writeHead(206, headers)
121-
return stream.pipe(res)
122-
} else {
123-
return stream.pipe(res)
124-
}
111+
return stream.pipe(res)
125112
}
113+
}
126114

127-
// If it is not in our RDFs we can't even translate,
128-
// Sorry, we can't help
129-
if (!possibleRDFType) {
130-
return next(error(406, 'Cannot serve requested type: ' + contentType))
115+
// If request accepts the content-type we found
116+
if (stream && negotiator.mediaType([contentType])) {
117+
res.setHeader('Content-Type', contentType)
118+
if (contentRange) {
119+
const headers = { 'Content-Range': contentRange, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize }
120+
res.writeHead(206, headers)
121+
return stream.pipe(res)
122+
} else {
123+
return stream.pipe(res)
131124
}
125+
}
126+
127+
// If it is not in our RDFs we can't even translate,
128+
// Sorry, we can't help
129+
if (!possibleRDFType) {
130+
return next(error(406, 'Cannot serve requested type: ' + contentType))
131+
}
132132

133+
try {
133134
// Translate from the contentType found to the possibleRDFType desired
134-
translate(stream, baseUri, contentType, possibleRDFType)
135-
.then((data) => {
136-
debug(req.originalUrl + ' translating ' + contentType + ' -> ' + possibleRDFType)
137-
res.setHeader('Content-Type', possibleRDFType)
138-
res.send(data)
139-
return next()
140-
})
141-
.catch((err) => {
142-
debug('error translating: ' + req.originalUrl + ' ' + contentType + ' -> ' + possibleRDFType + ' -- ' + 500 + ' ' + err.message)
143-
return next(error(500, 'Error translating between RDF formats'))
144-
})
145-
})
135+
const data = await translate(stream, baseUri, contentType, possibleRDFType)
136+
debug(req.originalUrl + ' translating ' + contentType + ' -> ' + possibleRDFType)
137+
res.setHeader('Content-Type', possibleRDFType)
138+
res.send(data)
139+
return next()
140+
} catch (err) {
141+
debug('error translating: ' + req.originalUrl + ' ' + contentType + ' -> ' + possibleRDFType + ' -- ' + 500 + ' ' + err.message)
142+
return next(error(500, 'Error translating between RDF formats'))
143+
}
146144
}
147145

148146
function globHandler (req, res, next) {

0 commit comments

Comments
 (0)