diff --git a/app.py b/app.py index 18b2b3a..0d68c16 100644 --- a/app.py +++ b/app.py @@ -19,6 +19,7 @@ socketio = SocketIO(app) app.config['UPLOAD_FOLDER'] = 'images' # Folder to store images app.config['DUMP_FOLDER'] = 'dump_images' app.config['CACHE_FOLDER'] = 'cache' # Folder to store cached resized images +app.config['DUMP_CACHE_FOLDER'] = 'dump_cache' app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'} USERNAME = config['login_info']['username'] @@ -44,6 +45,30 @@ def resize_image(image_path, cache_path, size=(800, 450)): img = img.convert("RGB") img.save(cache_path, format="JPEG") +def delete_non_existing_cache(): + cache_folder = app.config['CACHE_FOLDER'] + upload_folder = app.config['UPLOAD_FOLDER'] + dump_cache_folder = app.config['DUMP_CACHE_FOLDER'] + dump_folder = app.config['DUMP_FOLDER'] + + images = os.listdir(cache_folder) + for image in images: + image_path_upload = os.path.join(upload_folder, image) + cache_path = os.path.join(cache_folder, image) + + if not os.path.exists(image_path_upload): + if os.path.exists(cache_path): + os.remove(cache_path) + + dump_images = os.listdir(dump_cache_folder) + for image in dump_images: + image_path_dump = os.path.join(dump_folder, image) + dump_cache_path = os.path.join(dump_cache_folder, image) + + if not os.path.exists(image_path_dump): + if os.path.exists(dump_cache_path): + os.remove(dump_cache_path) + def create_cache(): images = os.listdir(app.config['UPLOAD_FOLDER']) cached_images = [] @@ -51,18 +76,47 @@ def create_cache(): if not os.path.exists(app.config['CACHE_FOLDER']): os.makedirs(app.config['CACHE_FOLDER']) - for image in images: - image_path = os.path.join(app.config['UPLOAD_FOLDER'], image) - cache_path = os.path.join(app.config['CACHE_FOLDER'], image) - - if not os.path.exists(cache_path) and is_image_valid(image_path): - resize_image(image_path, cache_path) + caches_path = set([os.path.join(app.config['CACHE_FOLDER'], image) for image in os.listdir(app.config['CACHE_FOLDER'])]) + images_path = set([os.path.join(app.config['UPLOAD_FOLDER'], image) for image in images]) + # get the images that are not cached + uncached_images = images_path - caches_path + for image_path in uncached_images: if is_image_valid(image_path): - cached_images.append(image) + cache_path = os.path.join(app.config['CACHE_FOLDER'], os.path.basename(image_path)) + resize_image(image_path, cache_path) + cached_images.append(os.path.basename(image_path)) return cached_images +def create_dump_cache(): + images = os.listdir(app.config['DUMP_FOLDER']) + cached_images = [] + + if not os.path.exists(app.config['DUMP_CACHE_FOLDER']): + os.makedirs(app.config['DUMP_CACHE_FOLDER']) + + caches_path = set([os.path.join(app.config['DUMP_CACHE_FOLDER'], image) for image in os.listdir(app.config['DUMP_CACHE_FOLDER'])]) + images_path = set([os.path.join(app.config['DUMP_FOLDER'], image) for image in images]) + + # get the images that are not cached + uncached_images = images_path - caches_path + for image_path in uncached_images: + if is_image_valid(image_path): + cache_path = os.path.join(app.config['DUMP_CACHE_FOLDER'], os.path.basename(image_path)) + resize_image(image_path, cache_path) + cached_images.append(os.path.basename(image_path)) + + return cached_images + +def update_cache(): + create_cache() + delete_non_existing_cache() + +def update_dump_cache(): + create_dump_cache() + delete_non_existing_cache() + @app.route('/') def index(): return redirect(url_for('login')) @@ -91,6 +145,7 @@ def gallery(): return redirect(url_for('login')) cached_images = create_cache() + cached_images.sort() batches = [] current_batch = [] current_batch_number = None @@ -112,6 +167,9 @@ def gallery(): if current_batch: batches.append(current_batch) + + batches = batches[::-1] + if unstructured_images: batches.append(unstructured_images) @@ -127,6 +185,17 @@ def uploaded_file(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) else: return "Invalid image", 404 + +@app.route('/dump_images/') +def uploaded_dump_file(filename): + if not session.get('logged_in'): + return redirect(url_for('login')) + + image_path = os.path.join(app.config['DUMP_FOLDER'], filename) + if is_image_valid(image_path): + return send_from_directory(app.config['DUMP_FOLDER'], filename) + else: + return "Invalid image", 404 @app.route('/cached/') def cached_file(filename): @@ -139,16 +208,24 @@ def cached_file(filename): else: return "Invalid image", 404 +@app.route('/dump_cached/') +def dump_cached_file(filename): + if not session.get('logged_in'): + return redirect(url_for('login')) + + image_path = os.path.join(app.config['DUMP_CACHE_FOLDER'], filename) + if is_image_valid(image_path): + return send_from_directory(app.config['DUMP_CACHE_FOLDER'], filename) + else: + return "Invalid image", 404 + @app.route('/api/images') def api_images(): if not session.get('logged_in'): return jsonify([]) - images = sorted( - os.listdir(app.config['UPLOAD_FOLDER']), - key=lambda x: os.path.getctime(os.path.join(app.config['UPLOAD_FOLDER'], x)), - reverse=True - ) + images = sorted(os.listdir(app.config['UPLOAD_FOLDER']), reverse=True) + valid_images = [img for img in images if is_image_valid(os.path.join(app.config['UPLOAD_FOLDER'], img))] return jsonify(valid_images) @@ -157,42 +234,51 @@ def dump(): if not session.get('logged_in'): return redirect(url_for('login')) - dump_folder = app.config['DUMP_FOLDER'] # Folder to store dump images - images = sorted( - os.listdir(dump_folder), - key=lambda x: os.path.getctime(os.path.join(dump_folder, x)), - reverse=True - ) - valid_images = [img for img in images if is_image_valid(os.path.join(dump_folder, img))] + cached_images = create_dump_cache() + cached_images.sort(reverse=True) + + valid_images = [img for img in cached_images if is_image_valid(os.path.join(app.config['DUMP_FOLDER'], img))] return render_template('dump.html', images=valid_images) def emit_gallery_update(): socketio.emit('update_gallery') -class Watcher(FileSystemEventHandler): - def on_modified(self, event): - if not event.is_directory and event.src_path.startswith(app.config['UPLOAD_FOLDER']): - create_cache() - emit_gallery_update() +def emit_dump_update(): + socketio.emit('update_dump') +class Watcher(FileSystemEventHandler): def on_created(self, event): if not event.is_directory and event.src_path.startswith(app.config['UPLOAD_FOLDER']): - create_cache() + print(f"File created: {event.src_path}") + update_cache() emit_gallery_update() + if not event.is_directory and event.src_path.startswith(app.config['DUMP_FOLDER']): + print(f"Dump file created: {event.src_path}") + update_dump_cache() + emit_dump_update() + def on_deleted(self, event): if not event.is_directory and event.src_path.startswith(app.config['UPLOAD_FOLDER']): - create_cache() + print(f"File deleted: {event.src_path}") + update_cache() emit_gallery_update() + if not event.is_directory and event.src_path.startswith(app.config['DUMP_FOLDER']): + print(f"Dump file deleted: {event.src_path}") + update_dump_cache() + emit_dump_update() + @socketio.on('connect') def handle_connect(): if not session.get('logged_in'): disconnect() if __name__ == '__main__': + event_handler = Watcher() observer = Observer() - observer.schedule(Watcher(), path=app.config['UPLOAD_FOLDER'], recursive=False) + observer.schedule(event_handler, path=app.config['UPLOAD_FOLDER'], recursive=False) + observer.schedule(event_handler, path=app.config['DUMP_FOLDER'], recursive=False) observer.start() try: context = ('cert.pem', 'key.pem') # Replace with your self-signed certificate and key files diff --git a/cache/97144634_p0.png b/cache/97144634_p0.png deleted file mode 100644 index 7b44c2a..0000000 Binary files a/cache/97144634_p0.png and /dev/null differ diff --git a/cache/nextsec.png b/cache/nextsec.png deleted file mode 100644 index 6db0406..0000000 Binary files a/cache/nextsec.png and /dev/null differ diff --git a/cache/unnamed.jpg b/cache/unnamed.jpg deleted file mode 100644 index 5176e36..0000000 Binary files a/cache/unnamed.jpg and /dev/null differ diff --git a/cache/5月壁紙.png b/cache/5月壁紙.png deleted file mode 100644 index 05f28fb..0000000 Binary files a/cache/5月壁紙.png and /dev/null differ diff --git a/dump_cache/螢幕截圖_20240424_231141.png b/dump_cache/螢幕截圖_20240424_231141.png new file mode 100644 index 0000000..1a019ef Binary files /dev/null and b/dump_cache/螢幕截圖_20240424_231141.png differ diff --git a/dump_cache/螢幕截圖_20240424_231239.png b/dump_cache/螢幕截圖_20240424_231239.png new file mode 100644 index 0000000..a9c26e9 Binary files /dev/null and b/dump_cache/螢幕截圖_20240424_231239.png differ diff --git a/dump_cache/螢幕截圖_20240424_231326.png b/dump_cache/螢幕截圖_20240424_231326.png new file mode 100644 index 0000000..8f61040 Binary files /dev/null and b/dump_cache/螢幕截圖_20240424_231326.png differ diff --git a/dump_cache/螢幕截圖_20240430_021336.png b/dump_cache/螢幕截圖_20240430_021336.png new file mode 100644 index 0000000..eda096b Binary files /dev/null and b/dump_cache/螢幕截圖_20240430_021336.png differ diff --git a/dump_cache/螢幕截圖_20240430_021453.png b/dump_cache/螢幕截圖_20240430_021453.png new file mode 100644 index 0000000..055a745 Binary files /dev/null and b/dump_cache/螢幕截圖_20240430_021453.png differ diff --git a/dump_cache/螢幕截圖_20240430_021713.png b/dump_cache/螢幕截圖_20240430_021713.png new file mode 100644 index 0000000..e6fc503 Binary files /dev/null and b/dump_cache/螢幕截圖_20240430_021713.png differ diff --git a/dump_cache/螢幕截圖_20240430_022407.png b/dump_cache/螢幕截圖_20240430_022407.png new file mode 100644 index 0000000..a151f71 Binary files /dev/null and b/dump_cache/螢幕截圖_20240430_022407.png differ diff --git a/dump_cache/螢幕截圖_20240630_144555.png b/dump_cache/螢幕截圖_20240630_144555.png new file mode 100644 index 0000000..6a9c9e9 Binary files /dev/null and b/dump_cache/螢幕截圖_20240630_144555.png differ diff --git a/dump_cache/螢幕截圖_20240630_144616.png b/dump_cache/螢幕截圖_20240630_144616.png new file mode 100644 index 0000000..43fb803 Binary files /dev/null and b/dump_cache/螢幕截圖_20240630_144616.png differ diff --git a/dump_cache/螢幕截圖_20240630_144627.png b/dump_cache/螢幕截圖_20240630_144627.png new file mode 100644 index 0000000..003c4c3 Binary files /dev/null and b/dump_cache/螢幕截圖_20240630_144627.png differ diff --git a/dump_images/螢幕截圖_20240424_231141.png b/dump_images/螢幕截圖_20240424_231141.png new file mode 100644 index 0000000..f2d3997 Binary files /dev/null and b/dump_images/螢幕截圖_20240424_231141.png differ diff --git a/dump_images/螢幕截圖_20240424_231239.png b/dump_images/螢幕截圖_20240424_231239.png new file mode 100644 index 0000000..3e5005e Binary files /dev/null and b/dump_images/螢幕截圖_20240424_231239.png differ diff --git a/dump_images/螢幕截圖_20240424_231326.png b/dump_images/螢幕截圖_20240424_231326.png new file mode 100644 index 0000000..e6f1f97 Binary files /dev/null and b/dump_images/螢幕截圖_20240424_231326.png differ diff --git a/dump_images/螢幕截圖_20240430_021336.png b/dump_images/螢幕截圖_20240430_021336.png new file mode 100644 index 0000000..898e002 Binary files /dev/null and b/dump_images/螢幕截圖_20240430_021336.png differ diff --git a/dump_images/螢幕截圖_20240430_021453.png b/dump_images/螢幕截圖_20240430_021453.png new file mode 100644 index 0000000..c2d58da Binary files /dev/null and b/dump_images/螢幕截圖_20240430_021453.png differ diff --git a/dump_images/螢幕截圖_20240430_021713.png b/dump_images/螢幕截圖_20240430_021713.png new file mode 100644 index 0000000..3f2e671 Binary files /dev/null and b/dump_images/螢幕截圖_20240430_021713.png differ diff --git a/dump_images/螢幕截圖_20240430_022407.png b/dump_images/螢幕截圖_20240430_022407.png new file mode 100644 index 0000000..f83a70f Binary files /dev/null and b/dump_images/螢幕截圖_20240430_022407.png differ diff --git a/dump_images/螢幕截圖_20240630_144555.png b/dump_images/螢幕截圖_20240630_144555.png new file mode 100644 index 0000000..188779c Binary files /dev/null and b/dump_images/螢幕截圖_20240630_144555.png differ diff --git a/dump_images/螢幕截圖_20240630_144616.png b/dump_images/螢幕截圖_20240630_144616.png new file mode 100644 index 0000000..ed4ebf4 Binary files /dev/null and b/dump_images/螢幕截圖_20240630_144616.png differ diff --git a/dump_images/螢幕截圖_20240630_144627.png b/dump_images/螢幕截圖_20240630_144627.png new file mode 100644 index 0000000..0781db0 Binary files /dev/null and b/dump_images/螢幕截圖_20240630_144627.png differ diff --git a/images/97144634_p0.png b/images/97144634_p0.png deleted file mode 100644 index f2d2ba7..0000000 Binary files a/images/97144634_p0.png and /dev/null differ diff --git a/images/nextsec.png b/images/nextsec.png deleted file mode 100644 index afeb81e..0000000 Binary files a/images/nextsec.png and /dev/null differ diff --git a/images/unnamed.jpg b/images/unnamed.jpg deleted file mode 100644 index e543a7a..0000000 Binary files a/images/unnamed.jpg and /dev/null differ diff --git a/images/5月壁紙.png b/images/5月壁紙.png deleted file mode 100644 index 8f24194..0000000 Binary files a/images/5月壁紙.png and /dev/null differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c6cbada --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask +requests +Pillow +duckduckgo_search diff --git a/static/login_styles.css b/static/login_styles.css index 0f10a24..6574788 100644 --- a/static/login_styles.css +++ b/static/login_styles.css @@ -16,6 +16,7 @@ form h1 { } form { background-color: #fff; + margin: 20px; padding: 30px; border-radius: 10px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); diff --git a/templates/dump.html b/templates/dump.html index 5b2286b..9ab8439 100644 --- a/templates/dump.html +++ b/templates/dump.html @@ -1,9 +1,24 @@ + Pumpkin Gallery🎃 + +
@@ -13,10 +28,39 @@

Pumpkin Gallery🎃

+ + \ No newline at end of file diff --git a/templates/gallery.html b/templates/gallery.html index 90c974d..9e9fd13 100644 --- a/templates/gallery.html +++ b/templates/gallery.html @@ -1,6 +1,7 @@ + Fencing Gallery🤺 @@ -35,111 +36,8 @@