Misc fixes

This commit is contained in:
2024-02-24 18:03:37 +00:00
parent 10d603f1c6
commit 79e4aba172
+24 -24
View File
@@ -9,18 +9,18 @@ from pathlib import Path
from PIL import Image from PIL import Image
app = Flask(__name__) app = Flask(__name__)
PICS = Path('data') DATA_FOLDER = Path('data')
MAX_SIZE = 1920 MAX_SIZE = 1920
PORT = 5003 PORT = 5003
URL = 'https://pic.t0.vc' URL = 'https://pic.t0.vc'
POST = 'pic' POST_PARAM = 'pic'
MAX_ALLOWED_SIZE = 16 * 1024 * 1024 # 16 MB MAX_FILE_SIZE = 16 * 1024 * 1024 # 16 MB
def help(): def help():
form = ( form = (
f'<form action="{URL}" method="POST" accept-charset="UTF-8" enctype="multipart/form-data">' f'<form action="{URL}" method="POST" accept-charset="UTF-8" enctype="multipart/form-data">'
'<input name="web" type="hidden" value="true">' '<input name="web" type="hidden" value="true">'
f'<input name="{POST}" type="file" accept="image/*" />' f'<input name="{POST_PARAM}" type="file" accept="image/*" />'
'<br><br><button type="submit">Submit</button></form>' '<br><br><button type="submit">Submit</button></form>'
) )
return f""" return f"""
@@ -30,7 +30,7 @@ NAME
t0pic: command line image host. t0pic: command line image host.
USAGE USAGE
&lt;image output&gt; | curl -F '{POST}=@/dev/stdin' {URL} &lt;image output&gt; | curl -F '{POST_PARAM}=@/dev/stdin' {URL}
or upload from the web: or upload from the web:
{form} {form}
@@ -43,15 +43,15 @@ DESCRIPTION
Don't use this for anything serious Don't use this for anything serious
EXAMPLES EXAMPLES
~$ cat kitten.jpg | curl -F '{POST}=@/dev/stdin' {URL} ~$ cat kitten.jpg | curl -F '{POST_PARAM}=@/dev/stdin' {URL}
{URL}/YXKV.jpg {URL}/YXKV.jpg
~$ firefox {URL}/YXKV.jpg ~$ firefox {URL}/YXKV.jpg
Add this to your .bashrc: Add this to your .bashrc:
alias {POST}="curl -F '{POST}=@/dev/stdin' {URL}" alias {POST_PARAM}="curl -F '{POST_PARAM}=@/dev/stdin' {URL}"
Now you can pipe directly into {POST}! Now you can pipe directly into {POST_PARAM}!
SOURCE CODE SOURCE CODE
https://txt.t0.vc/CQQE https://txt.t0.vc/CQQE
@@ -72,7 +72,7 @@ def paste():
image.src = url; image.src = url;
const form = new FormData(); const form = new FormData();
form.append('{POST}', file); form.append('{POST_PARAM}', file);
fetch('/', {{ fetch('/', {{
method: 'POST', method: 'POST',
body: form body: form
@@ -94,25 +94,25 @@ def index():
def new(): def new():
try: try:
if 'pic' not in request.files or request.files['pic'].filename == '': if 'pic' not in request.files or request.files['pic'].filename == '':
print("No file part") print('No file part')
abort(400) # No file was uploaded abort(400)
pic = request.files['pic'] pic = request.files['pic']
if pic.mimetype not in ['image/png', 'image/jpeg', 'image/gif', 'application/octet-stream']: if pic.mimetype not in ['image/png', 'image/jpeg', 'image/gif', 'application/octet-stream']:
print(f"Unsupported image format: {pic.mimetype}") print(f'Unsupported image format: {pic.mimetype}')
abort(400) # Consider using a more specific status code or returning an error message abort(400)
if pic.seek(0, 2) > MAX_ALLOWED_SIZE: # Move to end to get file size if pic.seek(0, 2) > MAX_FILE_SIZE: # Move to end to get file size
print(f"File size exceeds maximum allowed limit: {pic.tell()} bytes") print(f'File size exceeds maximum allowed limit: {pic.tell()} bytes')
abort(413) # Payload Too Large abort(413)
pic.seek(0) # Reset file pointer pic.seek(0) # Reset file pointer
nid = new_id() nid = new_id()
while nid in [p.stem for p in PICS.iterdir()]: while nid in [p.stem for p in DATA_FOLDER.iterdir()]:
nid = new_id() nid = new_id()
pic = Image.open(pic.stream) # Use .stream to read directly from Flask file storage pic = Image.open(pic.stream)
if pic.format == 'PNG': if pic.format == 'PNG':
ext = '.png' ext = '.png'
@@ -121,12 +121,12 @@ def new():
elif pic.format == 'GIF': elif pic.format == 'GIF':
ext = '.gif' ext = '.gif'
else: else:
print(f"Unsupported image format: {pic.format}") print(f'Unsupported image format: {pic.format}')
abort(400) abort(400)
filename = nid + ext filename = nid + ext
pic.thumbnail([MAX_SIZE, MAX_SIZE], Image.Resampling.LANCZOS) pic.thumbnail([MAX_SIZE, MAX_SIZE], Image.Resampling.LANCZOS)
pic.save(PICS / filename) # Use pathlib's / operator for path concatenation pic.save(DATA_FOLDER / filename)
print(f'Adding pic {nid}{ext}') print(f'Adding pic {nid}{ext}')
@@ -136,10 +136,10 @@ def new():
else: else:
return f'{url}\n' return f'{url}\n'
except Exception as e: except Exception as e:
print(f"Error processing image upload: {e}") print(f'Error processing image upload: {e}')
abort(400) abort(400)
if __name__ == '__main__': if __name__ == '__main__':
if not PICS.exists(): if not DATA_FOLDER.exists():
PICS.mkdir(parents=True) DATA_FOLDER.mkdir(parents=True)
app.run(port=PORT, debug=False) # Turn off debug for production app.run(port=PORT, debug=False)