我有一个服务器,用于允许人们上传文件。不幸的是,我的脚本允许人们上传文件名中带有空格的文件。当生成指向这些文件的链接时(也由我的脚本生成),文件名将与这些空格一起输出。然后我的 Python 脚本(也附上)无法下载这些文件。我的问题显然是如何最好地解决这个问题。我不确定是否是:
A:使用某种正则表达式在上传时更改文件名,或者
B:如果有一种方法可以引用我服务器上的实际文件名(我什至不知道,因为当我在其中 ssh 时,看起来正确的文件路径实际上确实有空格)。
我对这个问题的最佳解决方案非常感兴趣。谢谢。
这是我的服务器代码:
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 a = 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 was an error with your request');
console.log('error')
} else {
response.end('<h1>Thanks for uploading ' + fileName + '<h1>');
}
});
});
}
}).listen(port);
这是有问题的 Python 下载脚本。
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:
for i 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()