File size: 3,935 Bytes
05be5a5
 
 
 
 
 
 
 
 
 
 
 
c13ce0c
05be5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b74526
05be5a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, File, UploadFile, HTTPException
from fastapi.responses import FileResponse
import os
from .config import Config
from .main import ComicPanelExtractor
import traceback
from pathlib import Path
import shutil
import time
import mimetypes

base_output_folder = "api_outputs"
output_folder = os.path.join(Config.current_path, base_output_folder)

app = APIRouter()

def delete_folder_if_old_or_empty(parent_folder, age_days=1):
    """
    Delete subfolders inside `parent_folder` if they are empty
    or older than `age_days`.

    Args:
        parent_folder (str): Path to the parent directory.
        age_days (int): Number of days before a folder is considered old.
    """
    try:
        current_time = time.time()
        age_seconds = age_days * 24 * 60 * 60

        # Loop through all items in the parent folder
        for entry in os.scandir(parent_folder):
            if entry.is_dir():
                folder_path = entry.path
                # Check if folder is empty
                if not os.listdir(folder_path):
                    shutil.rmtree(folder_path)
                    print(f"Deleted empty folder: {folder_path}")
                    continue

                # Check if folder is older than age_days
                folder_mtime = os.path.getmtime(folder_path)
                if current_time - folder_mtime > age_seconds:
                    shutil.rmtree(folder_path)
                    print(f"Deleted old folder (>{age_days} day): {folder_path}")

    except Exception as e:
        print(f"Error cleaning subfolders in {parent_folder}: {e}")

@app.post("/api/extract/convert")
async def convert_comic(file: UploadFile = File(...)):
    """
    Upload a comic page and extract panels
    """
    # Generate unique filename
    file_id = os.path.splitext(file.filename)[0]
    specific_output_folder = f'{output_folder}/{file_id}'

    shutil.rmtree(specific_output_folder, ignore_errors=True)
    Path(specific_output_folder).mkdir(parents=True, exist_ok=True)
    file_path = f'{specific_output_folder}/{file.filename}'
    
    # Save uploaded file
    try:
        content = await file.read()
        with open(file_path, "wb") as f:
            f.write(content)

        # πŸ” DEBUG: Log file info
        print("======== DEBUG: Upload Info ========")
        print(f"Working Dir: {os.getcwd()}")
        print(f"Saved file path: {file_path}")
        print(f"Output folder: {specific_output_folder}")
        print(f"List of files in output folder: {os.listdir(specific_output_folder)}")
        print("====================================")

        # Extract panels
        config = Config()
        config.input_path = file_path
        config.output_folder = specific_output_folder

        print(f"[DEBUG] Setting config.input_path to: {config.input_path}")
        print(f"[DEBUG] Setting config.output_folder to: {config.output_folder}")

        _, _, all_panel_path = ComicPanelExtractor(config, reset=False).extract_panels_from_comic()
        all_panel_path = [f'/api/extract/{base_output_folder}/{file_id}/{os.path.basename(path)}' for path in all_panel_path]

        return {
            "success": True,
            "message": f"Extracted {len(all_panel_path)} panels",
            "panels": all_panel_path
        }
        
    except Exception as e:
        print(f"Error processing image: {str(e)} {traceback.format_exc()}")
        raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)} {traceback.format_exc()}")

@app.get("/api/extract/api_outputs/{folder}/{filename}")
async def get_output_file(folder: str, filename: str):
    file_path = f'{output_folder}/{folder}/{filename}'
    if not os.path.exists(file_path):
        raise HTTPException(status_code=404, detail="File not found")

    mime_type, _ = mimetypes.guess_type(file_path)
    return FileResponse(file_path, media_type=mime_type, filename=filename)