|
1 | | -const mime = require('mime-types') |
2 | 1 | const path = require('path') |
3 | 2 | const url = require('url') |
4 | 3 | const fs = require('fs') |
5 | 4 | const $rdf = require('rdflib') |
6 | 5 | const mkdirp = require('fs-extra').mkdirp |
7 | 6 | const uuid = require('uuid') |
8 | 7 | const debug = require('./debug') |
9 | | -const utils = require('./utils') |
10 | 8 | const error = require('./http-error') |
11 | 9 | const stringToStream = require('./utils').stringToStream |
12 | 10 | const serialize = require('./utils').serialize |
@@ -102,16 +100,12 @@ class LDP { |
102 | 100 | } |
103 | 101 |
|
104 | 102 | 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 | + } |
115 | 109 | } |
116 | 110 |
|
117 | 111 | async readContainerMeta (url) { |
@@ -330,15 +324,15 @@ class LDP { |
330 | 324 | } |
331 | 325 |
|
332 | 326 | 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 |
337 | 328 | try { |
| 329 | + ({ path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options })) |
338 | 330 | stats = await this.stat(filename) |
339 | 331 | } catch (err) { |
340 | | - throw error(err, 'Can\'t find file requested: ' + filename) |
| 332 | + throw error(404, 'Can\'t find file requested: ' + options) |
341 | 333 | } |
| 334 | + const baseUri = this.resourceMapper.getBaseUrl(options.hostname) |
| 335 | + |
342 | 336 | // Just return, since resource exists |
343 | 337 | if (!options.includeBody) { |
344 | 338 | return { 'stream': stats, 'contentType': contentType, 'container': stats.isDirectory() } |
@@ -386,32 +380,27 @@ class LDP { |
386 | 380 | }) |
387 | 381 | .on('open', function () { |
388 | 382 | 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 | | - } |
393 | 383 | return resolve({ stream, contentType, container: false, contentRange, chunksize }) |
394 | 384 | }) |
395 | 385 | }) |
396 | 386 | } |
397 | 387 | } |
398 | 388 |
|
399 | 389 | async delete (url) { |
400 | | - const { path } = await this.resourceMapper.mapUrlToFile({ url }) |
401 | | - |
402 | 390 | // First check if the path points to a valid file |
403 | | - let stats |
| 391 | + let filePath, stats |
404 | 392 | try { |
405 | | - stats = await this.stat(path) |
| 393 | + ({ path: filePath } = await this.resourceMapper.mapUrlToFile({ url })) |
| 394 | + stats = await this.stat(filePath) |
406 | 395 | } catch (err) { |
407 | 396 | throw error(404, "Can't find " + err) |
408 | 397 | } |
409 | 398 |
|
410 | 399 | // If so, delete the directory or file |
411 | 400 | if (stats.isDirectory()) { |
412 | | - return this.deleteContainer(path) |
| 401 | + return this.deleteContainer(filePath) |
413 | 402 | } else { |
414 | | - return this.deleteResource(path) |
| 403 | + return this.deleteResource(filePath) |
415 | 404 | } |
416 | 405 | } |
417 | 406 |
|
|
0 commit comments