Compare commits

...

2 Commits

Author SHA1 Message Date
Kenny Cheng 8bad36271a not mixing dump file with cached file 2024-10-26 01:18:01 +08:00
Kenny Cheng 5706377a31 fix dump page display nothing 2024-10-26 00:41:10 +08:00
2 changed files with 32 additions and 9 deletions

21
app.py
View File

@ -6,6 +6,7 @@ from werkzeug.security import check_password_hash
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
from os.path import realpath, join
app = Flask(__name__) app = Flask(__name__)
@ -16,8 +17,8 @@ with open('db/keys.json', 'r') as f:
app.config['SECRET_KEY'] = config['key'] app.config['SECRET_KEY'] = config['key']
socketio = SocketIO(app) socketio = SocketIO(app)
app.config['UPLOAD_FOLDER'] = 'images' # Folder to store images app.config['UPLOAD_FOLDER'] = '/home/kenny/workspace/Image_Gen_Server/output/expo_postprocessed' # Folder to store images
app.config['DUMP_FOLDER'] = 'dump_images' app.config['DUMP_FOLDER'] = '/home/kenny/workspace/ComfyUI-halloween/output' # Folder to store dump images
app.config['CACHE_FOLDER'] = 'cache' # Folder to store cached resized images app.config['CACHE_FOLDER'] = 'cache' # Folder to store cached resized images
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'} app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}
@ -45,13 +46,12 @@ def resize_image(image_path, cache_path, size=(800, 450)):
img.save(cache_path, format="JPEG") img.save(cache_path, format="JPEG")
def create_cache(): def create_cache():
images = os.listdir(app.config['UPLOAD_FOLDER'])
cached_images = [] cached_images = []
if not os.path.exists(app.config['CACHE_FOLDER']): if not os.path.exists(app.config['CACHE_FOLDER']):
os.makedirs(app.config['CACHE_FOLDER']) os.makedirs(app.config['CACHE_FOLDER'])
for image in images: for image in os.listdir(app.config['UPLOAD_FOLDER']):
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image) image_path = os.path.join(app.config['UPLOAD_FOLDER'], image)
cache_path = os.path.join(app.config['CACHE_FOLDER'], image) cache_path = os.path.join(app.config['CACHE_FOLDER'], image)
@ -139,6 +139,17 @@ def cached_file(filename):
else: else:
return "Invalid image", 404 return "Invalid image", 404
@app.route('/dump/<filename>')
def 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('/api/images') @app.route('/api/images')
def api_images(): def api_images():
if not session.get('logged_in'): if not session.get('logged_in'):
@ -157,6 +168,7 @@ def dump():
if not session.get('logged_in'): if not session.get('logged_in'):
return redirect(url_for('login')) return redirect(url_for('login'))
#cached_images = create_cache()
dump_folder = app.config['DUMP_FOLDER'] # Folder to store dump images dump_folder = app.config['DUMP_FOLDER'] # Folder to store dump images
images = sorted( images = sorted(
os.listdir(dump_folder), os.listdir(dump_folder),
@ -164,6 +176,7 @@ def dump():
reverse=True reverse=True
) )
valid_images = [img for img in images if is_image_valid(os.path.join(dump_folder, img))] valid_images = [img for img in images if is_image_valid(os.path.join(dump_folder, img))]
print(valid_images)
return render_template('dump.html', images=valid_images) return render_template('dump.html', images=valid_images)
def emit_gallery_update(): def emit_gallery_update():

View File

@ -11,12 +11,22 @@
<a href="{{ url_for('dump') }}" class="button">🎃🎃🎃</a> <a href="{{ url_for('dump') }}" class="button">🎃🎃🎃</a>
</div> </div>
<h1>Pumpkin Gallery🎃</h1> <h1>Pumpkin Gallery🎃</h1>
<div class="gallery-container"> <div class="gallery-container" style="flex-direction: row; flex-wrap: wrap;">
{% for image in images %} {% for image in images %}
<a href="#" data-original="{{ url_for('uploaded_file', filename=image) }}" target="_blank"> <a href="{{ url_for('dump_file', filename=image) }}" data-original="{{ url_for('dump_file', filename=image) }}" target="_blank" style="flex: 1 0 25%;">
<img class="responsive-img" src="{{ url_for('cached_file', filename=image) }}" alt="{{ image }}"> <img class="responsive-img loaded" src="{{ url_for('dump_file', filename=image) }}" alt="{{ image }}" style="max-width:33vw;">
</a> </a>
{% endfor %} {% endfor %}
</div> </div>
<script>
const images = document.querySelectorAll('.gallery-container a');
images.forEach(image => {
image.addEventListener('click', (event) => {
event.preventDefault();
const original = image.getAttribute('data-original');
window.open(original, '_blank');
});
});
</script>
</body> </body>
</html> </html>