forked from maagic6/SDIMV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_handler.py
116 lines (104 loc) · 5.15 KB
/
file_handler.py
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
import os, requests, shutil
from pathlib import Path
from urllib.parse import unquote
from PyQt6.QtWidgets import QFileDialog, QListWidgetItem
class FileHandler:
def __init__(self, main_window):
self.main_window = main_window
def downloadImage(self, url):
try:
response = requests.get(url.toString())
if response.status_code == 200:
# get the file extension from the content-type header
url_filename = os.path.basename(unquote(url.toString()))
invalid_characters = ['<', '>', ':', '"', '/', '\\', '|', '?', '*']
for char in invalid_characters:
url_filename = url_filename.replace(char, '_')
file_extension = response.headers.get('Content-Type').split('/')[-1]
# create a unique filename in the current working directory
filename = f"{url_filename}.{file_extension}"
save_path = os.path.join(os.getcwd(), f"{url_filename}.{file_extension}")
# save the image locally
with open(filename, 'wb') as file:
file.write(response.content)
return save_path
else:
print(f"Failed to download image. HTTP Status Code: {response.status_code}")
except Exception as e:
print(f"Error downloading image: {e}")
return None
def copyTempImage(self, temp_file_path):
try:
# create a copy of the image file in the current working directory
copied_path = os.path.join(os.getcwd(), os.path.basename(temp_file_path))
shutil.copyfile(temp_file_path, copied_path)
return copied_path
except Exception as e:
print(f"Error copying temp image: {e}")
return None
def openFileDialog(self):
filenames, _ = QFileDialog.getOpenFileNames(
self.main_window,
"Select image files",
"",
"Images and videos (*.png *.jpg *.gif *.webp *.mp4)"
)
if filenames:
new_files = []
new_files = [filename for filename in filenames if not self.isFileInList(filename)]
self.updateFileList(new_files)
def updateFileList(self, file_paths):
for file_path in file_paths:
item = QListWidgetItem(file_path)
self.main_window.fileList.addItem(item)
if self.main_window.fileList.count() > 0:
last_item = self.main_window.fileList.item(self.main_window.fileList.count() - 1)
self.main_window.fileList.setCurrentItem(last_item)
self.main_window.viewMetadata(last_item)
else:
self.main_window.viewMetadata(None)
def clearFileList(self):
self.main_window.fileList.clear()
#self.main_window.imageScene.clear()
self.main_window.selectedFile.clear()
for _, widget, _ in self.main_window.widgetInfo:
widget.clear()
self.main_window.viewMetadata(None)
def removeSelectedItem(self):
selectedItem = self.main_window.fileList.currentItem()
if selectedItem:
selectedIndex = self.main_window.fileList.row(selectedItem)
self.main_window.fileList.takeItem(selectedIndex)
# if last index
if selectedIndex == (self.main_window.fileList.count()):
if self.main_window.fileList.count() > 0:
last_item = self.main_window.fileList.item(self.main_window.fileList.count() - 1)
self.main_window.fileList.setCurrentItem(last_item)
self.main_window.viewMetadata(last_item)
else:
self.main_window.viewMetadata(None)
else:
self.main_window.viewMetadata(self.main_window.fileList.item(selectedIndex))
def getFilesFromFolder(self, path):
folder_path = Path(path)
png_files = list(folder_path.rglob('*.[pP][nN][gG]'))
jpg_files = list(folder_path.rglob('*.[jJ][pP][gG]'))
webp_files = list(folder_path.rglob('*.[wW][eE][bB][pP]'))
gif_files = list(folder_path.rglob('*.[gG][iI][fF]'))
mp4_files = list(folder_path.rglob('*.[mM][pP][4]'))
png_files = [str(file_path).replace('\\', '/') for file_path in png_files]
jpg_files = [str(file_path).replace('\\', '/') for file_path in jpg_files]
webp_files = [str(file_path).replace('\\', '/') for file_path in webp_files]
gif_files = [str(file_path).replace('\\', '/') for file_path in gif_files]
mp4_files = [str(file_path).replace('\\', '/') for file_path in mp4_files]
image_files = set(png_files + jpg_files + webp_files + gif_files + mp4_files)
unique_image_files = image_files
return unique_image_files
def isFileInList(self, file_path):
for row in range(self.main_window.fileList.count()):
item = self.main_window.fileList.item(row)
if item.text() == file_path:
return True
return False
def getFileList(self):
return [self.main_window.fileList.item(row).text() for row in range(self.main_window.fileList.count())]