Append the uploaded file’s name to the link

This commit is contained in:
Thomas Kolb 2020-10-07 22:11:32 +02:00
parent f69c1bcb8c
commit 45fa4b1195
1 changed files with 23 additions and 16 deletions

39
main.py
View File

@ -29,7 +29,8 @@ urls = (
'/p', 'Pastebin', '/p', 'Pastebin',
'/info', 'FileInfo', '/info', 'FileInfo',
'/cleanup', 'Cleanup', '/cleanup', 'Cleanup',
'/([a-zA-Z0-9_.-]*)', 'Pastebin' '/([a-zA-Z0-9_.-]*)', 'Pastebin',
'/([a-zA-Z0-9_-]+)/(.*)', 'Pastebin'
) )
class Greeter: class Greeter:
@ -85,6 +86,11 @@ class Pastebin:
pasteid = urlsafe_b64encode(m.digest()[0:9]).decode('ascii') pasteid = urlsafe_b64encode(m.digest()[0:9]).decode('ascii')
storeName = os.path.join(CONF_DATA_DIR, pasteid) 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 # get the mime type and extension from the data
mimeType, ext = self.detect_file_type(tempFileName) mimeType, ext = self.detect_file_type(tempFileName)
if not ext: if not ext:
@ -108,22 +114,23 @@ class Pastebin:
os.unlink(tempFileName); os.unlink(tempFileName);
# return the generated URL # return the generated URL
return "http://" + web.ctx.host + "/" + pasteid + ext + "\n" return "http://" + web.ctx.host + "/" + pasteid + "/" + basename + ext + "\n"
else: else:
return "Nothing uploaded." return "Nothing uploaded."
def GET(self, doc = None): def GET(self, paste_id = None, reqName=None):
if doc: if paste_id:
dotindex = doc.find(".") if reqName:
if dotindex != -1: fileName = reqName
fileName = doc[:dotindex]
fileExt = doc[dotindex+1:]
else: else:
fileName = doc fileName = paste_id
fileExt = None
dotpos = paste_id.rfind('.')
if dotpos >= 0:
paste_id = paste_id[:dotpos]
# set the content type from the user-specified file extension # 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:
if mimeType[0:4] == "text": if mimeType[0:4] == "text":
mimeType += "; charset=" + CONF_DEFAULT_CHARSET mimeType += "; charset=" + CONF_DEFAULT_CHARSET
@ -134,7 +141,7 @@ class Pastebin:
else: else:
web.header("Content-Type", CONF_DEFAULT_TYPE) web.header("Content-Type", CONF_DEFAULT_TYPE)
storeName = CONF_DATA_DIR + "/" + fileName storeName = CONF_DATA_DIR + "/" + paste_id
try: try:
f = open(storeName, "rb") f = open(storeName, "rb")
@ -144,27 +151,27 @@ class Pastebin:
# update the corresponding access time and count # update the corresponding access time and count
items = self.db.select(CONF_SQL_FILESTABLE, items = self.db.select(CONF_SQL_FILESTABLE,
where = "hash=" + web.db.sqlquote(fileName), where = "hash=" + web.db.sqlquote(paste_id),
what = "access_count") what = "access_count")
if items: if items:
oldcount = items[0]['access_count'] oldcount = items[0]['access_count']
self.db.update(CONF_SQL_FILESTABLE, self.db.update(CONF_SQL_FILESTABLE,
where = "hash=" + web.db.sqlquote(fileName), where = "hash=" + web.db.sqlquote(paste_id),
access_time = time.time(), access_time = time.time(),
access_count = oldcount + 1) access_count = oldcount + 1)
else: else:
# The count could not be read. # The count could not be read.
# Probably there is no entry for this file -> create one # Probably there is no entry for this file -> create one
self.db.insert(CONF_SQL_FILESTABLE, self.db.insert(CONF_SQL_FILESTABLE,
hash = fileName, hash = paste_id,
detected_type = mimeType, detected_type = mimeType,
create_time = os.path.getmtime(storeName), create_time = os.path.getmtime(storeName),
access_time = time.time(), access_time = time.time(),
access_count = 1) access_count = 1)
except IOError: except IOError:
yield "Cannot read \"" + fileName + "\"." yield "Cannot read \"" + paste_id + "\"."
else: else:
raise web.seeother("/") raise web.seeother("/")