Moved filename generation to extra function

This commit is contained in:
Sebastian Lohff 2012-04-07 18:48:48 +02:00
parent d46cc10107
commit 06b983fe1d
1 changed files with 17 additions and 7 deletions

View File

@ -112,10 +112,8 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
if not "file" in fstorage: if not "file" in fstorage:
self.sendResponse(400, "No file found in request.") self.sendResponse(400, "No file found in request.")
return return
print dir(fstorage["file"]) destFileName = self.getTargetName(fstorage["file"].filename)
print fstorage["file"].filename, if destFileName == "":
cleanFileName = self.cleanFileName(fstorage["file"].filename)
if cleanFileName == "":
self.sendResponse(400, "Filename was empty") self.sendResponse(400, "Filename was empty")
return return
destFileName = self.targetDir + "/" + cleanFileName destFileName = self.targetDir + "/" + cleanFileName
@ -140,9 +138,21 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write(msg) self.wfile.write(msg)
def cleanFileName(self, fname): def getTargetName(self, fname):
return fname.replace("/", "") cleanFileName = fname.replace("/", "")
if cleanFileName == "":
return ""
destFileName = self.targetDir + "/" + cleanFileName
if not os.path.exists(destFileName):
return destFileName
else:
i = 1
extraDestFileName = destFileName + "(%s)" % i
while os.path.exists(extraDestFileName):
i += 1
extraDestFileName = destFileName + "(%s)" % i
return extraDestFileName
# never reached
class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass pass