MCPcopy Index your code
hub / github.com/nodeSolidServer/node-solid-server / globHandler

Function globHandler

lib/handlers/get.mjs:171–238  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

169}
170
171async function globHandler (req, res, next) {
172 const { ldp } = req.app.locals
173
174 // Ensure this is a glob for all files in a single folder
175 // https://github.com/solid/solid-spec/pull/148
176 const requestUrl = await ldp.resourceMapper.getRequestUrl(req)
177 if (!/^[^*]+\/\*$/.test(requestUrl)) {
178 return next(HTTPError(404, 'Unsupported glob pattern'))
179 }
180
181 // Extract the folder on the file system from the URL glob
182 const folderUrl = requestUrl.substr(0, requestUrl.length - 1)
183 const folderPath = (await ldp.resourceMapper.mapUrlToFile({ url: folderUrl, searchIndex: false })).path
184
185 const globOptions = {
186 noext: true,
187 nobrace: true,
188 nodir: true
189 }
190
191 try {
192 const matches = await glob(`${folderPath}*`, globOptions)
193 if (matches.length === 0) {
194 debugGlob('No files matching the pattern')
195 return next(HTTPError(404, 'No files matching glob pattern'))
196 }
197
198 // Matches found
199 const globGraph = $rdf.graph()
200
201 debugGlob('found matches ' + matches)
202 await Promise.all(matches.map(match => new Promise(async (resolve, reject) => {
203 const urlData = await ldp.resourceMapper.mapFileToUrl({ path: match, hostname: req.hostname })
204 fs.readFile(match, { encoding: 'utf8' }, function (err, fileData) {
205 if (err) {
206 debugGlob('error ' + err)
207 return resolve()
208 }
209 // Files should be Turtle
210 if (urlData.contentType !== 'text/turtle') {
211 return resolve()
212 }
213 // The agent should have Read access to the file
214 hasReadPermissions(match, req, res, function (allowed) {
215 if (allowed) {
216 try {
217 $rdf.parse(fileData, globGraph, urlData.url, 'text/turtle')
218 } catch (parseErr) {
219 debugGlob(`error parsing ${match}: ${parseErr}`)
220 }
221 }
222 return resolve()
223 })
224 })
225 })))
226
227 const data = $rdf.serialize(undefined, globGraph, requestUrl, 'text/turtle')
228 // TODO this should be added as a middleware in the routes

Callers 1

handlerFunction · 0.85

Calls 6

HTTPErrorFunction · 0.85
hasReadPermissionsFunction · 0.85
getRequestUrlMethod · 0.80
mapUrlToFileMethod · 0.80
graphMethod · 0.80
mapFileToUrlMethod · 0.80

Tested by

no test coverage detected