-
Notifications
You must be signed in to change notification settings - Fork 385
/
app.py
43 lines (31 loc) · 1.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask, request, Response
from werkzeug.utils import secure_filename
from db import db_init, db
from models import Img
app = Flask(__name__)
# SQLAlchemy config. Read more: https://flask-sqlalchemy.palletsprojects.com/en/2.x/
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:https:///img.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db_init(app)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/upload', methods=['POST'])
def upload():
pic = request.files['pic']
if not pic:
return 'No pic uploaded!', 400
filename = secure_filename(pic.filename)
mimetype = pic.mimetype
if not filename or not mimetype:
return 'Bad upload!', 400
img = Img(img=pic.read(), name=filename, mimetype=mimetype)
db.session.add(img)
db.session.commit()
return 'Img Uploaded!', 200
@app.route('/<int:id>')
def get_img(id):
img = Img.query.filter_by(id=id).first()
if not img:
return 'Img Not Found!', 404
return Response(img.img, mimetype=img.mimetype)