-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcatbox.py
More file actions
1161 lines (961 loc) · 45.8 KB
/
catbox.py
File metadata and controls
1161 lines (961 loc) · 45.8 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import mimetypes
import os
import sys
import time
import traceback
import winreg
import sqlite3
import shutil
import json
import lzstring
from thumb import generate_thumbnail
from history_viewer import log_upload
import pythoncom
import requests
import PIL.Image as Image
from PyQt6.QtCore import (Qt, QThread, QTimer, pyqtSignal,
pyqtSlot)
from PyQt6.QtGui import QIcon, QImage, QPixmap, QAction, QCursor
from PyQt6.QtWidgets import (QApplication, QDialog, QHBoxLayout, QInputDialog,
QLabel, QMessageBox, QProgressBar, QPushButton,
QScrollArea, QTextEdit, QVBoxLayout, QWidget, QMenu)
from requests_toolbelt.multipart.encoder import (MultipartEncoder,
MultipartEncoderMonitor)
class UploadCancelledException(Exception):
"""Exception raised when upload is cancelled."""
pass
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
base_path = sys._MEIPASS
os.environ['TCL_LIBRARY'] = os.path.join(base_path, 'tcl')
os.environ['TK_LIBRARY'] = os.path.join(base_path, 'tk')
else:
application_path = os.path.dirname(os.path.abspath(__file__))
ico_path = os.path.join(application_path, "icons", "icon.ico")
REG_PATH = r"Software\CatboxUploader"
# Colors for dark/light theme
dark_theme_colors = {
"border": "#606060",
"bg": "#2D2D2D",
"text": "white",
"chunk": "#697DA0",
"chunk_pressed": "#50688A"
}
light_theme_colors = {
"border": "#c0c0c0",
"bg": "#ffffff",
"text": "#000000",
"chunk": "#8E9EBB",
"chunk_pressed": "#7A91B1"
}
# API Endpoints
API_CATBOX = "https://catbox.moe/user/api.php"
API_LITTERBOX = "https://litterbox.catbox.moe/resources/internals/api.php"
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
def read_registry_value(name):
"""Read a value from Windows Registry under HKEY_CURRENT_USER."""
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, REG_PATH, 0, winreg.KEY_READ) as key:
value, _ = winreg.QueryValueEx(key, name)
return value
except FileNotFoundError:
return None
def write_registry_value(name, value):
"""Write a value to Windows Registry under HKEY_CURRENT_USER."""
try:
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, REG_PATH) as key:
winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
except Exception as e:
app.setWindowIcon(QIcon(ico_path))
QMessageBox.critical(None, "Registry Error", f"Failed to save to registry:\n{str(e)}")
def is_windows_light_mode() -> bool:
"""Checks if the current Windows theme is light mode.
Returns:
A bool based off if Windows is using light mode
"""
try:
with winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
) as key:
value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
return value == 1
except Exception:
# Default to dark mode
return False
def get_themed_icon_filename(icon_name: str) -> str:
"""Get the themed icon filename based on current Windows theme.
Args:
icon_name: Base icon filename (e.g., 'upload_user.ico')
Returns:
The appropriate icon filename for the current theme
"""
# Icons that have light variants
light_variant_icons = ['upload_user', 'upload_anon', 'edit_userhash', 'history']
use_light = is_windows_light_mode()
# Extract base name without extension
base_name = icon_name.replace('.ico', '')
if use_light and base_name in light_variant_icons:
light_icon = f"{base_name}_light.ico"
# Check if light variant exists
if os.path.exists(os.path.join(icons_dir, light_icon)):
return light_icon
return icon_name
def prompt_for_userhash():
"""Show a Qt input dialog to enter and save userhash."""
app = QApplication(sys.argv)
app.setWindowIcon(QIcon(ico_path))
userhash, ok = QInputDialog.getText(None, "Enter User Hash", "Enter your Catbox userhash:")
if ok and userhash.strip():
write_registry_value("userhash", userhash.strip())
return userhash.strip()
elif ok and not userhash.strip():
app.setWindowIcon(QIcon(ico_path))
QMessageBox.critical(None, "Error", "User hash cannot be empty.")
return prompt_for_userhash()
os._exit(0) # Exit if user cancels
# Parse CLI arguments
parser = argparse.ArgumentParser(description="Upload files to Catbox or Litterbox.")
parser.add_argument("file", nargs="?", help="Path to the file to upload.")
parser.add_argument("--anonymous", action="store_true", help="Upload anonymously (no user hash).")
parser.add_argument("--litterbox", choices=["1h", "12h", "24h", "72h"], help="Litterbox with specified expiration time.")
parser.add_argument("--edit-userhash", action="store_true", help="Edit and save a new userhash.")
parser.add_argument("--history", action="store_true", help="Show upload history")
args = parser.parse_args()
cwd = os.getcwd()
icons_dir = os.path.join(application_path, "icons")
icon_path = f'"{icons_dir}\\icon.ico"'
CONTEXT_MENU_KEYS = [
(r"Software\Classes\*\shell\Catbox", "Catbox", True, icon_path),
# Ordered sub-items with individual icons
(r"Software\Classes\*\shell\Catbox\shell\001_upload_user", "Upload as User", False, "upload_user.ico"),
(r"Software\Classes\*\shell\Catbox\shell\001_upload_user\command", f'"{application_path}\\catbox.exe" "%1"', False, None),
(r"Software\Classes\*\shell\Catbox\shell\002_upload_anon", "Upload anonymously", False, "upload_anon.ico"),
(r"Software\Classes\*\shell\Catbox\shell\002_upload_anon\command", f'"{application_path}\\catbox.exe" --anonymous "%1"', False, None),
(r"Software\Classes\*\shell\Catbox\shell\003_edit_userhash", "Edit userhash", False, "edit_userhash.ico"),
(r"Software\Classes\*\shell\Catbox\shell\003_edit_userhash\command", f'"{application_path}\\catbox.exe" --edit-userhash', False, None),
(r"Software\Classes\*\shell\Catbox\shell\004_history", "Upload History", False, "history.ico"),
(r"Software\Classes\*\shell\Catbox\shell\004_history\command", f'"{application_path}\\catbox.exe" --history', False, None),
(r"Software\Classes\*\shell\Litterbox", "Litterbox", True, icon_path),
# Litterbox items without custom icons (will use default system icons)
(r"Software\Classes\*\shell\Litterbox\shell\001_litterbox_1h", "1h", False, None),
(r"Software\Classes\*\shell\Litterbox\shell\001_litterbox_1h\command", f'"{application_path}\\catbox.exe" --litterbox 1h "%1"', False, None),
(r"Software\Classes\*\shell\Litterbox\shell\002_litterbox_12h", "12h", False, None),
(r"Software\Classes\*\shell\Litterbox\shell\002_litterbox_12h\command", f'"{application_path}\\catbox.exe" --litterbox 12h "%1"', False, None),
(r"Software\Classes\*\shell\Litterbox\shell\003_litterbox_24h", "24h", False, None),
(r"Software\Classes\*\shell\Litterbox\shell\003_litterbox_24h\command", f'"{application_path}\\catbox.exe" --litterbox 24h "%1"', False, None),
(r"Software\Classes\*\shell\Litterbox\shell\004_litterbox_72h", "72h", False, None),
(r"Software\Classes\*\shell\Litterbox\shell\004_litterbox_72h\command", f'"{application_path}\\catbox.exe" --litterbox 72h "%1"', False, None),
]
def check_registry_keys():
"""Check if all context menu registry keys exist and have the correct values."""
missing_or_incorrect_keys = []
for entry in CONTEXT_MENU_KEYS:
key_path, value, is_parent = entry[:3]
icon_file = entry[3] if len(entry) > 3 else None
# Get themed icon filename if icon is specified
if icon_file and not icon_file.startswith('"'):
icon_file = get_themed_icon_filename(icon_file)
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_READ) as key:
if "command" in key_path:
# For command keys, check if the value matches the current executable path
current_value, _ = winreg.QueryValueEx(key, "")
if current_value != value:
missing_or_incorrect_keys.append((key_path, value, is_parent, icon_file))
else:
# For non-command keys, check if the MUIVerb value matches
current_value, _ = winreg.QueryValueEx(key, "MUIVerb")
if current_value != value:
missing_or_incorrect_keys.append((key_path, value, is_parent, icon_file))
# Check icon if specified
if icon_file:
try:
icon_path_full = f'"{os.path.join(icons_dir, icon_file)}"'
current_icon, _ = winreg.QueryValueEx(key, "Icon")
if current_icon != icon_path_full:
missing_or_incorrect_keys.append((key_path, value, is_parent, icon_file))
except FileNotFoundError:
missing_or_incorrect_keys.append((key_path, value, is_parent, icon_file))
except FileNotFoundError:
# If the key doesn't exist, add it to the list of missing keys
missing_or_incorrect_keys.append((key_path, value, is_parent, icon_file))
return not missing_or_incorrect_keys # True if no keys are missing or incorrect
def add_registry_keys():
"""Add or update context menu registry keys."""
try:
for entry in CONTEXT_MENU_KEYS:
key_path, value, is_parent = entry[:3]
icon_file = entry[3] if len(entry) > 3 else None
# Get themed icon filename if icon is specified (skip already-quoted full paths)
if icon_file and not icon_file.startswith('"'):
icon_file = get_themed_icon_filename(icon_file)
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_path) as key:
if "command" in key_path:
# For command keys, set the value to the current executable path
winreg.SetValueEx(key, "", 0, winreg.REG_SZ, value)
else:
# For non-command keys, set the MUIVerb value
winreg.SetValueEx(key, "MUIVerb", 0, winreg.REG_SZ, value)
# Set icon if specified
if icon_file:
# Handle both full paths (quoted) and relative icon filenames
if icon_file.startswith('"'):
icon_path_full = icon_file
icon_exists = os.path.exists(icon_file.strip('"'))
else:
icon_path_full = f'"{os.path.join(icons_dir, icon_file)}"'
icon_exists = os.path.exists(os.path.join(icons_dir, icon_file))
# Verify icon file exists before setting
if icon_exists:
winreg.SetValueEx(key, "Icon", 0, winreg.REG_SZ, icon_path_full)
else:
# Fallback to main icon if specific icon doesn't exist
winreg.SetValueEx(key, "Icon", 0, winreg.REG_SZ, icon_path)
if is_parent:
winreg.SetValueEx(key, "SubCommands", 0, winreg.REG_SZ, "")
return True
except Exception as e:
if QApplication.instance():
app.setWindowIcon(QIcon(ico_path))
QMessageBox.critical(None, "Registry Error", f"Failed to add/update context menu:\n{str(e)}")
return False
def main():
"""Main function to check or add/update registry keys."""
app = QApplication(sys.argv) if not QApplication.instance() else QApplication.instance()
if len(sys.argv) == 1:
# Ensure icons directory exists
ensure_icons_directory()
if not check_registry_keys():
if add_registry_keys():
app.setWindowIcon(QIcon(ico_path))
QMessageBox.information(None, "Context Menu Updated", "Context menu buttons have been added & updated with custom icons.")
sys.exit(0)
# Handle --edit-userhash separately
if args.edit_userhash:
new_userhash = prompt_for_userhash()
print(f"Userhash updated: {new_userhash}")
sys.exit(0)
# Ensure userhash exists if not in anonymous mode and not in litterbox mode
USER_HASH = read_registry_value("userhash")
if not USER_HASH and args.file and not args.anonymous and not args.litterbox:
USER_HASH = prompt_for_userhash()
def get_database_path():
"""Get the database path, preferring %APPDATA%/Catbox Uploader/ location."""
# New location in %APPDATA%
appdata_path = os.path.expandvars(r"%APPDATA%\Catbox Uploader")
new_db_path = os.path.join(appdata_path, "catbox.db")
# Old location in working directory
old_db_path = os.path.join(application_path, "catbox.db")
# Create %APPDATA%/Catbox Uploader directory if it doesn't exist
os.makedirs(appdata_path, exist_ok=True)
# Check if old database exists and new one doesn't
if os.path.exists(old_db_path) and not os.path.exists(new_db_path):
try:
shutil.move(old_db_path, new_db_path)
print(f"✅ Migrated database from {old_db_path} to {new_db_path}")
except Exception as e:
print(f"⚠️ Failed to migrate database: {e}")
# Fall back to old location if migration fails
return old_db_path
return new_db_path
def ensure_database_schema():
"""Ensure the database exists and has the correct schema."""
db_path = get_database_path()
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Check if uploads table exists
cursor.execute("""
SELECT name FROM sqlite_master
WHERE type='table' AND name='uploads'
""")
table_exists = cursor.fetchone() is not None
if not table_exists:
# Create the uploads table
cursor.execute("""
CREATE TABLE uploads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_path TEXT,
url TEXT,
mode TEXT,
timestamp INTEGER,
expiry_duration TEXT,
is_deleted INTEGER DEFAULT 0
)
""")
print("✅ Created uploads table")
else:
# Check if is_deleted column exists (for backward compatibility)
cursor.execute("PRAGMA table_info(uploads)")
columns = [column[1] for column in cursor.fetchall()]
if 'is_deleted' not in columns:
cursor.execute("ALTER TABLE uploads ADD COLUMN is_deleted INTEGER DEFAULT 0")
print("✅ Added is_deleted column to uploads table")
conn.commit()
conn.close()
print(f"✅ Database schema validated: {db_path}")
return db_path
except Exception as e:
print(f"❌ Database schema validation failed: {e}")
return None
def log_upload(file_path, url, mode, expiry_duration=None):
"""Log upload information to database."""
db_path = ensure_database_schema()
if not db_path:
return
try:
file_path = os.path.abspath(file_path)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO uploads (file_path, url, mode, timestamp, expiry_duration, is_deleted)
VALUES (?, ?, ?, ?, ?, 0)
""", (
file_path,
url,
mode,
int(time.time()),
expiry_duration
))
conn.commit()
conn.close()
print(f"✅ Successfully logged upload: {file_path}")
except Exception as e:
print(f"⚠️ Failed to log upload: {e}")
class UploadWorker(QThread):
update_progress = pyqtSignal(int)
update_bytes_uploaded = pyqtSignal(int)
upload_finished = pyqtSignal(str)
def __init__(self, file_path, is_anonymous=False, litterbox_time=None):
super().__init__()
self.file_path = file_path
self.is_anonymous = is_anonymous
self.litterbox_time = litterbox_time
self.total_size = 0
self.bytes_uploaded = 0
self._cancelled = False
self._session = None
def cancel(self):
"""Cancel the upload by closing the session."""
self._cancelled = True
if self._session:
self._session.close()
def run(self):
try:
# Create a session for this upload
self._session = requests.Session()
# Get file size for progress tracking
self.total_size = os.path.getsize(self.file_path)
# Choose upload method based on parameters
if self.litterbox_time:
result = self.upload_to_litterbox()
else:
result = self.upload_to_catbox()
self.upload_finished.emit(result)
except UploadCancelledException:
self.upload_finished.emit("CANCELLED")
except Exception as e:
if self._cancelled:
self.upload_finished.emit("CANCELLED")
else:
self.upload_finished.emit(f"Error: {str(e)}")
finally:
if self._session:
self._session.close()
self._session = None
def create_monitor_callback(self, encoder):
"""Create a callback function for monitoring upload progress."""
def callback(monitor):
# Check if cancelled and raise exception to abort upload immediately
if self._cancelled:
raise UploadCancelledException("Upload cancelled by user")
self.bytes_uploaded = monitor.bytes_read
if self.total_size > 0:
progress = int((self.bytes_uploaded / self.total_size) * 100)
self.update_progress.emit(progress)
self.update_bytes_uploaded.emit(self.bytes_uploaded)
return callback
def upload_to_catbox(self):
"""Upload file to Catbox."""
if self._cancelled:
return "CANCELLED"
url = API_CATBOX
# Prepare form data
fields = {
'reqtype': 'fileupload',
}
# Add userhash if not anonymous
if not self.is_anonymous and USER_HASH:
fields['userhash'] = USER_HASH
# Add the file
with open(self.file_path, 'rb') as f:
fields['fileToUpload'] = (os.path.basename(self.file_path), f, mimetypes.guess_type(self.file_path)[0])
# Create multipart encoder
encoder = MultipartEncoder(fields=fields)
monitor = MultipartEncoderMonitor(encoder, self.create_monitor_callback(encoder))
# Make the request
headers = {
'Content-Type': monitor.content_type,
'User-Agent': USER_AGENT
}
response = self._session.post(url, data=monitor, headers=headers, timeout=None)
if response.status_code == 200:
result = response.text.strip()
if result.startswith('http'):
return result
elif not result: # Empty response - server bug
return "EMPTY_RESPONSE"
else:
return f"❌ Upload failed: {result}"
else:
return f"❌ Upload failed with status code: {response.status_code} \n {response.text.strip()}"
def upload_to_litterbox(self):
"""Upload file to Litterbox with specified expiration time."""
url = API_LITTERBOX
# Prepare form data
fields = {
'reqtype': 'fileupload',
'time': self.litterbox_time
}
# Add the file
with open(self.file_path, 'rb') as f:
fields['fileToUpload'] = (os.path.basename(self.file_path), f, mimetypes.guess_type(self.file_path)[0])
# Create multipart encoder
encoder = MultipartEncoder(fields=fields)
monitor = MultipartEncoderMonitor(encoder, self.create_monitor_callback(encoder))
# Make the request
headers = {
'Content-Type': monitor.content_type,
'User-Agent': USER_AGENT
}
if self._cancelled:
return "CANCELLED"
response = self._session.post(url, data=monitor, headers=headers, timeout=None)
if response.status_code == 200:
result = response.text.strip()
if result.startswith('http'):
return result
elif not result: # Empty response - server bug
return "EMPTY_RESPONSE"
else:
return f"Upload failed: {result}"
else:
return f"Upload failed with status code: {response.status_code}"
def pil_image_to_qpixmap(pil_image: Image.Image) -> QPixmap:
if pil_image.mode != "RGBA":
pil_image = pil_image.convert("RGBA")
data = pil_image.tobytes("raw", "RGBA")
qimage = QImage(data, pil_image.width, pil_image.height, QImage.Format.Format_RGBA8888)
return QPixmap.fromImage(qimage)
def is_video_file(file_path):
"""Check if the file is a video file based on extension."""
video_extensions = ['.mp4', '.mov', '.webm']
return any(file_path.lower().endswith(ext) for ext in video_extensions)
def generate_discord_embed_url(video_url, original_filename=None):
"""Generate a Discord-embeddable URL using video.karimawi.me service with LZString."""
try:
# Extract filename from URL (files.catbox.moe/abcdef.mp4 -> abcdef.mp4)
filename = video_url.split('/')[-1]
# Use original filename for title if provided, otherwise default to URL filename without extension
if original_filename:
title = os.path.splitext(original_filename)[0]
else:
title = os.path.splitext(filename)[0]
# Check for Litterbox
is_litterbox = "litter.catbox.moe" in video_url
# Format for embedder: ["filename.ext", "Title"] or ["*filename.ext", "Title"] for litterbox
stored_filename = f"*{filename}" if is_litterbox else filename
payload = [stored_filename, title]
json_str = json.dumps(payload)
lz = lzstring.LZString()
encoded = lz.compressToEncodedURIComponent(json_str)
return f"https://video.karimawi.me/{encoded}"
except Exception as e:
print(f"⚠️ Failed to generate embed URL: {e}")
return video_url
def get_themed_icon(icon_name: str) -> QIcon:
"""Get an icon based on the current theme.
Args:
icon_name: Base icon name without extension (e.g., 'reload', 'del', 'bin')
Returns:
QIcon for the appropriate theme
"""
use_light = is_windows_light_mode()
# Icons that have light variants
light_variant_icons = ['bin', 'reload', 'edit_userhash', 'history', 'upload_anon', 'upload_user']
if use_light and icon_name in light_variant_icons:
icon_file = f"{icon_name}_light.ico"
else:
icon_file = f"{icon_name}.ico"
icon_path = os.path.join(application_path, "icons", icon_file)
# Fallback to regular icon if light variant doesn't exist
if use_light and icon_name in light_variant_icons and not os.path.exists(icon_path):
icon_path = os.path.join(application_path, "icons", f"{icon_name}.ico")
return QIcon(icon_path)
def create_thumbnail(path, deleted=False):
"""Create thumbnail icon, with optional theme-aware fallback icon."""
if not deleted:
try:
thumb = generate_thumbnail(path)
pixmap = QPixmap.fromImage(thumb.toqpixmap().toImage())
return QIcon(pixmap)
except:
pass
# Use themed delete icon
return get_themed_icon('del')
def get_progressbar_stylesheet(colors: dict) -> str:
"""
Generates a QSS string for QProgressBar using the given color dictionary.
"""
return f"""
QProgressBar {{
border: 1px solid {colors['border']};
border-radius: 5px;
background-color: {colors['bg']};
text-align: center;
font-weight: bold;
color: {colors['text']};
}}
QProgressBar::chunk {{
background-color: {colors['chunk']};
}}
"""
def get_menu_stylesheet(colors: dict) -> str:
"""
Generates a QSS string for QMenu using the given color dictionary.
"""
return f"""
QMenu {{
background-color: {colors['bg']};
color: {colors['text']};
border: 1px solid {colors['border']};
border-radius: 8px;
padding: 2px;
}}
QMenu::item {{
background-color: transparent;
padding: 6px 12px;
border-radius: 4px;
}}
QMenu::item:selected {{
background-color: {colors['chunk']};
color: {colors['text']};
}}
QMenu::item:pressed {{
background-color: {colors['chunk_pressed']};
}}
"""
class UploadWindow(QWidget):
def __init__(self, file_path, is_anonymous=False, litterbox_time=None):
super().__init__()
self.file_path = file_path
self.file_size = os.path.getsize(file_path)
self.bytes_uploaded = 0
self.uploading = True
self.cancelled = False
self.is_anonymous = is_anonymous
self.litterbox_time = litterbox_time
self.start_time = time.time() # Initialize start_time here
use_light = is_windows_light_mode()
theme_colors = light_theme_colors if use_light else dark_theme_colors
# Dynamic Window Title
if litterbox_time:
self.setWindowTitle(f"Uploading to Litterbox ({litterbox_time})")
elif is_anonymous:
self.setWindowTitle("Uploading to Catbox anonymously")
else:
self.setWindowTitle("Uploading to Catbox")
self.setWindowFlags(self.windowFlags() | Qt.WindowType.WindowMinimizeButtonHint | Qt.WindowType.WindowStaysOnTopHint)
self.setWindowIcon(QIcon(ico_path))
self.setFixedSize(420, 160)
# Set the background color of the window
self.setStyleSheet(f"background-color: {theme_colors['bg']};")
layout = QHBoxLayout()
self.thumbnail_label = QLabel(self)
pixmap = pil_image_to_qpixmap(generate_thumbnail(self.file_path))
self.thumbnail_label.setPixmap(pixmap.scaled(120, 120, Qt.AspectRatioMode.KeepAspectRatio))
layout.addWidget(self.thumbnail_label)
right_layout = QVBoxLayout()
# Create file label
self.file_label = QLabel(f"Uploading: {os.path.basename(file_path)}")
self.file_label.setWordWrap(True) # Enable word wrap
self.file_label.setStyleSheet(f"background-color: {theme_colors['bg']}; color: {theme_colors['text']};")
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True) # Allow the label to resize within the scroll area
scroll_area.setWidget(self.file_label)
scroll_area.setStyleSheet(f"background-color: {theme_colors['bg']}; border: none;")
right_layout.addWidget(scroll_area)
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setFixedHeight(20) # Set the height of the progress bar to make it thicker
self.progress_bar.setStyleSheet(get_progressbar_stylesheet(theme_colors))
right_layout.addWidget(self.progress_bar)
self.eta_label = QLabel("ETA: Starting...")
right_layout.addWidget(self.eta_label)
self.cancel_button = QPushButton("Cancel")
self.cancel_button.setStyleSheet(f"background-color: {theme_colors['chunk']}; color: {theme_colors['text']};")
self.cancel_button.clicked.connect(self.cancel_upload)
right_layout.addWidget(self.cancel_button)
layout.addLayout(right_layout)
self.setLayout(layout)
self.move_to_bottom_right()
self.upload_worker = UploadWorker(file_path, is_anonymous, litterbox_time)
self.upload_worker.update_progress.connect(self.update_progress)
self.upload_worker.update_bytes_uploaded.connect(self.update_bytes_uploaded)
self.upload_worker.upload_finished.connect(self.update_ui_after_upload)
self.upload_worker.start()
self.timer = QTimer()
self.timer.timeout.connect(self.update_eta)
self.timer.start(500)
def move_to_bottom_right(self):
screen = QApplication.primaryScreen()
available_geometry = screen.availableGeometry()
window_geometry = self.frameGeometry()
x = available_geometry.right() - window_geometry.width() - 10
y = available_geometry.bottom() - window_geometry.height() - 40
self.move(x, y)
def update_progress(self, progress):
self.progress_bar.setValue(progress)
def update_bytes_uploaded(self, bytes_uploaded):
self.bytes_uploaded = bytes_uploaded
def update_eta(self):
if hasattr(self, 'start_time') and hasattr(self, 'bytes_uploaded') and self.bytes_uploaded > 0:
elapsed_time = time.time() - self.start_time
# Check if upload_worker and total_size are available
if hasattr(self.upload_worker, 'total_size') and self.upload_worker.total_size > 0:
total_bytes = self.upload_worker.total_size
else:
# Fallback: try to get file size directly
try:
total_bytes = os.path.getsize(self.upload_worker.file_path)
except:
total_bytes = 0
if total_bytes > 0 and elapsed_time > 0:
bytes_per_second = self.bytes_uploaded / elapsed_time
remaining_bytes = total_bytes - self.bytes_uploaded
eta_seconds = remaining_bytes / bytes_per_second
# Format ETA with hours, minutes, and seconds
if eta_seconds > 3600: # More than 1 hour
hours = int(eta_seconds // 3600)
minutes = int((eta_seconds % 3600) // 60)
seconds = int(eta_seconds % 60)
eta_text = f"ETA: {hours}h {minutes}m {seconds}s"
elif eta_seconds > 60: # More than 1 minute
minutes = int(eta_seconds // 60)
seconds = int(eta_seconds % 60)
eta_text = f"ETA: {minutes}m {seconds}s"
else: # Less than 1 minute
seconds = int(eta_seconds)
eta_text = f"ETA: {seconds}s"
self.eta_label.setText(eta_text)
else:
self.eta_label.setText("ETA: Calculating...")
else:
self.eta_label.setText("ETA: Starting...")
@pyqtSlot(str)
def update_ui_after_upload(self, result):
# Ignore if already cancelled (UI was already updated)
if self.cancelled:
return
if result == "CANCELLED":
# This shouldn't normally be reached since we disconnect signals,
# but handle it just in case
self.file_label.setText("❌ Upload cancelled")
self.progress_bar.setFormat("Cancelled")
self.eta_label.setText("Cancelled")
self.cancel_button.setText("OK")
self.uploading = False
self.timer.stop()
elif result == "EMPTY_RESPONSE":
self.handle_empty_response()
elif "http" in result:
self.file_label.setText(f"<p>✅ Uploaded: <a href='{result}'>{result}</a></p>")
self.file_label.setOpenExternalLinks(True)
# Store the URL for context menu
self.file_label.setProperty("upload_url", result.strip())
# Set up context menu for URL
self.file_label.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.file_label.customContextMenuRequested.connect(self.show_url_context_menu)
clipboard = QApplication.clipboard()
clipboard.setText(result.strip(), clipboard.Mode.Clipboard)
self.cancel_button.setText("OK")
self.progress_bar.setFormat("%p%")
self.progress_bar.setValue(100)
self.eta_label.setText("Upload Complete")
if self.is_anonymous:
mode = "Anonymous"
elif self.litterbox_time:
mode = f"Litterbox {self.litterbox_time}"
else:
mode = "User"
log_upload(file_path=self.file_path, url=result, mode=mode, expiry_duration=getattr(self, 'litterbox_time', None))
self.uploading = False
self.timer.stop() # Stop the timer when the upload is complete
# Force UI to update now
QApplication.processEvents()
else:
self.file_label.setText(result)
self.cancel_button.setText("OK")
self.progress_bar.setValue(100)
self.eta_label.setText("Upload Failed")
self.uploading = False
self.timer.stop()
def show_url_context_menu(self, position):
"""Show context menu for the uploaded URL."""
url = self.file_label.property("upload_url")
use_light = is_windows_light_mode()
theme_colors = light_theme_colors if use_light else dark_theme_colors
if not url:
return
menu = QMenu(self)
menu.setStyleSheet(get_menu_stylesheet(theme_colors))
# Copy action
copy_action = QAction("Copy URL", self)
copy_action.triggered.connect(lambda: QApplication.clipboard().setText(url))
menu.addAction(copy_action)
# Copy embeddable action (only for videos)
if is_video_file(self.file_path):
# Pass basename of source file as title
embed_url = generate_discord_embed_url(url, os.path.basename(self.file_path))
copy_embed_action = QAction("Copy Embeddable", self)
copy_embed_action.triggered.connect(lambda: QApplication.clipboard().setText(embed_url))
menu.addAction(copy_embed_action)
open_action = QAction("Open in Browser", self)
open_action.triggered.connect(lambda: self.open_url_in_browser(url))
menu.addAction(open_action)
menu.exec(self.file_label.mapToGlobal(position))
def open_url_in_browser(self, url):
"""Open URL in default browser."""
import webbrowser
try:
webbrowser.open(url)
except Exception as e:
QMessageBox.warning(self, "Error", f"Failed to open URL: {str(e)}")
def cancel_upload(self):
"""Cancel the current upload."""
if self.uploading:
# Mark as cancelled and not uploading first
self.uploading = False
self.cancelled = True
# Stop timer first to prevent UI updates
self.timer.stop()
# Disconnect ALL signals to stop any updates from the worker
if hasattr(self, 'upload_worker'):
try:
self.upload_worker.update_progress.disconnect(self.update_progress)
except TypeError:
pass # Already disconnected
try:
self.upload_worker.update_bytes_uploaded.disconnect(self.update_bytes_uploaded)
except TypeError:
pass # Already disconnected
try:
self.upload_worker.upload_finished.disconnect(self.update_ui_after_upload)
except TypeError:
pass # Already disconnected
# Cancel the upload worker
self.upload_worker.cancel()
# Forcefully terminate the thread - requests doesn't support true cancellation
# Give it a tiny moment to clean up, then terminate
if not self.upload_worker.wait(100): # Wait 100ms max
self.upload_worker.terminate()
self.upload_worker.wait() # Wait for termination to complete
# Update UI immediately
self.file_label.setText("❌ Upload cancelled")
self.progress_bar.setFormat("Cancelled")
self.eta_label.setText("Cancelled")
self.cancel_button.setText("OK")
# Force UI to update now
QApplication.processEvents()
else:
# If not uploading, just close the window
self.close()
def handle_empty_response(self):
"""Handle empty response from server - known Catbox bug."""
self.progress_bar.setValue(100)
self.eta_label.setText("Server Bug Detected")
self.uploading = False
self.timer.stop()
# Create message box with appropriate options
msg_box = QMessageBox(self)
msg_box.setWindowTitle("Server Response Bug")
msg_box.setWindowIcon(QIcon(ico_path))
msg_box.setIcon(QMessageBox.Icon.Warning)
if self.litterbox_time:
msg_box.setText("Catbox server bug detected!")
msg_box.setInformativeText(
"The file was likely uploaded to Litterbox successfully, but the server "
"didn't return the link due to a known bug (especially common with GIFs).\n"
"It also may be corrupted, a re-upload would ensure the uploaded file is not corrupted.\n\n"
"What would you like to do?"
)
elif self.is_anonymous:
msg_box.setText("Catbox server bug detected!")
msg_box.setInformativeText(
"The file was likely uploaded anonymously to Catbox successfully, but the server "
"didn't return the link due to a known bug (especially common with GIFs).\n"
"It also may be corrupted, a re-upload would ensure the uploaded file is not corrupted.\n\n"
"What would you like to do?"
)
else:
msg_box.setText("Catbox server bug detected!")
msg_box.setInformativeText(
"The file was likely uploaded to your Catbox account successfully, but the server "
"didn't return the link due to a known bug (especially common with GIFs).\n"
"It also may be corrupted, a re-upload would ensure the uploaded file is not corrupted.\n\n"
"What would you like to do?"
)
# Add buttons based on upload type
reupload_btn = msg_box.addButton("Reupload File", QMessageBox.ButtonRole.ActionRole)
if not self.is_anonymous and not self.litterbox_time:
dashboard_btn = msg_box.addButton("Open Dashboard", QMessageBox.ButtonRole.ActionRole)
else:
dashboard_btn = None
cancel_btn = msg_box.addButton("Cancel", QMessageBox.ButtonRole.RejectRole)
msg_box.setDefaultButton(reupload_btn)
msg_box.exec()
clicked_button = msg_box.clickedButton()
if clicked_button == reupload_btn:
self.reupload_file()
elif dashboard_btn and clicked_button == dashboard_btn:
self.open_catbox_dashboard()
else:
# Cancel - just update UI to show the situation
self.file_label.setText("❌ Upload completed but link not returned due to server bug")