-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
393 lines (356 loc) · 16.6 KB
/
Program.cs
File metadata and controls
393 lines (356 loc) · 16.6 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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using MediaDevices;
namespace iPhoneVideoBackup
{
class Program
{
// Define a constant array of file extensions to handle
private static readonly string[] SupportedExtensions = { "*.MOV", "*.MP4", "*.AVI", "*.JPG" };
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
try
{
// Parse command-line arguments for destination directory and device type
string destinationRoot = null;
string deviceType = null;
for (int i = 0; i < args.Length; i++)
{
if ((args[i].Equals("--dest", StringComparison.OrdinalIgnoreCase) || args[i].Equals("/dest", StringComparison.OrdinalIgnoreCase)) && i + 1 < args.Length)
{
destinationRoot = args[i + 1];
i++;
continue;
}
if ((args[i].Equals("--device", StringComparison.OrdinalIgnoreCase) || args[i].Equals("/device", StringComparison.OrdinalIgnoreCase)) && i + 1 < args.Length)
{
deviceType = args[i + 1].Trim().ToLowerInvariant();
i++;
continue;
}
}
// If not provided, prompt the user for the destination directory
if (string.IsNullOrWhiteSpace(destinationRoot))
{
Console.Write("📁 Enter destination directory (required): ");
var inputDest = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(inputDest))
{
destinationRoot = inputDest.Trim();
}
else
{
Console.WriteLine("❌ Destination directory is required. Exiting.");
return;
}
}
// Validate the supplied path and drive
try
{
// Check if path is absolute
if (!Path.IsPathRooted(destinationRoot))
{
Console.WriteLine("❌ Please provide an absolute path (e.g., C:\\Backup). Exiting.");
return;
}
// Check if drive exists
var root = Path.GetPathRoot(destinationRoot);
if (string.IsNullOrWhiteSpace(root) || !Directory.GetLogicalDrives().Any(d => string.Equals(d.TrimEnd('\\'), root.TrimEnd('\\'), StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine($"❌ Drive '{root}' does not exist. Exiting.");
return;
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ Invalid path: {ex.Message} Exiting.");
return;
}
// Prompt for device type if not set
if (string.IsNullOrWhiteSpace(deviceType))
{
Console.WriteLine("Select device type:");
Console.WriteLine(" 1. iPhone");
Console.WriteLine(" 2. Pixel");
Console.Write("Enter 1 or 2: ");
var deviceChoice = Console.ReadLine();
if (deviceChoice == "1") deviceType = "iphone";
else if (deviceChoice == "2") deviceType = "pixel";
else
{
Console.WriteLine("❌ Invalid device type selection. Exiting.");
return;
}
}
// Append today's date as a subfolder in yyyy-MM-dd format
var today = DateTime.Today.ToString("yyyy-MM-dd");
destinationRoot = Path.Combine(destinationRoot, today);
// Create the destination directory if it doesn't exist
Directory.CreateDirectory(destinationRoot);
// List all detected devices and their friendly names
var devices = MediaDevice.GetDevices();
Console.WriteLine($"Devices found: {devices.Count()}");
foreach (var mediaDevice in devices)
{
Console.WriteLine($"Device: {mediaDevice.FriendlyName}");
}
// Find the connected device by its friendly name
MediaDevice device = null;
if (deviceType == "iphone")
{
device = devices.FirstOrDefault(d => d.FriendlyName.IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0);
}
else if (deviceType == "pixel")
{
device = devices.FirstOrDefault(d => d.FriendlyName.IndexOf("Pixel", StringComparison.OrdinalIgnoreCase) >= 0);
}
if (device == null)
{
Console.WriteLine($"❌ {deviceType.First().ToString().ToUpper() + deviceType.Substring(1)} not found.");
return; // Exit if no device is found
}
// Connect to the device BEFORE accessing files
try
{
device.Connect();
Console.WriteLine($"✅ Connected to {deviceType} device.");
}
catch (Exception ex)
{
Console.WriteLine($"❌ Failed to connect: {ex.Message}");
return;
}
if (!device.IsConnected)
{
Console.WriteLine("❌ Device is not connected.");
return;
}
// Now it's safe to access files and directories
// Path to the DCIM folder where photos and videos are stored
string dcimPath;
if (deviceType == "pixel")
{
dcimPath = @"\Internal shared storage\DCIM";
}
else
{
dcimPath = @"\Internal Storage\DCIM";
}
var videoFiles = new List<(string sourcePath, string fileName, long size)>();
// Prompt the user to commence copying files
Console.Write("❓ Do you want to commence copying files? (Y/N): ");
var startResponse = Console.ReadLine();
if (startResponse == null || !startResponse.Trim().Equals("Y", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("⏹️ Operation cancelled by user.");
return;
}
Console.WriteLine($"\n📂 Starting to copy files from {dcimPath} to {destinationRoot}...\n");
// Enumerate all directories in the DCIM folder
foreach (var folder in device.GetDirectories(dcimPath))
{
// Find files matching any of the supported extensions
foreach (var extension in SupportedExtensions)
{
foreach (var file in device.GetFiles(folder, extension))
{
var fileInfo = device.GetFileInfo(file);
// Add file details (path, name, size) to the list
videoFiles.Add((file, Path.GetFileName(file), (long)fileInfo.Length)); // Explicitly cast ulong to long
}
}
}
// Exit if no video files are found
if (videoFiles.Count == 0)
{
Console.WriteLine("⚠️ No supported files found.");
return;
}
// Sort videoFiles by fileName before copying
videoFiles = videoFiles.OrderBy(f => f.fileName).ToList();
// Check if there is sufficient space on the destination drive
long totalSizeNeeded = videoFiles.Sum(f => f.size);
if (!CheckDriveSpace(destinationRoot, totalSizeNeeded))
{
Console.Write("\n❓ Insufficient space on destination drive. Do you still want to continue? (Y/N): ");
var continueResponse = Console.ReadLine();
if (continueResponse == null || !continueResponse.Trim().Equals("Y", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("⏹️ Operation cancelled by user.");
device.Disconnect();
return;
}
}
// Variables to track verification results
List<string> verified = new List<string>();
List<string> failed = new List<string>();
try
{
CopyFiles(device, videoFiles, destinationRoot);
}
catch (Exception ex)
{
Console.WriteLine($"\n⚠️ Transfer was interrupted: {ex.Message}");
}
finally
{
// Verify that the copied files match the originals
VerifyFiles(videoFiles, destinationRoot, out verified, out failed);
// Handle deletion of successfully copied files based on user input
HandleDeletion(device, verified);
}
// Disconnect the device
device.Disconnect();
Console.WriteLine("\n🎉 Done.");
}
catch (Exception ex)
{
// Catch and display any errors that occur during the process
Console.WriteLine($"❌ An error occurred: {ex.Message}");
}
finally
{
// Wait for the user to press Enter before closing the program
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
}
}
// Method to compute SHA256 checksum of a file
static string ComputeFileChecksum(string filePath)
{
using (var sha256 = SHA256.Create())
using (var stream = File.OpenRead(filePath))
{
var hash = sha256.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
// Method to compute SHA256 checksum of a file on the device
static string ComputeDeviceFileChecksum(MediaDevice device, string sourcePath)
{
using (var sha256 = SHA256.Create())
using (var ms = new MemoryStream())
{
device.DownloadFile(sourcePath, ms);
ms.Position = 0;
var hash = sha256.ComputeHash(ms);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
// Modified CopyFiles method to show size and average transfer speed
static void CopyFiles(MediaDevice device, List<(string sourcePath, string fileName, long size)> videoFiles, string destinationRoot)
{
foreach (var (sourcePath, fileName, size, index) in videoFiles.Select((file, index) => (file.sourcePath, file.fileName, file.size, index)))
{
var destPath = Path.Combine(destinationRoot, fileName);
// If file exists and size matches, skip copying
if (File.Exists(destPath))
{
var destFileInfo = new FileInfo(destPath);
if (destFileInfo.Length == size)
{
Console.WriteLine($"Skipped (already copied): {fileName} ({index + 1}/{videoFiles.Count}, {((index + 1) * 100 / videoFiles.Count):F2}%)");
Console.Out.Flush();
continue;
}
}
var sw = System.Diagnostics.Stopwatch.StartNew();
using (var destStream = File.Create(destPath))
{
device.DownloadFile(sourcePath, destStream);
}
sw.Stop();
double sizeMB = size / (1024.0 * 1024.0);
double seconds = sw.Elapsed.TotalSeconds;
double speedMBps = seconds > 0 ? sizeMB / seconds : 0;
Console.WriteLine($"Copied: {fileName} ({index + 1}/{videoFiles.Count}, {((index + 1) * 100 / videoFiles.Count):F2}%) | Size: {sizeMB:F2} MB | Speed: {speedMBps:F2} MB/s");
Console.Out.Flush();
}
}
// Method to verify that copied files match the originals in size
static void VerifyFiles(List<(string sourcePath, string fileName, long size)> videoFiles, string destinationRoot, out List<string> verified, out List<string> failed)
{
Console.WriteLine("\n🔍 Verifying copied files...");
verified = new List<string>();
failed = new List<string>();
foreach (var (sourcePath, fileName, size) in videoFiles)
{
var destPath = Path.Combine(destinationRoot, fileName);
// Check if the file exists and its size matches the original
if (File.Exists(destPath) && new FileInfo(destPath).Length == size)
{
verified.Add(sourcePath); // Add to verified list
}
else
{
failed.Add(fileName); // Add to failed list
}
}
// Display verification results
Console.WriteLine($"\n✅ {verified.Count} file(s) verified.");
if (failed.Count > 0)
{
Console.WriteLine("⚠️ Some files failed to verify:");
failed.ForEach(f => Console.WriteLine($" - {f}"));
}
}
// Method to check if there is sufficient space on the destination drive
static bool CheckDriveSpace(string destinationPath, long requiredBytes)
{
try
{
var driveInfo = new DriveInfo(Path.GetPathRoot(destinationPath));
long availableBytes = driveInfo.AvailableFreeSpace;
double requiredGB = requiredBytes / (1024.0 * 1024.0 * 1024.0);
double availableGB = availableBytes / (1024.0 * 1024.0 * 1024.0);
Console.WriteLine($"\n💾 Space Check:");
Console.WriteLine($" Required: {requiredGB:F2} GB");
Console.WriteLine($" Available: {availableGB:F2} GB");
if (availableBytes < requiredBytes)
{
Console.WriteLine($" ⚠️ Insufficient space! Short by {(requiredBytes - availableBytes) / (1024.0 * 1024.0 * 1024.0):F2} GB");
return false;
}
else
{
Console.WriteLine($" ✅ Sufficient space available.");
return true;
}
}
catch (Exception ex)
{
Console.WriteLine($"⚠️ Could not check drive space: {ex.Message}");
return true; // Assume sufficient space if check fails
}
}
// Method to handle deletion of successfully copied files from the iPhone
static void HandleDeletion(MediaDevice device, List<string> verified)
{
if (verified.Count > 0)
{
// Prompt the user to confirm deletion
Console.Write("\n❓ Delete successfully copied files from iPhone? (Y/N): ");
var response = Console.ReadLine();
if (response?.Trim().ToUpper() == "Y")
{
Console.WriteLine("🗑️ Deleting files...");
foreach (var path in verified)
{
// Delete the file from the iPhone
device.DeleteFile(path);
Console.WriteLine($"Deleted: {Path.GetFileName(path)}");
Console.Out.Flush();
}
}
else
{
Console.WriteLine("⏭️ Skipped deletion.");
}
}
}
}
}