@@ -18,8 +18,8 @@ const parse = require('./utils').parse
18
18
const fetch = require ( 'node-fetch' )
19
19
const { promisify } = require ( 'util' )
20
20
const URL = require ( 'url' )
21
- const URI = require ( 'urijs' )
22
21
const withLock = require ( './lock' )
22
+ const utilPath = require ( 'path' )
23
23
24
24
const RDF_MIME_TYPES = new Set ( [
25
25
'text/turtle' , // .ttl
@@ -129,7 +129,7 @@ class LDP {
129
129
}
130
130
}
131
131
132
- async post ( host , containerPath , stream , { container, slug, extension } ) {
132
+ async post ( hostname , containerPath , stream , { container, slug, extension, contentType } ) {
133
133
const ldp = this
134
134
debug . handlers ( 'POST -- On parent: ' + containerPath )
135
135
// prepare slug
@@ -144,26 +144,26 @@ class LDP {
144
144
extension = ''
145
145
}
146
146
// 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
150
150
if ( container ) {
151
151
// 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 += '/'
155
155
}
156
156
}
157
- const { url : putUrl , contentType } = await this . resourceMapper . mapFileToUrl ( { path : resourcePath , hostname : host } )
157
+ // const { url: putUrl } = await this.resourceMapper.mapFileToUrl({ path: resourceUrl , hostname })
158
158
159
159
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
160
160
// for JSON bodies. So, the stream needs to be reset
161
161
if ( contentType . includes ( 'application/json' ) ) {
162
162
stream = intoStream ( JSON . stringify ( stream . body ) )
163
163
}
164
164
165
- await ldp . put ( putUrl , stream , contentType )
166
- return originalPath
165
+ await ldp . put ( resourceUrl , stream , contentType )
166
+ return URL . parse ( originalUrl ) . path
167
167
}
168
168
169
169
/**
@@ -432,19 +432,17 @@ class LDP {
432
432
}
433
433
}
434
434
435
- getAvailablePath ( host , containerURI , { slug = uuid . v1 ( ) , extension } ) {
436
- const path = slug + extension
437
- function ensureNotExists ( self , newPath ) {
438
- // Verify whether the new path already exists
439
- return self . exists ( host , newPath ) . then (
440
- // If it does, generate another one
441
- ( ) => ensureNotExists ( self , URI . joinPaths ( containerURI ,
442
- `${ uuid . v1 ( ) . split ( '-' ) [ 0 ] } -${ path } ` ) . toString ( ) ) ,
443
- // If not, we found an appropriate path
444
- ( ) => newPath
445
- )
435
+ async getAvailableUrl ( hostname , containerURI , { slug = uuid . v1 ( ) , extension } ) {
436
+ let requestUrl = this . resourceMapper . resolveUrl ( hostname , containerURI )
437
+ requestUrl = requestUrl . replace ( / \/ * $ / , '/' )
438
+
439
+ const { path : containerFilePath } = await this . resourceMapper . mapUrlToFile ( { url : requestUrl } )
440
+ let fileName = slug + extension
441
+ if ( await promisify ( fs . exists ) ( utilPath . join ( containerFilePath , fileName ) ) ) {
442
+ fileName = `${ uuid . v1 ( ) } -${ fileName } `
446
443
}
447
- return ensureNotExists ( this , URI . joinPaths ( containerURI , path ) . toString ( ) )
444
+
445
+ return requestUrl + fileName
448
446
}
449
447
450
448
getTrustedOrigins ( req ) {
0 commit comments