regex - Uploads with white space in file name is causing errors on my server -


i have server used allow people upload files. unfortunately script allows people upload files blank spaces in file name. when links these files generated (also script) file name output these empty spaces. these files can not downloaded python script (which attached). question how best fix problem. not sure if to:

a: use sort of regular expression change file name upon uploading, or

b: if there way can reference actual file name on server (which not know because when ssh in looks correct file path have spaces in it).

i interested in best solution problem is. thanks.

here server code:

var http = require('http'),     url = require('url'),     util = require('util'),     path = require('path'),     fs = require('fs'),     qs = require('querystring');  var formidable = require('formidable'),     mime = require('mime');   function dirtree(filename) {     var stats = fs.lstatsync(filename),         info = {             name: path.basename(filename),             path: ip + ':' + port + '/uploads/finished/' + path.basename(filename),             type: mime.lookup(filename).substring(0, 5)         };     if (stats.isdirectory()) {         info.type = "folder";         info.children = fs.readdirsync(filename).map(function (child) {             return dirtree(filename + '/' + child);         });     }     return info; }   http.createserver(function (request, response) {     if (request.method.tolowercase() == 'get') {         var filepath = './content' + request.url;         if (filepath == './content/') {             filepath = './content/home.html';         }         if (filepath == './content/feed') {             var = dirtree('./content/uploads/finished');             response.end(json.stringify(a));         }         var extname = path.extname(filepath);         var contenttype = mime.lookup(extname);         fs.exists(filepath, function (exists) {             if (exists) {                 fs.readfile(filepath, function (error, content) {                     if (error) {                         response.writehead(500);                         response.end();                     }                     else {                         response.writehead(200, {'content-type': contenttype});                         response.end(content, 'utf-8');                     }                 })             } else {                 response.writehead(404);                 response.end();             }         });     }      var form = new formidable.incomingform;     if (request.url == '/upload') {         var oldpath,             newpath,             filename;         form.uploaddir = './content/uploads/temp/';         form.keepextensions = true;         form.parse(request, function (err, fields, files) {             type = files['upload']['type'];             filename = files['upload']['name'];             oldpath = files['upload']['path'];             newpath = './content/uploads/finished/' + filename;         });          form.on('end', function () {             fs.rename(oldpath, newpath, function (err) {                 if (err) {                     response.end('there error request');                     console.log('error')                 } else {                     response.end('<h1>thanks uploading ' + filename + '<h1>');                 }             });         });     } }).listen(port); 

here python download script having issues.

import json import urllib.request  url = '192.241.228.76:9090' def test():         response = urllib.request.urlopen('http://192.241.228.76:9090/feed')         readd = response.read()         data = json.loads(readd.decode(response.info().get_param('charset', 'utf8')))         if "children" in data:             in range(len(data['children'])):                 kind = data['children'][i]['type']                 url = 'http://' + data['children'][i]['path']                 name = 'media/' + data['children'][i]['name']                 if kind in ('video', 'image'):                     try:                         urllib.request.urlretrieve(url, name)                     except:                         try:                             print('spaces in ' + url)                         except:                             print('weird in file name') test() 

applications shall able handle whitespaces.

in browser, change whitespace %20, example:

http://foo.bar/my%20cat.jpg 

though my%20cat might not pretty, in case can replace whitespace underscore (_) using:

filename.replace(new regexp(find, ' '), '_'); 

for python script, replace urllib.request.urlretrive.... with:

urllib.request.urlretrieve(urllib.quote(url + '\' + name)) 

don't forget add import urllib.quote


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -