@@ -18,8 +18,8 @@ const parse = require('./utils').parse
1818const fetch = require ( 'node-fetch' )
1919const { promisify } = require ( 'util' )
2020const URL = require ( 'url' )
21- const URI = require ( 'urijs' )
2221const withLock = require ( './lock' )
22+ const utilPath = require ( 'path' )
2323
2424const RDF_MIME_TYPES = new Set ( [
2525 'text/turtle' , // .ttl
@@ -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,19 +432,17 @@ class LDP {
432432 }
433433 }
434434
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 } `
446443 }
447- return ensureNotExists ( this , URI . joinPaths ( containerURI , path ) . toString ( ) )
444+
445+ return requestUrl + fileName
448446 }
449447
450448 getTrustedOrigins ( req ) {
0 commit comments