-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
300 lines (243 loc) · 8.86 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import math
from flask import (
Flask,
render_template,
request,
redirect,
url_for,
send_from_directory,
flash,
jsonify,
g,
)
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SelectField, SubmitField
from wtforms.validators import DataRequired
from flask_paginate import Pagination
from werkzeug.security import generate_password_hash, check_password_hash
from wand.image import Image
UPLOAD_FOLDER = "library"
APP_KEY = os.environ.get("DOCKER_PDF_SERVER_KEY", "super_secret_key")
APP_USER = os.environ.get("DOCKER_PDF_SERVER_USER", "admin")
APP_PASSWORD = os.environ.get("DOCKER_PDF_SERVER_PASSWORD", "password")
ALLOWED_EXTENSIONS = {"pdf"}
app = Flask(__name__)
auth = HTTPBasicAuth()
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = APP_KEY
db = SQLAlchemy(app)
app.app_context().push()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
role = db.Column(db.String(20), nullable=False)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class UserForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
role = SelectField(
"Role",
choices=[
("reader", "Reader"),
("admin", "Admin"),
("maintainer", "Maintainer"),
],
validators=[DataRequired()],
)
submit = SubmitField("Add User")
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
@auth.verify_password
def verify_password(username, password):
if username == APP_USER and password == APP_PASSWORD:
g.current_user = User(username=username, role="admin")
return g.current_user
user = User.query.filter_by(username=username).first()
if user and user.check_password(password):
g.current_user = user
return g.current_user
return None
@auth.error_handler
def unauthorized():
return jsonify({"error": "Unauthorized"}), 401
@app.before_request
def before_request():
g.current_user = auth.current_user()
@app.errorhandler(500)
def internal_server_error():
return (
jsonify(
{
"error": "Sorry, the app encountered a 500 internal server error. It just doesn't like you today."
}
),
500,
)
@app.route("/")
@auth.login_required
def index():
page = request.args.get("page", 1, type=int)
per_page = 12
upload_folder = app.config["UPLOAD_FOLDER"]
pdf_files = [file for file in os.listdir(upload_folder) if file.endswith(".pdf")]
total_files = len(pdf_files)
final_page = math.ceil(total_files / per_page)
start = (page - 1) * per_page
end = start + per_page
pdf_files_slice = pdf_files[start:end] if start < total_files else []
pagination = Pagination(page=page, per_page=per_page, total=total_files)
pdf_files_with_thumbnails = []
for file in pdf_files_slice:
thumbnail_path = f"{file}.png"
if not os.path.exists(os.path.join(upload_folder, f"{file}.png")):
thumbnail_path = "pdf-file.png"
pdf_files_with_thumbnails.append({"file": file, "thumbnail": thumbnail_path})
return render_template(
"index.html",
files=pdf_files_with_thumbnails,
pagination=pagination,
last=final_page,
)
@app.route("/search")
@auth.login_required
def search():
query = request.args.get("query", "")
page = request.args.get("page", 1, type=int)
per_page = 12
limit_exceeded = False
upload_folder = app.config["UPLOAD_FOLDER"]
if query:
query = query.strip()
pdf_files = [
file
for file in os.listdir(upload_folder)
if file.endswith(".pdf") and query.lower() in file.lower()
]
total_files = len(pdf_files)
final_page = math.ceil(total_files / per_page)
if total_files > 20:
limit_exceeded = True
start = (page - 1) * per_page
end = start + per_page
pdf_files_slice = pdf_files[start:end] if start < total_files else []
pagination = Pagination(page=page, per_page=per_page, total=total_files)
pdf_files_with_thumbnails = []
for file in pdf_files_slice:
thumbnail_path = f"{file}.png"
if not os.path.exists(os.path.join(upload_folder, f"{file}.png")):
thumbnail_path = "pdf-file.png"
pdf_files_with_thumbnails.append({"file": file, "thumbnail": thumbnail_path})
return render_template(
"index.html",
files=pdf_files_with_thumbnails,
query=query,
pagination=pagination,
last=final_page,
limit_exceeded=limit_exceeded,
)
@app.route("/upload", methods=["POST"])
@auth.login_required
def upload_file():
if g.current_user.role not in ["admin", "maintainer"]:
flash("Unauthorized access!", "danger")
return redirect(url_for("index"))
if "file" not in request.files:
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
return redirect(request.url)
if file and allowed_file(file.filename):
filename = file.filename
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(file_path)
try:
thumbnail_path = os.path.join(
app.config["UPLOAD_FOLDER"], filename + ".png"
)
with Image(filename=file_path + "[0]", resolution=100) as img:
img.format = "png"
img.save(filename=thumbnail_path)
except Exception:
error_message = "An error occurred while processing your request. Could not generate thumbnail. The PDF uploaded maybe malformed. You may still view it if your client supports it."
return render_template("error.html", error_message=error_message)
return redirect(url_for("index"))
@app.route("/library/<filename>")
@auth.login_required
def uploaded_file(filename):
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
@app.route("/delete", methods=["POST"])
@auth.login_required
def delete_file():
if g.current_user.role not in ["admin", "maintainer"]:
flash("Unauthorized access!", "danger")
return redirect(url_for("index"))
filename = request.form["filename"]
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
if os.path.exists(file_path):
os.remove(file_path)
thumbnail_path = os.path.join(app.config["UPLOAD_FOLDER"], filename + ".png")
if os.path.exists(thumbnail_path):
os.remove(thumbnail_path)
flash("File deleted successfully", "success")
else:
flash("File not found", "error")
return redirect(url_for("index"))
@app.route("/admin", methods=["GET", "POST"])
@auth.login_required
def admin():
if g.current_user.role != "admin":
flash("Unauthorized access!", "danger")
return redirect(url_for("index"))
form = UserForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
role = form.role.data
if User.query.filter_by(username=username).first():
flash("Username already exists", "danger")
else:
new_user = User(username=username, role=role)
new_user.set_password(password)
db.session.add(new_user)
db.session.commit()
flash("User added successfully", "success")
return redirect(url_for("admin"))
users = User.query.all()
return render_template("admin.html", form=form, users=users)
@app.route("/delete_user", methods=["POST"])
@auth.login_required
def delete_user():
if g.current_user.role != "admin":
flash("Unauthorized access!", "danger")
return redirect(url_for("index"))
user_id = request.form["user_id"]
user = User.query.get(user_id)
if user:
db.session.delete(user)
db.session.commit()
flash("User deleted successfully", "success")
else:
flash("User not found", "error")
return redirect(url_for("admin"))
@app.route("/favicon.ico")
def favicon():
return send_from_directory(
os.path.join(app.root_path, "static"),
"favicon.ico",
mimetype="image/vnd.microsoft.icon",
)
if __name__ == "__main__":
db.create_all()
app.run(port=3030, debug=False)
else:
with app.app_context():
db.create_all()