From 32ddaa081c9f220dedd2e4647b8b576d1dcedea8 Mon Sep 17 00:00:00 2001 From: Bryce Handerson Date: Fri, 29 Apr 2016 16:28:51 -0700 Subject: [PATCH] return the url also --- pbnh/app/util.py | 5 ++--- pbnh/app/views.py | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pbnh/app/util.py b/pbnh/app/util.py index ae6d26d..66fd179 100644 --- a/pbnh/app/util.py +++ b/pbnh/app/util.py @@ -1,5 +1,4 @@ import io -import json import magic import mimetypes @@ -29,7 +28,7 @@ def fileData(files, addr=None, sunset=None, mimestr=None): username=config.get('username')) as pstr: j = pstr.create(data, mime=mime, ip=addr, sunset=sunset) - return json.dumps(j) + return j except IOError as e: return 'caught exception in filedata' + str(e) return 'File save error' @@ -41,7 +40,7 @@ def stringData(inputstr, addr=None, sunset=None, mime=None): password=config.get('password'), port=config.get('port'), username=config.get('username')) as pstr: j = pstr.create(inputstr.encode('utf-8'), mime=mime, ip=addr, sunset=sunset) - return json.dumps(j) + return j return 'String save error' def getSunsetFromStr(sunsetstr): diff --git a/pbnh/app/views.py b/pbnh/app/views.py index 0f41787..d1c121a 100644 --- a/pbnh/app/views.py +++ b/pbnh/app/views.py @@ -1,5 +1,6 @@ import io import re +import json from datetime import datetime, timezone, timedelta from docutils.core import publish_parts @@ -38,22 +39,28 @@ def post_paste(): sunset = util.getSunsetFromStr(sunsetstr) redirectstr = request.form.get('r') or request.form.get('redirect') if redirectstr: - return util.stringData(redirectstr, addr=addr, sunset=sunset, - mime='redirect'), 201 + j = util.stringData(redirectstr, addr=addr, sunset=sunset, mime='redirect') + if j: + j['link'] = request.url + str(j.get('id')) + return json.dumps(j), 201 inputstr = request.form.get('content') or request.form.get('c') # we got string data if inputstr and isinstance(inputstr, str): try: - return util.stringData(inputstr, addr=addr, sunset=sunset, - mime=mimestr), 201 + j = util.stringData(inputstr, addr=addr, sunset=sunset, mime=mimestr) + if j: + j['link'] = request.url + str(j.get('id')) + return json.dumps(j), 201 except (exc.OperationalError, exc.InternalError): abort(500) files = request.files.get('content') or request.files.get('c') # we got file data if files and isinstance(files, FileStorage): try: - return util.fileData(files, addr=addr, sunset=sunset, - mimestr=mimestr), 201 + j = util.fileData(files, addr=addr, sunset=sunset, mimestr=mimestr) + if j: + j['link'] = request.url + str(j.get('id')) + return json.dumps(j), 201 except (exc.OperationalError, exc.InternalError): abort(500) abort(400)