33
44var mime = require ( 'mime' ) ;
55var fs = require ( 'fs' ) ;
6- var fse = require ( 'fs-extra' ) ;
6+ var mkdirp = require ( 'fs-extra' ) . mkdirp ;
77var path = require ( 'path' ) ;
88var $rdf = require ( 'rdflib' ) ;
9- var S = require ( 'string' ) ;
109
1110var debug = require ( '../logging' ) . handlers ;
1211var file = require ( '../fileStore.js' ) ;
@@ -19,53 +18,32 @@ function handler(req, res) {
1918 debug ( "PUT -- Text length: " + ( req . text ? req . text . length : 'undefined' ) ) ;
2019 res . header ( 'MS-Author-Via' , 'SPARQL' ) ;
2120
22- var filename = file . uriToFilename ( req . path , options . base ) ;
21+ var filePath = file . uriToFilename ( req . path , options . base ) ;
2322
2423 // PUT requests not supported on containers. Use POST instead
25- if ( S ( filename ) . endsWith ( '/' ) ) {
26- return res . status ( 409 ) . send ( "PUT to containers not supported. Use POST method instead" ) ;
24+ if ( filePath [ filePath . length - 1 ] === '/' ) {
25+ return res . status ( 409 )
26+ . send ( "PUT to containers not supported. Use POST method instead" ) ;
2727 }
2828
29- // var ct1 = req.get('content-type');
30- // var ct2 = mime.lookup(filename);
31-
32- // if (ct1 && ct2 && (ct1 !== ct2)) {
33- // debug("PUT -- MIME mismatch. Content-type: " + ct1 +
34- // ". MIME: " + ct2);
35- // return res.status(415).send("Content type mismatch with path file.extenstion");
36- // }
37- // if (!ct2) {
38- // return res.status(415).send("Sorry, Filename must have extension for content type");
39- // }
40-
41- createDir ( ) ;
42-
43- function createDir ( ) {
44- var directory = path . dirname ( filename ) ;
45- fs . exists ( directory , function ( exists ) {
46- if ( exists ) {
47- writeFile ( null ) ;
48- } else {
49- fse . mkdirp ( directory , writeFile ) ;
50- }
51- } ) ;
52- }
29+ writeFile ( filePath , req . text , function ( err ) {
30+ if ( err ) {
31+ debug ( "PUT -- Write error: " + err ) ;
32+ return res . status ( 500 ) . send ( "Can't write file: " + err ) ;
33+ }
34+ debug ( "PUT -- Write Ok. Bytes written: " + req . text . length ) ;
35+ return res . sendStatus ( 201 ) ;
36+ } ) ;
37+ }
5338
54- function writeFile ( mkdirErr ) {
55- if ( mkdirErr ) {
56- debug ( "PUT -- Error creating directory: " + mkdirErr ) ;
57- return res . status ( 500 ) . send ( "Can't create directory" ) ;
39+ function writeFile ( filePath , contents , cb ) {
40+ mkdirp ( path . dirname ( filePath ) , function ( err ) {
41+ if ( err ) {
42+ debug ( "PUT -- Error creating directory: " + err ) ;
43+ return cb ( err ) ;
5844 }
59- fs . writeFile ( filename , req . text , function ( err ) {
60- if ( err ) {
61- debug ( "PUT -- Write error: " + err ) ;
62- return res . status ( 500 ) . send ( "Can't write file: " + err ) ;
63- } else {
64- debug ( "PUT -- Write Ok. Bytes written: " + req . text . length ) ;
65- return res . sendStatus ( 201 ) ;
66- }
67- } ) ; // file write
68- }
45+ fs . writeFile ( filePath , contents , cb ) ;
46+ } ) ;
6947}
7048
7149exports . handler = handler ;
0 commit comments