-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
48 lines (36 loc) · 1.59 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
44
45
46
47
48
import os
from flask import Flask, request, jsonify, render_template, send_from_directory
def create_app(test_config=None):
app = Flask(__name__)
# Configuration
app.config['IMG_FOLDER'] = 'images'
# Ensure the folder exists
if not os.path.exists(app.config['IMG_FOLDER']):
os.makedirs(app.config['IMG_FOLDER'])
@app.route('/')
def home():
photo_count = len(os.listdir(app.config['IMG_FOLDER']))
photos = os.listdir(app.config['IMG_FOLDER'])
return render_template('index.html', photo_count=photo_count, photos=photos)
@app.route('/photo', methods=['POST'])
def upload_photo():
if 'image' not in request.files:
return jsonify({"error": "No image part in the request"}), 400
file = request.files['image']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file:
filepath = os.path.join(app.config['IMG_FOLDER'], file.filename)
file.save(filepath)
return jsonify({"message": "Image uploaded successfully!", "filepath": filepath}), 201
@app.route('/photo/<filename>', methods=['GET'])
def get_photo(filename):
return send_from_directory(app.config['IMG_FOLDER'], filename)
@app.route('/photo', methods=['GET'])
def list_photos():
files = os.listdir(app.config['IMG_FOLDER'])
return jsonify({"photos": files})
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)