shiue2000 commited on
Commit
822b379
·
verified ·
1 Parent(s): dc9f3c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -8
app.py CHANGED
@@ -14,15 +14,14 @@ UPLOAD_FOLDER = BASE_DIR / "images"
14
  UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)
15
 
16
  def get_image_paths(limit=30):
17
- """Retrieve paths of up to `limit` images sorted by modification time."""
18
  try:
19
  files = sorted(UPLOAD_FOLDER.glob("*"), key=lambda x: x.stat().st_mtime, reverse=True)
20
- return [str(f) for f in files if f.suffix.lower() in ['.png', '.jpg', '.jpeg']]
 
21
  except Exception:
22
  return []
23
 
24
  def save_image(file_path):
25
- """Save uploaded image to UPLOAD_FOLDER, converting HEIC if needed."""
26
  if file_path is None:
27
  return "❗請選擇一張圖片", get_image_paths(), None
28
  try:
@@ -43,7 +42,6 @@ def save_image(file_path):
43
  return f"❌ 上傳失敗: {str(e)}", get_image_paths(), None
44
 
45
  def clear_images_with_code(code):
46
- """Clear all images if the correct passcode is provided."""
47
  if code.strip() == "78750":
48
  try:
49
  shutil.rmtree(UPLOAD_FOLDER)
@@ -54,8 +52,7 @@ def clear_images_with_code(code):
54
  return "❌ 驗證碼錯誤,無法清除圖片", get_image_paths(), None
55
 
56
  def download_all_images():
57
- """Create a zip file with all images and return its path."""
58
- image_files = list(UPLOAD_FOLDER.glob("*"))
59
  if not image_files:
60
  return None, "⚠️ 沒有圖片可下載"
61
 
@@ -66,15 +63,15 @@ def download_all_images():
66
  with zipfile.ZipFile(temp_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
67
  for filepath in image_files:
68
  zipf.write(filepath, arcname=filepath.name)
69
- return str(temp_zip), f"✅ ZIP 文件已生成"
70
  except Exception as e:
71
  return None, f"❌ ZIP 文件生成失敗: {str(e)}"
72
 
73
  def cleanup_old_zips():
74
- """Remove old zip files from the temp directory."""
75
  temp_dir = Path(tempfile.gettempdir())
76
  for zip_file in temp_dir.glob("all_images_*.zip"):
77
  try:
 
78
  zip_file.unlink()
79
  except Exception:
80
  pass
 
14
  UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)
15
 
16
  def get_image_paths(limit=30):
 
17
  try:
18
  files = sorted(UPLOAD_FOLDER.glob("*"), key=lambda x: x.stat().st_mtime, reverse=True)
19
+ paths = [str(f) for f in files if f.suffix.lower() in ['.png', '.jpg', '.jpeg']]
20
+ return paths[:limit]
21
  except Exception:
22
  return []
23
 
24
  def save_image(file_path):
 
25
  if file_path is None:
26
  return "❗請選擇一張圖片", get_image_paths(), None
27
  try:
 
42
  return f"❌ 上傳失敗: {str(e)}", get_image_paths(), None
43
 
44
  def clear_images_with_code(code):
 
45
  if code.strip() == "78750":
46
  try:
47
  shutil.rmtree(UPLOAD_FOLDER)
 
52
  return "❌ 驗證碼錯誤,無法清除圖片", get_image_paths(), None
53
 
54
  def download_all_images():
55
+ image_files = [f for f in UPLOAD_FOLDER.iterdir() if f.suffix.lower() in ['.png', '.jpg', '.jpeg']]
 
56
  if not image_files:
57
  return None, "⚠️ 沒有圖片可下載"
58
 
 
63
  with zipfile.ZipFile(temp_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
64
  for filepath in image_files:
65
  zipf.write(filepath, arcname=filepath.name)
66
+ return str(temp_zip), "✅ ZIP 文件已生成"
67
  except Exception as e:
68
  return None, f"❌ ZIP 文件生成失敗: {str(e)}"
69
 
70
  def cleanup_old_zips():
 
71
  temp_dir = Path(tempfile.gettempdir())
72
  for zip_file in temp_dir.glob("all_images_*.zip"):
73
  try:
74
+ # keep recent ones; optional: delete older than X hours if desired
75
  zip_file.unlink()
76
  except Exception:
77
  pass