-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileTransfer_GUI.py
More file actions
427 lines (349 loc) · 15.2 KB
/
Copy pathfileTransfer_GUI.py
File metadata and controls
427 lines (349 loc) · 15.2 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
import os
import json
import uuid
import shutil
import asyncio
import websockets
from datetime import datetime, timezone
import threading
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QScrollArea, QPushButton, QSplitter, QTextEdit, QLineEdit,
QMessageBox, QFrame, QSizePolicy, QDialog
)
from PyQt5.QtCore import Qt, pyqtSignal
class DuplicateFileDialog(QDialog):
def __init__(self, file_name, parent=None):
super().__init__(parent)
self.setWindowTitle("File Already Exists")
self.action = "cancel"
layout = QVBoxLayout(self)
layout.addWidget(QLabel(f"A file with the name '{file_name}' already exists."))
layout.addWidget(QLabel("Do you want to overwrite it, or rename the new file?"))
self.name_edit = QLineEdit(file_name)
layout.addWidget(self.name_edit)
button_layout = QHBoxLayout()
self.btn_overwrite = QPushButton("Overwrite")
self.btn_rename = QPushButton("Rename")
self.btn_cancel = QPushButton("Cancel")
button_layout.addWidget(self.btn_overwrite)
button_layout.addWidget(self.btn_rename)
button_layout.addWidget(self.btn_cancel)
layout.addLayout(button_layout)
self.btn_overwrite.clicked.connect(self.on_overwrite)
self.btn_rename.clicked.connect(self.on_rename)
self.btn_cancel.clicked.connect(self.reject)
def on_overwrite(self):
self.action = "overwrite"
self.accept()
def on_rename(self):
self.action = "rename"
self.accept()
TRANSFERS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'transfers')
METADATA_FILE = os.path.join(TRANSFERS_DIR, 'metadata.json')
def load_metadata():
if not os.path.exists(METADATA_FILE):
return []
with open(METADATA_FILE, 'r', encoding='utf-8') as f:
try:
return json.load(f)
except json.JSONDecodeError:
return []
def save_metadata(metadata):
os.makedirs(TRANSFERS_DIR, exist_ok=True)
with open(METADATA_FILE, 'w', encoding='utf-8') as f:
json.dump(metadata, f, indent=2)
def notify_servers():
async def _notify():
try:
# Assuming the default port is 9734 based on config
async with websockets.connect("ws://127.0.0.1:9734") as ws:
msg = {
"id": str(uuid.uuid4()),
"type": "server_reload_files",
"source": "local_ui_drop",
"target": "server",
"timestamp": datetime.now(timezone.utc).isoformat(),
"flags": {},
"payload": {}
}
await ws.send(json.dumps(msg))
except Exception as e:
print(f"Could not notify server: {e}")
def run_notify():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(_notify())
threading.Thread(target=run_notify, daemon=True).start()
class FileWidget(QFrame):
clicked = pyqtSignal(dict)
deleteRequested = pyqtSignal(dict)
def __init__(self, file_info, parent=None):
super().__init__(parent)
self.file_info = file_info
self.setFocusPolicy(Qt.StrongFocus)
self.setFrameShape(QFrame.StyledPanel)
self.setFrameShadow(QFrame.Raised)
layout = QVBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)
self.name_label = QLabel(f"📄 {file_info.get('fileName', 'Unknown')}")
self.name_label.setStyleSheet("font-weight: bold;")
self.size_label = QLabel(f"Size: {file_info.get('fileSize', 0)} bytes")
self.type_label = QLabel(f"Type: {file_info.get('fileType', 'unknown')}")
layout.addWidget(self.name_label)
layout.addWidget(self.size_label)
layout.addWidget(self.type_label)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.setFocus()
self.clicked.emit(self.file_info)
super().mousePressEvent(event)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Delete:
self.deleteRequested.emit(self.file_info)
else:
super().keyPressEvent(event)
class FolderWidget(QFrame):
def __init__(self, folder_name, parent=None):
super().__init__(parent)
self.setFrameShape(QFrame.StyledPanel)
self.setFrameShadow(QFrame.Raised)
layout = QVBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)
self.name_label = QLabel(f"📁 {folder_name}")
self.name_label.setStyleSheet("font-weight: bold;")
layout.addWidget(self.name_label)
class DropArea(QScrollArea):
fileDropped = pyqtSignal(list)
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setWidgetResizable(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files = [url.toLocalFile() for url in event.mimeData().urls()]
self.fileDropped.emit(files)
class FileTransferUI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("File Transfer Manager")
self.resize(800, 600)
self.metadata = load_metadata()
main_widget = QWidget()
self.setCentralWidget(main_widget)
main_layout = QHBoxLayout(main_widget)
splitter = QSplitter(Qt.Horizontal)
main_layout.addWidget(splitter)
# Left Panel - File List (Drag and Drop enabled)
self.left_panel = QWidget()
left_layout = QVBoxLayout(self.left_panel)
self.drop_area = DropArea()
self.drop_area.fileDropped.connect(self.handle_files_dropped)
self.list_widget = QWidget()
self.list_layout = QVBoxLayout(self.list_widget)
self.list_layout.setAlignment(Qt.AlignTop)
self.drop_area.setWidget(self.list_widget)
left_layout.addWidget(QLabel("Files & Folders (Drag files here to add)"))
left_layout.addWidget(self.drop_area)
# Right Panel - Editor
self.right_panel = QWidget()
right_layout = QVBoxLayout(self.right_panel)
self.editor_title = QLabel("Edit File:")
self.file_name_editor = QLineEdit()
self.file_name_editor.setPlaceholderText("File Name")
self.text_editor = QTextEdit()
self.save_button = QPushButton("Save File")
self.save_button.clicked.connect(self.save_current_file)
right_layout.addWidget(self.editor_title)
right_layout.addWidget(QLabel("Name:"))
right_layout.addWidget(self.file_name_editor)
right_layout.addWidget(QLabel("Content (Text files only):"))
right_layout.addWidget(self.text_editor)
right_layout.addWidget(self.save_button)
splitter.addWidget(self.left_panel)
splitter.addWidget(self.right_panel)
self.current_file_info = None
self.refresh_list()
def refresh_list(self):
# Clear current list
while self.list_layout.count():
child = self.list_layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
self.metadata = load_metadata()
# Display files from metadata
for file_info in self.metadata:
fw = FileWidget(file_info)
fw.clicked.connect(self.load_file_to_editor)
fw.deleteRequested.connect(self.prompt_delete_file)
self.list_layout.addWidget(fw)
def handle_files_dropped(self, files):
os.makedirs(TRANSFERS_DIR, exist_ok=True)
for f in files:
if os.path.isfile(f):
file_name = os.path.basename(f)
existing_entry = next((item for item in self.metadata if item.get("fileName") == file_name), None)
if existing_entry:
dialog = DuplicateFileDialog(file_name, self)
if dialog.exec_() == QDialog.Rejected:
continue
action = dialog.action
new_file_name = dialog.name_edit.text()
if action == "rename" and new_file_name != file_name:
file_name = new_file_name
existing_entry = None
if existing_entry:
old_path = existing_entry.get("storedPath", "")
old_real_path = os.path.join(TRANSFERS_DIR, os.path.basename(old_path))
if os.path.exists(old_real_path):
try:
os.remove(old_real_path)
except OSError:
pass
self.metadata.remove(existing_entry)
file_id = f"dropped-{str(uuid.uuid4())[:8]}"
stored_name = f"{file_id}_{file_name}"
dest_path = os.path.join(TRANSFERS_DIR, stored_name)
shutil.copy2(f, dest_path)
# Create metadata entry
new_entry = {
"fileId": file_id,
"fileName": file_name,
"fileType": "application/octet-stream", # Best guess or use mimetypes
"fileSize": os.path.getsize(dest_path),
"storedPath": dest_path,
"source": "local_ui_drop",
"target": "server",
"sentAt": datetime.now(timezone.utc).isoformat(),
"storedAt": datetime.now(timezone.utc).isoformat(),
"storedBy": "local_ui"
}
self.metadata.insert(0, new_entry)
save_metadata(self.metadata)
self.refresh_list()
notify_servers()
def load_file_to_editor(self, file_info):
self.current_file_info = file_info
self.file_name_editor.setText(file_info.get("fileName", ""))
stored_path = file_info.get("storedPath", "")
# Resolve real path since storedPath might have old directory structure
real_path = os.path.join(TRANSFERS_DIR, os.path.basename(stored_path))
if os.path.exists(real_path):
try:
with open(real_path, 'r', encoding='utf-8') as f:
self.text_editor.setText(f.read())
except UnicodeDecodeError:
self.text_editor.setText("[Binary or non-UTF-8 file cannot be displayed]")
else:
self.text_editor.setText("[File not found on disk]")
def prompt_delete_file(self, file_info):
file_name = file_info.get("fileName", "Unknown")
reply = QMessageBox.question(self, 'Delete File',
f"Are you sure you want to delete '{file_name}'?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
stored_path = file_info.get("storedPath", "")
real_path = os.path.join(TRANSFERS_DIR, os.path.basename(stored_path))
if os.path.exists(real_path):
try:
os.remove(real_path)
except OSError:
pass
file_id = file_info.get("fileId")
self.metadata = [item for item in self.metadata if item.get("fileId") != file_id]
save_metadata(self.metadata)
if self.current_file_info and self.current_file_info.get("fileId") == file_id:
self.current_file_info = None
self.file_name_editor.clear()
self.text_editor.clear()
self.refresh_list()
notify_servers()
def save_current_file(self):
if not self.current_file_info:
return
new_name = self.file_name_editor.text()
stored_path = self.current_file_info.get("storedPath", "")
old_real_path = os.path.join(TRANSFERS_DIR, os.path.basename(stored_path))
# If name changed, we could decide to change storedPath as well, but usually
# it's best just to track the original format: fileId_fileName
file_id = self.current_file_info.get("fileId", "unknown")
new_basename = f"{file_id}_{new_name}"
new_real_path = os.path.join(TRANSFERS_DIR, new_basename)
# Rename physical file if name changed
if old_real_path != new_real_path and os.path.exists(old_real_path):
os.rename(old_real_path, new_real_path)
self.current_file_info["fileName"] = new_name
self.current_file_info["storedPath"] = new_real_path
# Write text content
if not self.text_editor.toPlainText().startswith("[Binary"):
with open(new_real_path, 'w', encoding='utf-8') as f:
f.write(self.text_editor.toPlainText())
# Update metadata size
if os.path.exists(new_real_path):
self.current_file_info["fileSize"] = os.path.getsize(new_real_path)
save_metadata(self.metadata)
self.refresh_list()
notify_servers()
QMessageBox.information(self, "Saved", "File saved and metadata updated.")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
# Simple styling
app.setStyleSheet("""
QMainWindow, QWidget {
background-color: #1e1e1e;
color: #d4d4d4;
}
QLineEdit, QTextEdit {
background-color: #252526;
color: #d4d4d4;
border: 1px solid #3c3c3c;
border-radius: 4px;
padding: 4px;
}
QPushButton {
background-color: #0e639c;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 8px 16px;
}
QPushButton:hover {
background-color: #1177bb;
}
QPushButton:pressed {
background-color: #094771;
}
FileWidget {
background-color: #2d2d30;
border-radius: 5px;
margin-bottom: 5px;
border: 1px solid #3e3e42;
color: #d4d4d4;
}
FileWidget:hover {
background-color: #3e3e42;
border: 1px solid #555555;
}
FolderWidget {
background-color: #252526;
border-radius: 5px;
margin-bottom: 5px;
border: 1px solid #3e3e42;
color: #d4d4d4;
}
DropArea {
border: 2px dashed #555;
background-color: #1e1e1e;
}
QSplitter::handle {
background-color: #3c3c3c;
}
""")
window = FileTransferUI()
window.show()
sys.exit(app.exec_())