SohomToom commited on
Commit
e56fe2d
·
verified ·
1 Parent(s): 5451df8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -98,6 +98,7 @@ def process_cbz_cbr(files):
98
  final_output = tempfile.mkdtemp()
99
 
100
  for file_path in files:
 
101
  if file_path.endswith(".cbz"):
102
  with zipfile.ZipFile(file_path, 'r') as archive:
103
  extract_dir = tempfile.mkdtemp()
@@ -125,6 +126,32 @@ def process_cbz_cbr(files):
125
 
126
  return zip_buffer.name
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  demo = gr.Interface(
129
  fn=process_cbz_cbr,
130
  inputs=gr.File(file_types=[".cbz", ".cbr"], file_count="multiple", label="Upload .cbz or .cbr Comic Files"),
 
98
  final_output = tempfile.mkdtemp()
99
 
100
  for file_path in files:
101
+ file_path = convert_cbr_to_cbz(file_path)
102
  if file_path.endswith(".cbz"):
103
  with zipfile.ZipFile(file_path, 'r') as archive:
104
  extract_dir = tempfile.mkdtemp()
 
126
 
127
  return zip_buffer.name
128
 
129
+ import os
130
+ import zipfile
131
+ import rarfile
132
+ import tempfile
133
+ import shutil
134
+
135
+ def convert_cbr_to_cbz(cbr_path):
136
+ temp_dir = tempfile.mkdtemp()
137
+ try:
138
+ with rarfile.RarFile(cbr_path) as archive:
139
+ archive.extractall(temp_dir)
140
+ except rarfile.RarCannotExec:
141
+ raise RuntimeError("Cannot extract CBR. 'unrar' tool not found. Install 'unrar' or 'unrar-free'.")
142
+
143
+ cbz_path = cbr_path.replace('.cbr', '.cbz')
144
+ with zipfile.ZipFile(cbz_path, 'w') as zipf:
145
+ for root, _, files in os.walk(temp_dir):
146
+ for file in files:
147
+ file_path = os.path.join(root, file)
148
+ arcname = os.path.relpath(file_path, temp_dir)
149
+ zipf.write(file_path, arcname)
150
+
151
+ shutil.rmtree(temp_dir)
152
+ return cbz_path
153
+
154
+
155
  demo = gr.Interface(
156
  fn=process_cbz_cbr,
157
  inputs=gr.File(file_types=[".cbz", ".cbr"], file_count="multiple", label="Upload .cbz or .cbr Comic Files"),