-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (86 loc) · 3.3 KB
/
Copy pathmain.py
File metadata and controls
125 lines (86 loc) · 3.3 KB
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
import os
import sys
import tempfile
import logging
from utils.file_helpers import allowed_file, get_file_extension
from utils.processors import PDFProcessor, PPTXProcessor
pdf_processor = PDFProcessor()
pptx_processor = PPTXProcessor()
logger = logging.getLogger(__name__)
OUTPUT_FOLDER = "output"
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
def allowed_file(filename):
return filename.lower().endswith((".pdf", ".pptx"))
def get_file_extension(filename):
return filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
def _process_pdf_local(upload_path, filename):
output_filename = f"processed_{filename}"
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
result = pdf_processor.process(upload_path, output_path, filename)
if not result["success"]:
raise RuntimeError(result["error"])
return {
"message": result["message"],
"output_path": output_path if result["has_watermark"] else None,
"file_type": "pdf",
"has_watermark": result["has_watermark"],
}
def _process_pptx_local(upload_path, filename):
output_filename = f"processed_{filename}"
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
result = pptx_processor.process(upload_path, output_path, filename)
if not result["success"]:
raise RuntimeError(result["error"])
return {
"message": result["message"],
"output_path": output_path if result["has_watermark"] else None,
"file_type": "pptx",
"has_watermark": result["has_watermark"],
}
def remove_watermark_local(file_path):
if not os.path.exists(file_path):
raise ValueError("File does not exist")
filename = os.path.basename(file_path)
if not allowed_file(filename):
raise ValueError("Invalid file type. Only PDF or PPTX allowed.")
original_extension = get_file_extension(filename)
if not original_extension:
raise ValueError("File must have a valid extension (.pdf or .pptx)")
file_extension = original_extension.lower()
logger.info(f"Processing file: {filename} (type: {file_extension})")
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file_extension}") as temp_input:
upload_path = temp_input.name
try:
with open(file_path, "rb") as f:
temp_input.write(f.read())
temp_input.flush()
if file_extension == "pdf":
return _process_pdf_local(upload_path, filename)
elif file_extension == "pptx":
return _process_pptx_local(upload_path, filename)
else:
raise ValueError(f"Unsupported file type: {file_extension}")
finally:
try:
os.unlink(upload_path)
except Exception:
pass
def main():
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <file.pdf|file.pptx>")
sys.exit(1)
file_path = sys.argv[1]
try:
result = remove_watermark_local(file_path)
print(result["message"])
if result["output_path"]:
print(result["output_path"])
else:
print("No watermark found.")
print(file_path)
sys.exit(0)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()