From 694bcd003de5632e536bf45808e5f13cc05704e3 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Fri, 21 Jun 2019 11:29:53 -0400 Subject: [PATCH 1/6] POST does not error in multiuser mode --- lib/ldp.js | 18 +++++++++++++++++- lib/resource-mapper.js | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/ldp.js b/lib/ldp.js index 3f178c924..2ff1e89bd 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -432,7 +432,23 @@ class LDP { } } - getAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) { + async getAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) { + const fileName = slug + extension + function ensureNotExists (self, newPath) { + // Verify whether the new path already exists + return self.exists(host, newPath).then( + // If it does, generate another one + () => ensureNotExists(self, URI.joinPaths(containerURI, + `${uuid.v1().split('-')[0]}-${fileName}`).toString()), + // If not, we found an appropriate path + () => newPath + ) + } + const relativePath = await ensureNotExists(this, URI.joinPaths(containerURI, fileName).toString()) + return this.resourceMapper.resolveRelativeFilePath(host, relativePath) + } + + oldGetAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) { const path = slug + extension function ensureNotExists (self, newPath) { // Verify whether the new path already exists diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index cc62fc8ff..9cfc97bf4 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -62,6 +62,10 @@ class ResourceMapper { : `${this._rootPath}/${hostname}${filePath}` } + resolveRelativeFilePath (hostname, filePath = '') { + return !this._includeHost ? filePath : `/${hostname}${filePath}` + } + // Maps a given server file to a URL async mapFileToUrl ({ path, hostname }) { // Remove the root path if specified From 65e1746b3eb716aac2ecf2baf6ff0060079e8ae0 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Fri, 21 Jun 2019 11:31:18 -0400 Subject: [PATCH 2/6] Remove old code --- lib/ldp.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 2ff1e89bd..8d2d32f41 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -448,21 +448,6 @@ class LDP { return this.resourceMapper.resolveRelativeFilePath(host, relativePath) } - oldGetAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) { - const path = slug + extension - function ensureNotExists (self, newPath) { - // Verify whether the new path already exists - return self.exists(host, newPath).then( - // If it does, generate another one - () => ensureNotExists(self, URI.joinPaths(containerURI, - `${uuid.v1().split('-')[0]}-${path}`).toString()), - // If not, we found an appropriate path - () => newPath - ) - } - return ensureNotExists(this, URI.joinPaths(containerURI, path).toString()) - } - getTrustedOrigins (req) { let trustedOrigins = [this.resourceMapper.resolveUrl(req.hostname)].concat(this.trustedOrigins) if (this.multiuser) { From adef27a821026a9a54d68d5f54d97fac1d3bfdce Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Wed, 26 Jun 2019 07:32:55 -0400 Subject: [PATCH 3/6] Refactor to maintain url and content type --- lib/handlers/post.js | 2 +- lib/ldp.js | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/handlers/post.js b/lib/handlers/post.js index 928528417..c0cd9a321 100644 --- a/lib/handlers/post.js +++ b/lib/handlers/post.js @@ -86,7 +86,7 @@ async function handler (req, res, next) { const extension = mimeType in extensions ? `.${extensions[mimeType][0]}` : '' ldp.post(req.hostname, containerPath, req, - { slug, extension, container: links.isBasicContainer }).then( + { slug, extension, container: links.isBasicContainer, contentType }).then( resourcePath => { debug('File stored in ' + resourcePath) header.addLinks(res, links) diff --git a/lib/ldp.js b/lib/ldp.js index 8d2d32f41..6624d00b1 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -129,7 +129,7 @@ class LDP { } } - async post (host, containerPath, stream, { container, slug, extension }) { + async post (hostname, containerPath, stream, { container, slug, extension, contentType }) { const ldp = this debug.handlers('POST -- On parent: ' + containerPath) // prepare slug @@ -144,17 +144,17 @@ class LDP { extension = '' } // TODO: possibly package this in ldp.post - let resourcePath = await ldp.getAvailablePath(host, containerPath, { slug, extension }) - debug.handlers('POST -- Will create at: ' + resourcePath) - let originalPath = resourcePath + let resourceUrl = await ldp.getAvailableUrl(hostname, containerPath, { slug, extension }) + debug.handlers('POST -- Will create at: ' + resourceUrl) + let originalUrl = resourceUrl if (container) { // Create directory by an LDP PUT to the container's .meta resource - resourcePath = join(originalPath, ldp.suffixMeta) - if (originalPath && !originalPath.endsWith('/')) { - originalPath += '/' + resourceUrl = `${resourceUrl}${resourceUrl.endsWith('/') ? '' : '/'}${ldp.suffixMeta}` + if (originalUrl && !originalUrl.endsWith('/')) { + originalUrl += '/' } } - const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl({ path: resourcePath, hostname: host }) + // const { url: putUrl } = await this.resourceMapper.mapFileToUrl({ path: resourceUrl, hostname }) // HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data // for JSON bodies. So, the stream needs to be reset @@ -162,8 +162,8 @@ class LDP { stream = intoStream(JSON.stringify(stream.body)) } - await ldp.put(putUrl, stream, contentType) - return originalPath + await ldp.put(resourceUrl, stream, contentType) + return URL.parse(originalUrl).path } /** @@ -432,11 +432,11 @@ class LDP { } } - async getAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) { + async getAvailableUrl (hostname, containerURI, { slug = uuid.v1(), extension }) { const fileName = slug + extension function ensureNotExists (self, newPath) { // Verify whether the new path already exists - return self.exists(host, newPath).then( + return self.exists(hostname, newPath).then( // If it does, generate another one () => ensureNotExists(self, URI.joinPaths(containerURI, `${uuid.v1().split('-')[0]}-${fileName}`).toString()), @@ -445,7 +445,7 @@ class LDP { ) } const relativePath = await ensureNotExists(this, URI.joinPaths(containerURI, fileName).toString()) - return this.resourceMapper.resolveRelativeFilePath(host, relativePath) + return this.resourceMapper.resolveUrl(hostname, relativePath) } getTrustedOrigins (req) { From 7ff0edd877fdce0a11ddc7c85e558e5a8812f174 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Wed, 26 Jun 2019 07:35:50 -0400 Subject: [PATCH 4/6] Remove resolveRelativeFilePath --- lib/resource-mapper.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index 9cfc97bf4..cc62fc8ff 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -62,10 +62,6 @@ class ResourceMapper { : `${this._rootPath}/${hostname}${filePath}` } - resolveRelativeFilePath (hostname, filePath = '') { - return !this._includeHost ? filePath : `/${hostname}${filePath}` - } - // Maps a given server file to a URL async mapFileToUrl ({ path, hostname }) { // Remove the root path if specified From 71829013365216c29adba754c704c32a88238b59 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Wed, 26 Jun 2019 15:13:02 -0400 Subject: [PATCH 5/6] Refactor to use urls in case of encoding errors --- lib/ldp.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 6624d00b1..862cded41 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -18,8 +18,8 @@ const parse = require('./utils').parse const fetch = require('node-fetch') const { promisify } = require('util') const URL = require('url') -const URI = require('urijs') const withLock = require('./lock') +const utilPath = require('path') const RDF_MIME_TYPES = new Set([ 'text/turtle', // .ttl @@ -433,19 +433,16 @@ class LDP { } async getAvailableUrl (hostname, containerURI, { slug = uuid.v1(), extension }) { - const fileName = slug + extension - function ensureNotExists (self, newPath) { - // Verify whether the new path already exists - return self.exists(hostname, newPath).then( - // If it does, generate another one - () => ensureNotExists(self, URI.joinPaths(containerURI, - `${uuid.v1().split('-')[0]}-${fileName}`).toString()), - // If not, we found an appropriate path - () => newPath - ) + let requestUrl = this.resourceMapper.resolveUrl(hostname, containerURI) + requestUrl.replace(/\/*$/, '/') + + const { path: containerFilePath } = await this.resourceMapper.mapUrlToFile({ url: requestUrl }) + let fileName = slug + extension + if (fs.existsSync(utilPath.join(containerFilePath, fileName))) { + fileName = `${uuid.v1()}-${fileName}` } - const relativePath = await ensureNotExists(this, URI.joinPaths(containerURI, fileName).toString()) - return this.resourceMapper.resolveUrl(hostname, relativePath) + + return requestUrl + fileName } getTrustedOrigins (req) { From 75efb962be2533c9938fd0b82b7d5490e94b5688 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Wed, 26 Jun 2019 16:29:53 -0400 Subject: [PATCH 6/6] Promisify fs exists --- lib/ldp.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 862cded41..2cef477db 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -434,11 +434,11 @@ class LDP { async getAvailableUrl (hostname, containerURI, { slug = uuid.v1(), extension }) { let requestUrl = this.resourceMapper.resolveUrl(hostname, containerURI) - requestUrl.replace(/\/*$/, '/') + requestUrl = requestUrl.replace(/\/*$/, '/') const { path: containerFilePath } = await this.resourceMapper.mapUrlToFile({ url: requestUrl }) let fileName = slug + extension - if (fs.existsSync(utilPath.join(containerFilePath, fileName))) { + if (await promisify(fs.exists)(utilPath.join(containerFilePath, fileName))) { fileName = `${uuid.v1()}-${fileName}` }