-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_transfer.py
More file actions
687 lines (582 loc) · 22.3 KB
/
Copy pathfile_transfer.py
File metadata and controls
687 lines (582 loc) · 22.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
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
#!/usr/bin/env python3
"""
QuickDrop - Simple Mac ↔ Android File Transfer
Run this on your Mac, open the URL on your Android phone's browser.
"""
import os
import socket
import qrcode
import io
import base64
from pathlib import Path
from flask import Flask, render_template_string, request, send_from_directory, jsonify, Response
from werkzeug.utils import secure_filename
# Configuration
SHARE_FOLDER = os.path.expanduser("~/Downloads/PhoneTransfer")
PORT = 5000
# Create share folder if it doesn't exist
os.makedirs(SHARE_FOLDER, exist_ok=True)
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 * 1024 # 10GB max upload
def get_local_ip():
"""Get the local IP address of this machine"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
except Exception:
ip = '127.0.0.1'
finally:
s.close()
return ip
def generate_qr_code(url):
"""Generate QR code as base64 string"""
qr = qrcode.QRCode(version=1, box_size=10, border=2)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buffer = io.BytesIO()
img.save(buffer, format='PNG')
return base64.b64encode(buffer.getvalue()).decode()
def get_file_size_str(size_bytes):
"""Convert bytes to human readable string"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024:
return f"{size_bytes:.1f} {unit}"
size_bytes /= 1024
return f"{size_bytes:.1f} TB"
HTML_TEMPLATE = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QuickDrop</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
min-height: 100vh;
color: #fff;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
header {
text-align: center;
margin-bottom: 30px;
}
h1 {
font-size: 2.5rem;
background: linear-gradient(90deg, #00d9ff, #00ff88);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 10px;
}
.subtitle {
color: #888;
font-size: 0.95rem;
}
.card {
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 25px;
margin-bottom: 20px;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.card h2 {
font-size: 1.2rem;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.upload-zone {
border: 2px dashed rgba(0, 217, 255, 0.4);
border-radius: 12px;
padding: 50px 20px;
text-align: center;
transition: all 0.3s ease;
cursor: pointer;
}
.upload-zone:hover, .upload-zone.dragover {
border-color: #00d9ff;
background: rgba(0, 217, 255, 0.1);
}
.upload-zone p {
color: #aaa;
margin-top: 10px;
}
.upload-icon {
font-size: 3rem;
margin-bottom: 10px;
}
input[type="file"] {
display: none;
}
.btn {
background: linear-gradient(90deg, #00d9ff, #00ff88);
color: #000;
border: none;
padding: 12px 30px;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(0, 217, 255, 0.4);
}
.file-list {
list-style: none;
}
.file-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px;
background: rgba(255, 255, 255, 0.03);
border-radius: 8px;
margin-bottom: 10px;
transition: background 0.2s;
}
.file-item:hover {
background: rgba(255, 255, 255, 0.08);
}
.file-info {
display: flex;
align-items: center;
gap: 15px;
flex: 1;
min-width: 0;
}
.file-icon {
font-size: 1.5rem;
width: 40px;
text-align: center;
}
.file-details {
flex: 1;
min-width: 0;
}
.file-name {
font-weight: 500;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.file-size {
color: #888;
font-size: 0.85rem;
}
.download-btn {
background: rgba(0, 255, 136, 0.2);
color: #00ff88;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
transition: all 0.2s;
text-decoration: none;
}
.download-btn:hover {
background: rgba(0, 255, 136, 0.3);
}
.progress-container {
display: none;
margin-top: 20px;
}
.progress-bar {
height: 8px;
background: rgba(255, 255, 255, 0.1);
border-radius: 4px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #00d9ff, #00ff88);
width: 0%;
transition: width 0.3s;
}
.progress-text {
text-align: center;
margin-top: 10px;
color: #888;
}
.empty-state {
text-align: center;
padding: 40px;
color: #666;
}
.toast {
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: #00ff88;
color: #000;
padding: 15px 30px;
border-radius: 8px;
font-weight: 500;
opacity: 0;
transition: all 0.3s ease;
z-index: 1000;
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
@media (max-width: 600px) {
h1 { font-size: 1.8rem; }
.card { padding: 15px; }
.upload-zone { padding: 30px 15px; }
.file-item { flex-direction: column; gap: 10px; align-items: flex-start; }
.download-btn { width: 100%; text-align: center; }
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>⚡ QuickDrop</h1>
<p class="subtitle">Fast file transfer between Mac & Android</p>
</header>
<div class="card">
<h2>📤 Upload to Mac</h2>
<div class="upload-zone" id="uploadZone">
<div class="upload-icon">📁</div>
<button class="btn" onclick="document.getElementById('fileInput').click()">Select Files</button>
<p>or drag and drop files here</p>
</div>
<input type="file" id="fileInput" multiple>
<div class="progress-container" id="progressContainer">
<div class="progress-bar">
<div class="progress-fill" id="progressFill"></div>
</div>
<p class="progress-text" id="progressText">Uploading...</p>
</div>
</div>
<div class="card">
<h2>📥 Download from Mac</h2>
<ul class="file-list" id="fileList">
{% if files %}
{% for file in files %}
<li class="file-item">
<div class="file-info">
<span class="file-icon">{{ file.icon }}</span>
<div class="file-details">
<div class="file-name">{{ file.name }}</div>
<div class="file-size">{{ file.size }}</div>
</div>
</div>
<a href="/download/{{ file.name | urlencode }}" class="download-btn">Download</a>
</li>
{% endfor %}
{% else %}
<div class="empty-state">
<p>No files in shared folder yet</p>
<p style="font-size: 0.85rem; margin-top: 5px;">{{ share_folder }}</p>
</div>
{% endif %}
</ul>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
const uploadZone = document.getElementById('uploadZone');
const fileInput = document.getElementById('fileInput');
const progressContainer = document.getElementById('progressContainer');
const progressFill = document.getElementById('progressFill');
const progressText = document.getElementById('progressText');
const CHUNK_SIZE = 10 * 1024 * 1024; // 10MB chunks
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 3000);
}
function formatSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
}
async function uploadFileChunked(file) {
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
const fileId = Date.now() + '_' + Math.random().toString(36).substr(2, 9);
progressContainer.style.display = 'block';
progressFill.style.width = '0%';
progressText.textContent = `Preparing ${file.name}...`;
for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) {
const start = chunkIndex * CHUNK_SIZE;
const end = Math.min(start + CHUNK_SIZE, file.size);
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append('chunk', chunk);
formData.append('filename', file.name);
formData.append('fileId', fileId);
formData.append('chunkIndex', chunkIndex);
formData.append('totalChunks', totalChunks);
formData.append('fileSize', file.size);
try {
const response = await fetch('/upload_chunk', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Upload failed');
}
const percent = Math.round(((chunkIndex + 1) / totalChunks) * 100);
progressFill.style.width = percent + '%';
progressText.textContent = `Uploading ${file.name}... ${percent}% (${formatSize(end)} / ${formatSize(file.size)})`;
} catch (error) {
showToast('✗ Upload failed: ' + error.message);
progressContainer.style.display = 'none';
return false;
}
}
return true;
}
async function uploadFiles(files) {
if (files.length === 0) return;
let successCount = 0;
for (let file of files) {
const success = await uploadFileChunked(file);
if (success) successCount++;
}
if (successCount === files.length) {
showToast(`✓ ${successCount} file(s) uploaded!`);
setTimeout(() => location.reload(), 1000);
} else {
showToast(`✗ Some uploads failed`);
}
progressContainer.style.display = 'none';
}
// File input change
fileInput.addEventListener('change', (e) => {
uploadFiles(e.target.files);
});
// Drag and drop
uploadZone.addEventListener('dragover', (e) => {
e.preventDefault();
uploadZone.classList.add('dragover');
});
uploadZone.addEventListener('dragleave', () => {
uploadZone.classList.remove('dragover');
});
uploadZone.addEventListener('drop', (e) => {
e.preventDefault();
uploadZone.classList.remove('dragover');
uploadFiles(e.dataTransfer.files);
});
</script>
</body>
</html>
'''
def get_file_icon(filename):
"""Return emoji icon based on file extension"""
ext = filename.lower().split('.')[-1] if '.' in filename else ''
icons = {
'pdf': '📕',
'doc': '📘', 'docx': '📘',
'xls': '📗', 'xlsx': '📗',
'ppt': '📙', 'pptx': '📙',
'jpg': '🖼️', 'jpeg': '🖼️', 'png': '🖼️', 'gif': '🖼️', 'webp': '🖼️',
'mp4': '🎬', 'mov': '🎬', 'avi': '🎬', 'mkv': '🎬',
'mp3': '🎵', 'wav': '🎵', 'flac': '🎵', 'm4a': '🎵',
'zip': '📦', 'rar': '📦', '7z': '📦', 'tar': '📦', 'gz': '📦',
'txt': '📄', 'md': '📄',
'py': '🐍', 'js': '💛', 'html': '🌐', 'css': '🎨',
'apk': '🤖',
}
return icons.get(ext, '📄')
@app.route('/')
def index():
files = []
try:
for f in os.listdir(SHARE_FOLDER):
filepath = os.path.join(SHARE_FOLDER, f)
if os.path.isfile(filepath):
files.append({
'name': f,
'size': get_file_size_str(os.path.getsize(filepath)),
'icon': get_file_icon(f)
})
except Exception as e:
print(f"Error listing files: {e}")
files.sort(key=lambda x: x['name'].lower())
return render_template_string(HTML_TEMPLATE, files=files, share_folder=SHARE_FOLDER)
@app.route('/upload', methods=['POST'])
def upload():
if 'files' not in request.files:
return jsonify({'error': 'No files'}), 400
files = request.files.getlist('files')
uploaded = []
for file in files:
if file.filename:
filename = secure_filename(file.filename)
# Handle duplicate names
filepath = os.path.join(SHARE_FOLDER, filename)
if os.path.exists(filepath):
base, ext = os.path.splitext(filename)
counter = 1
while os.path.exists(filepath):
filename = f"{base}_{counter}{ext}"
filepath = os.path.join(SHARE_FOLDER, filename)
counter += 1
file.save(filepath)
uploaded.append(filename)
return jsonify({'uploaded': uploaded})
# Temporary storage for chunked uploads
CHUNK_FOLDER = os.path.join(SHARE_FOLDER, '.chunks')
os.makedirs(CHUNK_FOLDER, exist_ok=True)
@app.route('/upload_chunk', methods=['POST'])
def upload_chunk():
"""Handle chunked file uploads for large files"""
try:
chunk = request.files.get('chunk')
filename = request.form.get('filename', 'unknown')
file_id = request.form.get('fileId')
chunk_index = int(request.form.get('chunkIndex', 0))
total_chunks = int(request.form.get('totalChunks', 1))
if not chunk or not file_id:
return jsonify({'error': 'Missing data'}), 400
# Save chunk to temp folder
chunk_dir = os.path.join(CHUNK_FOLDER, file_id)
os.makedirs(chunk_dir, exist_ok=True)
chunk_path = os.path.join(chunk_dir, f'chunk_{chunk_index:06d}')
chunk.save(chunk_path)
# Check if all chunks received
received_chunks = len([f for f in os.listdir(chunk_dir) if f.startswith('chunk_')])
if received_chunks == total_chunks:
# All chunks received, reassemble file
safe_filename = secure_filename(filename)
# Handle duplicate names
final_path = os.path.join(SHARE_FOLDER, safe_filename)
if os.path.exists(final_path):
base, ext = os.path.splitext(safe_filename)
counter = 1
while os.path.exists(final_path):
safe_filename = f"{base}_{counter}{ext}"
final_path = os.path.join(SHARE_FOLDER, safe_filename)
counter += 1
# Reassemble chunks
with open(final_path, 'wb') as outfile:
for i in range(total_chunks):
chunk_path = os.path.join(chunk_dir, f'chunk_{i:06d}')
with open(chunk_path, 'rb') as infile:
while True:
data = infile.read(1024 * 1024) # Read 1MB at a time
if not data:
break
outfile.write(data)
# Clean up chunks
import shutil
shutil.rmtree(chunk_dir, ignore_errors=True)
return jsonify({'status': 'complete', 'filename': safe_filename})
return jsonify({'status': 'chunk_received', 'received': received_chunks, 'total': total_chunks})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/download/<path:filename>')
def download(filename):
"""Optimized download with chunked streaming and resume support"""
# URL decode the filename and sanitize path traversal attempts
from urllib.parse import unquote
filename = unquote(filename)
# Security: prevent directory traversal
if '..' in filename or filename.startswith('/'):
return "Invalid filename", 400
filepath = os.path.join(SHARE_FOLDER, filename)
# Check file exists and is within share folder
if not os.path.exists(filepath) or not os.path.isfile(filepath):
return "File not found", 404
real_share = os.path.realpath(SHARE_FOLDER)
real_file = os.path.realpath(filepath)
if not real_file.startswith(real_share):
return "Access denied", 403
file_size = os.path.getsize(filepath)
chunk_size = 1024 * 1024 # 1MB chunks
# Handle range requests (for resume support)
range_header = request.headers.get('Range')
start = 0
end = file_size - 1
if range_header:
try:
range_match = range_header.replace('bytes=', '').split('-')
start = int(range_match[0]) if range_match[0] else 0
end = int(range_match[1]) if range_match[1] else file_size - 1
except (ValueError, IndexError):
pass
content_length = end - start + 1
def generate():
with open(filepath, 'rb') as f:
f.seek(start)
remaining = content_length
while remaining > 0:
read_size = min(chunk_size, remaining)
chunk = f.read(read_size)
if not chunk:
break
remaining -= len(chunk)
yield chunk
# Encode filename for Content-Disposition header
from urllib.parse import quote
encoded_filename = quote(filename)
headers = {
'Content-Disposition': f"attachment; filename*=UTF-8''{encoded_filename}",
'Content-Length': str(content_length),
'Content-Range': f'bytes {start}-{end}/{file_size}',
'Accept-Ranges': 'bytes',
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no'
}
status_code = 206 if range_header else 200
return Response(
generate(),
status=status_code,
mimetype='application/octet-stream',
headers=headers
)
def print_banner(url, qr_base64):
"""Print startup banner"""
print("\n" + "="*50)
print(" ⚡ QuickDrop - File Transfer Server")
print("="*50)
print(f"\n 📁 Shared folder: {SHARE_FOLDER}")
print(f"\n 🌐 Open this URL on your Android phone:")
print(f"\n {url}")
print(f"\n 📱 Or scan the QR code below:\n")
# Generate ASCII QR code for terminal
qr = qrcode.QRCode(version=1, box_size=1, border=1)
qr.add_data(url)
qr.make(fit=True)
qr.print_ascii(invert=True)
print("\n" + "="*50)
print(" Press Ctrl+C to stop the server")
print("="*50 + "\n")
if __name__ == '__main__':
ip = get_local_ip()
url = f"http://{ip}:{PORT}"
qr_base64 = generate_qr_code(url)
print_banner(url, qr_base64)
# Use Waitress production server (MUCH faster than Flask dev server)
try:
from waitress import serve
print(" 🚀 Running with Waitress (production server)")
print(" Expected speeds: 30-100+ MB/s on 5GHz WiFi\n")
serve(app, host='0.0.0.0', port=PORT, threads=8,
channel_timeout=120, recv_bytes=262144, send_bytes=262144)
except ImportError:
print(" ⚠️ Waitress not found, using Flask dev server (slower)")
print(" Install waitress for faster transfers: pip3 install waitress\n")
app.run(host='0.0.0.0', port=PORT, debug=False, threaded=True)