Browse Source

initial commit

Kyle Perik 1 year ago
commit
977f0a7f61
3 changed files with 46 additions and 0 deletions
  1. 2 0
      imgs/.gitignore
  2. 43 0
      main.py
  3. 1 0
      templates/index.html

+ 2 - 0
imgs/.gitignore

@@ -0,0 +1,2 @@
1
+*
2
+!.gitignore

+ 43 - 0
main.py

@@ -0,0 +1,43 @@
1
+from flask import (
2
+    Flask, request, render_template, send_file,
3
+    redirect, url_for
4
+)
5
+
6
+import os
7
+
8
+app = Flask(__name__)
9
+app.debug = True
10
+
11
+@app.route('/')
12
+def index():
13
+    return render_template('index.html')
14
+
15
+def imgpath(p):
16
+    return os.path.join('imgs/', p)
17
+
18
+@app.route('/img')
19
+def imgs():
20
+    files = [
21
+        f for f in os.listdir('imgs/')
22
+        if os.path.isfile(imgpath(f))
23
+    ]
24
+    ordered = sorted(
25
+        files, key=lambda f: os.stat(imgpath(f)).st_mtime,
26
+        reverse=True
27
+    )
28
+    return '\n'.join([
29
+        f'<img src="img/{f}" style="width: 100%"/>'
30
+        for f in ordered
31
+    ])
32
+
33
+@app.route('/img/<path:path>')
34
+def img(path):
35
+    return send_file(os.path.join('imgs/', path))
36
+
37
+@app.route('/upload', methods=['POST'])
38
+def upload():
39
+    file = request.files['file']
40
+    file.save(os.path.join('imgs/', file.filename))
41
+    return redirect(url_for('imgs'))
42
+
43
+app.run()

+ 1 - 0
templates/index.html

@@ -0,0 +1 @@
1
+ <!doctype html>     <title>Upload new File</title>     <h1>Upload new File</h1>     <form action='/upload' method=post enctype=multipart/form-data>       <input type=file name=file>       <input type=submit value=Upload>     </form>