-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
239 lines (198 loc) · 7.5 KB
/
app.py
File metadata and controls
239 lines (198 loc) · 7.5 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Box Storage - QR Code Inventory System
A simple web app to manage box contents via QR codes.
"""
import os
import io
import base64
from pathlib import Path
from datetime import datetime
from flask import Flask, render_template, request, redirect, url_for, send_file
from werkzeug.utils import secure_filename
import qrcode
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max upload
# Version info
VERSION = "1.0.0"
BUILD_DATE = "2026-01-30"
@app.context_processor
def inject_version():
return {'version': VERSION, 'build_date': BUILD_DATE}
# Configuration - use /data in production (Fly.io volume), local in dev
DATA_DIR = Path(os.environ.get('DATA_PATH', Path(__file__).parent))
BOXES_DIR = DATA_DIR / "boxes"
PHOTOS_DIR = DATA_DIR / "photos"
BOXES_DIR.mkdir(exist_ok=True)
PHOTOS_DIR.mkdir(exist_ok=True)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_box_photos(box_id):
"""Get all photos for a box."""
box_photos_dir = PHOTOS_DIR / box_id
if not box_photos_dir.exists():
return []
photos = []
for f in sorted(box_photos_dir.glob('*')):
if f.suffix.lower().lstrip('.') in ALLOWED_EXTENSIONS:
photos.append(f.name)
return photos
def get_all_boxes():
"""Get all box files sorted by modification time."""
boxes = []
for f in BOXES_DIR.glob("*.md"):
box_id = f.stem
content = f.read_text()
# Extract title from first line if it's a heading
lines = content.strip().split('\n')
title = lines[0].lstrip('# ').strip() if lines else box_id
boxes.append({
'id': box_id,
'title': title,
'modified': datetime.fromtimestamp(f.stat().st_mtime)
})
return sorted(boxes, key=lambda x: x['modified'], reverse=True)
def get_box_content(box_id):
"""Read box markdown file."""
box_file = BOXES_DIR / f"{box_id}.md"
if box_file.exists():
return box_file.read_text()
return None
def save_box_content(box_id, content):
"""Save box markdown file."""
box_file = BOXES_DIR / f"{box_id}.md"
box_file.write_text(content)
def generate_box_id():
"""Generate a new unique box ID."""
existing = {f.stem for f in BOXES_DIR.glob("*.md")}
counter = 1
while f"box-{counter:03d}" in existing:
counter += 1
return f"box-{counter:03d}"
def generate_qr_code(url):
"""Generate QR code as base64 image."""
qr = qrcode.QRCode(version=1, box_size=10, border=4)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buffer = io.BytesIO()
img.save(buffer, format='PNG')
buffer.seek(0)
return base64.b64encode(buffer.getvalue()).decode()
@app.route('/')
def index():
"""Home page - list all boxes."""
boxes = get_all_boxes()
return render_template('index.html', boxes=boxes)
@app.route('/box/<box_id>')
def view_box(box_id):
"""View a single box's contents."""
content = get_box_content(box_id)
if content is None:
# New box - create with template
content = f"# {box_id}\n\n## Contents\n\n- \n\n## Location\n\n\n## Notes\n\n"
save_box_content(box_id, content)
# Generate QR code URL
host = request.host_url.rstrip('/')
qr_url = f"{host}/box/{box_id}"
qr_base64 = generate_qr_code(qr_url)
# Get photos for this box
photos = get_box_photos(box_id)
return render_template('box.html', box_id=box_id, content=content, qr_base64=qr_base64, qr_url=qr_url, photos=photos)
@app.route('/box/<box_id>/edit', methods=['GET', 'POST'])
def edit_box(box_id):
"""Edit a box's contents."""
if request.method == 'POST':
content = request.form.get('content', '')
save_box_content(box_id, content)
return redirect(url_for('view_box', box_id=box_id))
content = get_box_content(box_id)
if content is None:
content = f"# {box_id}\n\n## Contents\n\n- \n\n## Location\n\n\n## Notes\n\n"
return render_template('edit.html', box_id=box_id, content=content)
@app.route('/box/<box_id>/qr')
def download_qr(box_id):
"""Download QR code as PNG."""
host = request.host_url.rstrip('/')
qr_url = f"{host}/box/{box_id}"
qr = qrcode.QRCode(version=1, box_size=10, border=4)
qr.add_data(qr_url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buffer = io.BytesIO()
img.save(buffer, format='PNG')
buffer.seek(0)
return send_file(buffer, mimetype='image/png', download_name=f'{box_id}-qr.png')
@app.route('/new')
def new_box():
"""Create a new box and redirect to edit."""
box_id = generate_box_id()
return redirect(url_for('edit_box', box_id=box_id))
@app.route('/search')
def search():
"""Search across all boxes."""
query = request.args.get('q', '').lower()
results = []
if query:
for f in BOXES_DIR.glob("*.md"):
content = f.read_text()
if query in content.lower():
box_id = f.stem
lines = content.strip().split('\n')
title = lines[0].lstrip('# ').strip() if lines else box_id
# Find matching lines for preview
matching_lines = [l.strip() for l in lines if query in l.lower()][:3]
results.append({
'id': box_id,
'title': title,
'preview': matching_lines
})
return render_template('search.html', query=query, results=results)
@app.route('/box/<box_id>/photos', methods=['POST'])
def upload_photo(box_id):
"""Upload a photo for a box."""
if 'photo' not in request.files:
return redirect(url_for('view_box', box_id=box_id))
file = request.files['photo']
if file.filename == '':
return redirect(url_for('view_box', box_id=box_id))
if file and allowed_file(file.filename):
# Create box photos directory
box_photos_dir = PHOTOS_DIR / box_id
box_photos_dir.mkdir(exist_ok=True)
# Generate unique filename with timestamp
ext = file.filename.rsplit('.', 1)[1].lower()
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"{timestamp}.{ext}"
file.save(box_photos_dir / filename)
return redirect(url_for('view_box', box_id=box_id))
@app.route('/box/<box_id>/photos/<filename>')
def get_photo(box_id, filename):
"""Serve a photo."""
photo_path = PHOTOS_DIR / box_id / secure_filename(filename)
if photo_path.exists():
return send_file(photo_path)
return "Not found", 404
@app.route('/box/<box_id>/photos/<filename>/delete', methods=['POST'])
def delete_photo(box_id, filename):
"""Delete a photo."""
photo_path = PHOTOS_DIR / box_id / secure_filename(filename)
if photo_path.exists():
photo_path.unlink()
return redirect(url_for('view_box', box_id=box_id))
@app.route('/box/<box_id>/delete', methods=['POST'])
def delete_box(box_id):
"""Delete a box and its photos."""
box_file = BOXES_DIR / f"{box_id}.md"
if box_file.exists():
box_file.unlink()
# Also delete photos
box_photos_dir = PHOTOS_DIR / box_id
if box_photos_dir.exists():
for photo in box_photos_dir.glob('*'):
photo.unlink()
box_photos_dir.rmdir()
return redirect(url_for('index'))
if __name__ == '__main__':
# Run on all interfaces so it's accessible on local network
app.run(host='0.0.0.0', port=5000, debug=True)