Support pasting image directly

This commit is contained in:
2020-06-26 04:50:45 +00:00
parent ae032bcac7
commit ec9dd1168c
+24 -2
View File
@@ -33,6 +33,8 @@ USAGE
{2} {2}
or paste the image into this page.
DESCRIPTION DESCRIPTION
I got sick of imgur not working on mobile, so I built this I got sick of imgur not working on mobile, so I built this
Images are resized to a max dimension of {3} px Images are resized to a max dimension of {3} px
@@ -59,6 +61,26 @@ SEE ALSO
https://url.t0.vc https://url.t0.vc
</pre>""".format(POST, URL, form, MAX_SIZE) </pre>""".format(POST, URL, form, MAX_SIZE)
def paste():
return """
window.addEventListener('paste', e => {
const file = e.clipboardData.items[0].getAsFile();
const url = URL.createObjectURL(file);
const image = new Image();
image.src = url;
const form = new FormData();
form.append('pic', file);
fetch('/', {
method: 'POST',
body: form
})
.then(r => r.text())
.then(u => window.location = u)
.catch(e => alert(e.message));
});
"""
def new_id(): def new_id():
return ''.join(random.choice(string.ascii_uppercase) for _ in range(4)) return ''.join(random.choice(string.ascii_uppercase) for _ in range(4))
@@ -67,7 +89,7 @@ flask_app = Flask(__name__)
@flask_app.route('/', methods=['GET']) @flask_app.route('/', methods=['GET'])
def index(): def index():
return '<html><body>{}</body></html>'.format(help()) return '<html><script>{}</script><body>{}</body></html>'.format(paste(), help())
@flask_app.route('/', methods=['POST']) @flask_app.route('/', methods=['POST'])
def new(): def new():
@@ -93,7 +115,7 @@ def new():
print('Adding pic {}'.format(nid)) print('Adding pic {}'.format(nid))
url = '{}/{}'.format(URL, filename) url = URL + '/' + nid
if 'web' in request.form: if 'web' in request.form:
return redirect(url) return redirect(url)
else: else: