From 45fa4b1195bccbd531cc3484ea1cd8c0085e7bde Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 7 Oct 2020 22:11:32 +0200 Subject: [PATCH] =?UTF-8?q?Append=20the=20uploaded=20file=E2=80=99s=20name?= =?UTF-8?q?=20to=20the=20link?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index fe61025..2e1baf1 100755 --- a/main.py +++ b/main.py @@ -29,7 +29,8 @@ urls = ( '/p', 'Pastebin', '/info', 'FileInfo', '/cleanup', 'Cleanup', - '/([a-zA-Z0-9_.-]*)', 'Pastebin' + '/([a-zA-Z0-9_.-]*)', 'Pastebin', + '/([a-zA-Z0-9_-]+)/(.*)', 'Pastebin' ) class Greeter: @@ -85,6 +86,11 @@ class Pastebin: pasteid = urlsafe_b64encode(m.digest()[0:9]).decode('ascii') storeName = os.path.join(CONF_DATA_DIR, pasteid) + basename = os.path.basename(x['content'].filename) + pos = basename.find('.') + if pos >= 0: + basename = basename[:pos] + # get the mime type and extension from the data mimeType, ext = self.detect_file_type(tempFileName) if not ext: @@ -108,22 +114,23 @@ class Pastebin: os.unlink(tempFileName); # return the generated URL - return "http://" + web.ctx.host + "/" + pasteid + ext + "\n" + return "http://" + web.ctx.host + "/" + pasteid + "/" + basename + ext + "\n" else: return "Nothing uploaded." - def GET(self, doc = None): - if doc: - dotindex = doc.find(".") - if dotindex != -1: - fileName = doc[:dotindex] - fileExt = doc[dotindex+1:] + def GET(self, paste_id = None, reqName=None): + if paste_id: + if reqName: + fileName = reqName else: - fileName = doc - fileExt = None + fileName = paste_id + + dotpos = paste_id.rfind('.') + if dotpos >= 0: + paste_id = paste_id[:dotpos] # set the content type from the user-specified file extension - mimeType, encoding = mimetypes.guess_type(doc); + mimeType, encoding = mimetypes.guess_type(fileName); if mimeType: if mimeType[0:4] == "text": mimeType += "; charset=" + CONF_DEFAULT_CHARSET @@ -134,7 +141,7 @@ class Pastebin: else: web.header("Content-Type", CONF_DEFAULT_TYPE) - storeName = CONF_DATA_DIR + "/" + fileName + storeName = CONF_DATA_DIR + "/" + paste_id try: f = open(storeName, "rb") @@ -144,27 +151,27 @@ class Pastebin: # update the corresponding access time and count items = self.db.select(CONF_SQL_FILESTABLE, - where = "hash=" + web.db.sqlquote(fileName), + where = "hash=" + web.db.sqlquote(paste_id), what = "access_count") if items: oldcount = items[0]['access_count'] self.db.update(CONF_SQL_FILESTABLE, - where = "hash=" + web.db.sqlquote(fileName), + where = "hash=" + web.db.sqlquote(paste_id), access_time = time.time(), access_count = oldcount + 1) else: # The count could not be read. # Probably there is no entry for this file -> create one self.db.insert(CONF_SQL_FILESTABLE, - hash = fileName, + hash = paste_id, detected_type = mimeType, create_time = os.path.getmtime(storeName), access_time = time.time(), access_count = 1) except IOError: - yield "Cannot read \"" + fileName + "\"." + yield "Cannot read \"" + paste_id + "\"." else: raise web.seeother("/")