Skip to content

Commit adef27a

Browse files
committed
Refactor to maintain url and content type
1 parent 65e1746 commit adef27a

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/handlers/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function handler (req, res, next) {
8686
const extension = mimeType in extensions ? `.${extensions[mimeType][0]}` : ''
8787

8888
ldp.post(req.hostname, containerPath, req,
89-
{ slug, extension, container: links.isBasicContainer }).then(
89+
{ slug, extension, container: links.isBasicContainer, contentType }).then(
9090
resourcePath => {
9191
debug('File stored in ' + resourcePath)
9292
header.addLinks(res, links)

lib/ldp.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class LDP {
129129
}
130130
}
131131

132-
async post (host, containerPath, stream, { container, slug, extension }) {
132+
async post (hostname, containerPath, stream, { container, slug, extension, contentType }) {
133133
const ldp = this
134134
debug.handlers('POST -- On parent: ' + containerPath)
135135
// prepare slug
@@ -144,26 +144,26 @@ class LDP {
144144
extension = ''
145145
}
146146
// TODO: possibly package this in ldp.post
147-
let resourcePath = await ldp.getAvailablePath(host, containerPath, { slug, extension })
148-
debug.handlers('POST -- Will create at: ' + resourcePath)
149-
let originalPath = resourcePath
147+
let resourceUrl = await ldp.getAvailableUrl(hostname, containerPath, { slug, extension })
148+
debug.handlers('POST -- Will create at: ' + resourceUrl)
149+
let originalUrl = resourceUrl
150150
if (container) {
151151
// Create directory by an LDP PUT to the container's .meta resource
152-
resourcePath = join(originalPath, ldp.suffixMeta)
153-
if (originalPath && !originalPath.endsWith('/')) {
154-
originalPath += '/'
152+
resourceUrl = `${resourceUrl}${resourceUrl.endsWith('/') ? '' : '/'}${ldp.suffixMeta}`
153+
if (originalUrl && !originalUrl.endsWith('/')) {
154+
originalUrl += '/'
155155
}
156156
}
157-
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl({ path: resourcePath, hostname: host })
157+
// const { url: putUrl } = await this.resourceMapper.mapFileToUrl({ path: resourceUrl, hostname })
158158

159159
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
160160
// for JSON bodies. So, the stream needs to be reset
161161
if (contentType.includes('application/json')) {
162162
stream = intoStream(JSON.stringify(stream.body))
163163
}
164164

165-
await ldp.put(putUrl, stream, contentType)
166-
return originalPath
165+
await ldp.put(resourceUrl, stream, contentType)
166+
return URL.parse(originalUrl).path
167167
}
168168

169169
/**
@@ -432,11 +432,11 @@ class LDP {
432432
}
433433
}
434434

435-
async getAvailablePath (host, containerURI, { slug = uuid.v1(), extension }) {
435+
async getAvailableUrl (hostname, containerURI, { slug = uuid.v1(), extension }) {
436436
const fileName = slug + extension
437437
function ensureNotExists (self, newPath) {
438438
// Verify whether the new path already exists
439-
return self.exists(host, newPath).then(
439+
return self.exists(hostname, newPath).then(
440440
// If it does, generate another one
441441
() => ensureNotExists(self, URI.joinPaths(containerURI,
442442
`${uuid.v1().split('-')[0]}-${fileName}`).toString()),
@@ -445,7 +445,7 @@ class LDP {
445445
)
446446
}
447447
const relativePath = await ensureNotExists(this, URI.joinPaths(containerURI, fileName).toString())
448-
return this.resourceMapper.resolveRelativeFilePath(host, relativePath)
448+
return this.resourceMapper.resolveUrl(hostname, relativePath)
449449
}
450450

451451
getTrustedOrigins (req) {

0 commit comments

Comments
 (0)