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

39
main.py
View file

@ -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("/")