@@ -191,21 +191,23 @@ class LDP {
191191 extension = ''
192192 }
193193 // TODO: possibly package this in ldp.post
194- ldp . getAvailablePath ( host , containerPath , { slug, extension } ) . then ( resourcePath => {
195- debug . handlers ( 'POST -- Will create at: ' + resourcePath )
196- let originalPath = resourcePath
197- if ( container ) {
198- // Create directory by an LDP PUT to the container's .meta resource
199- resourcePath = path . join ( originalPath , ldp . suffixMeta )
200- if ( originalPath && ! originalPath . endsWith ( '/' ) ) {
201- originalPath += '/'
194+ ldp . getAvailablePath ( host , containerPath , { slug, extension } )
195+ . then ( async resourcePath => {
196+ debug . handlers ( 'POST -- Will create at: ' + resourcePath )
197+ let originalPath = resourcePath
198+ if ( container ) {
199+ // Create directory by an LDP PUT to the container's .meta resource
200+ resourcePath = path . join ( originalPath , ldp . suffixMeta )
201+ if ( originalPath && ! originalPath . endsWith ( '/' ) ) {
202+ originalPath += '/'
203+ }
202204 }
203- }
204- ldp . put ( host , resourcePath , stream , function ( err ) {
205- if ( err ) callback ( err )
205+ const { url : putUrl } = await this . resourceMapper . mapFileToUrl (
206+ { path : this . resourceMapper . _rootPath + resourcePath , hostname : host } )
207+ await ldp . put ( putUrl , stream )
206208 callback ( null , originalPath )
207209 } )
208- } )
210+ . catch ( ( err ) => callback ( err ) )
209211 }
210212
211213 /**
@@ -226,63 +228,54 @@ class LDP {
226228 *
227229 * @return {Promise<Graph> }
228230 */
229- putGraph ( graph , uri , contentType = DEFAULT_CONTENT_TYPE ) {
230- return new Promise ( ( resolve , reject ) => {
231- let parsedUri = url . parse ( uri )
232- let hostname = parsedUri . hostname
233- let path = parsedUri . pathname
234-
235- serialize ( graph , uri , contentType )
236- . then ( ( content ) => {
237- let stream = stringToStream ( content )
238-
239- this . put ( hostname , path , stream , ( error ) => {
240- if ( error ) { return reject ( error ) }
241-
242- resolve ( graph )
243- } )
244- } )
245- . catch ( reject )
246- } )
231+ async putGraph ( graph , uri , contentType = DEFAULT_CONTENT_TYPE ) {
232+ const { path } = url . parse ( uri )
233+ const content = await serialize ( graph , uri , contentType )
234+ let stream = stringToStream ( content )
235+ return await this . put ( path , stream )
247236 }
248237
249- put ( host , resourcePath , stream , callback ) {
250- const ldp = this
251- const root = ! ldp . multiuser ? ldp . root : ldp . root + host + '/'
252- const filePath = utils . uriToFilename ( resourcePath , root , host )
238+ async put ( url , stream ) {
239+ const { path : filePath } = await this . resourceMapper . mapUrlToFile ( { url } )
253240
254241 // PUT requests not supported on containers. Use POST instead
255242 if ( filePath . endsWith ( '/' ) ) {
256- return callback ( error ( 409 ,
257- 'PUT not supported on containers, use POST instead' ) )
243+ throw error ( 409 ,
244+ 'PUT not supported on containers, use POST instead' )
258245 }
259246
260247 // First check if we are above quota
261- overQuota ( root , this . serverUri ) . then ( ( isOverQuota ) => {
262- if ( isOverQuota ) {
263- return callback ( error ( 413 ,
264- 'User has exceeded their storage quota' ) )
265- }
248+ let isOverQuota
249+ try {
250+ isOverQuota = await overQuota ( this . resourceMapper . rootPath , this . serverUri )
251+ } catch ( err ) {
252+ throw error ( 500 , 'Error finding user quota' )
253+ }
254+ if ( isOverQuota ) {
255+ throw error ( 413 , 'User has exceeded their storage quota' )
256+ }
266257
267- // Second, create the enclosing directory, if necessary
268- const dirName = path . dirname ( filePath )
269- mkdirp ( dirName , ( err ) => {
270- if ( err ) {
271- debug . handlers ( 'PUT -- Error creating directory: ' + err )
272- return callback ( error ( err ,
273- 'Failed to create the path to the new resource' ) )
274- }
275- // Directory created, now write the file
276- const file = stream . pipe ( fs . createWriteStream ( filePath ) )
277- file . on ( 'error' , function ( ) {
278- callback ( error ( 500 , 'Error writing data' ) )
279- } )
280- file . on ( 'finish' , function ( ) {
281- debug . handlers ( 'PUT -- Wrote data to: ' + filePath )
282- callback ( null )
283- } )
258+ // Second, create the enclosing directory, if necessary
259+ const dirName = path . dirname ( filePath )
260+ try {
261+ await promisify ( mkdirp ) ( dirName )
262+ } catch ( err ) {
263+ debug . handlers ( 'PUT -- Error creating directory: ' + err )
264+ throw error ( err ,
265+ 'Failed to create the path to the new resource' )
266+ }
267+
268+ // Directory created, now write the file
269+ return await new Promise ( ( resolve , reject ) => {
270+ const file = stream . pipe ( fs . createWriteStream ( filePath ) )
271+ file . on ( 'error' , function ( ) {
272+ reject ( error ( 500 , 'Error writing data' ) )
273+ } )
274+ file . on ( 'finish' , function ( ) {
275+ debug . handlers ( 'PUT -- Wrote data to: ' + filePath )
276+ resolve ( null )
284277 } )
285- } ) . catch ( ( ) => callback ( error ( 500 , 'Error finding user quota' ) ) )
278+ } )
286279 }
287280
288281 exists ( hostname , path , callback ) {
0 commit comments