gpt4 book ai didi

regex - 文件名中包含空格的上传导致我的服务器出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:44 25 4
gpt4 key购买 nike

我有一个服务器,用于允许人们上传文件。不幸的是,我的脚本允许人们上传文件名中带有空格的文件。当生成这些文件的链接时(也是由我的脚本生成),文件名将带有这些空格输出。然后我的 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()

最佳答案

应用程序应能够处理空格。

在浏览器中,将空格更改为 %20,例如:

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

虽然 my%20cat 可能看起来不太漂亮,但在这种情况下,您可以使用下划线 (_) 替换空格:

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

对于 python 脚本,将 urllib.request.urlretrive.... 替换为:

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

不要忘记添加import urllib.quote

关于regex - 文件名中包含空格的上传导致我的服务器出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24424728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com