-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcloud_sync.ps1
360 lines (299 loc) · 11.7 KB
/
cloud_sync.ps1
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
<#
.SYNOPSIS
Ensure all the files are uploaded from the phone to the cloud.
Copy any missing files to a destination folder which is expected to be a cloud
sync folder.
#>
param(
[Parameter(Mandatory)]
[string]$phoneName,
[string]$phoneFolderPath="<configured>",
[string]$cloudFolderPath="<configured>",
[string]$destinationFolderPath="<configured>",
[string]$filter=".(jpg|jpeg|mp4)$",
[string]$choco="choco",
[string]$winmerge="WinMergeU",
[string]$tempDir=$env:Temp,
# See cloud_sync_config.example.ps1
[string]$configFile="cloud_sync_config.ps1",
# TODO add multiprocessing [uint32]$jobCount=$(Get-ComputerInfo -Property CsProcessors).CsProcessors.NumberOfCores,
[switch]$confirmCopy=$false,
[switch]$dryRun=$false
)
#==============================================================================#
# FUNCTIONS
#==============================================================================#
function Get-ShellProxy
{
if (-not $global:ShellProxy)
{
$global:ShellProxy = new-object -com Shell.Application
}
$global:ShellProxy
}
function Get-Phone
{
param($phoneName)
$shell = Get-ShellProxy
# 17 (0x11) = ssfDRIVES from the ShellSpecialFolderConstants (https://msdn.microsoft.com/en-us/library/windows/desktop/bb774096(v=vs.85).aspx)
# => "My Computer" — the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel.
# This folder can also contain mapped network drives.
$shellItem = $shell.NameSpace(17).self
$phone = $shellItem.GetFolder.items() | where { $_.name -eq $phoneName }
return $phone
}
function Get-SubFolder
{
param($parent,[string]$path)
$pathParts = @( $path.Split([system.io.path]::DirectorySeparatorChar) )
$current = $parent
foreach ($pathPart in $pathParts)
{
if ($pathPart)
{
$current = $current.GetFolder.items() | where { $_.Name -eq $pathPart }
}
}
return $current
}
<#
.SYNOPSIS
Return a possible equivalent of the name of `$phoneFile` in the cloud when
"Keep file names as in the device" is disabled.
#>
function GetMegaFilename
{
param($phoneFile)
$extension = [System.IO.Path]::GetExtension($phoneFile.name)
$megaName = $phoneFile.ExtendedProperty("System.DateModified").ToString("yyyy-MM-dd HH.mm.ss")
$megaFilename = "$megaName$extension"
return $megaFilename
}
function EnsureChocoIsInstalled
{
param([string]$choco)
$command = Get-Command -Name $choco -ErrorAction SilentlyContinue
if ($command -eq $null) {
Write-Output "'$choco' is missing. Let's install it."
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
$chocoInstaller = "https://community.chocolatey.org/install.ps1"
$arguments = "Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('$chocoInstaller'))"
Start-Process powershell -ArgumentList $arguments -Verb RunAs -Wait
} else {
Write-Output "'$choco' is installed."
}
}
function EnsureWinMergeIsInstalled
{
param([string]$choco, [string]$winmerge)
$command = Get-Command -Name $winmerge -ErrorAction SilentlyContinue
if ($command -eq $null) {
Write-Output "'$winmerge' is missing. Let's install it."
EnsureChocoIsInstalled $choco
# handle the case when choco was freshly installed
$command = Get-Command -Name $choco -ErrorAction SilentlyContinue
if ($command -eq $null) {
$choco = [IO.Path]::Combine($env:ChocolateyInstall, "choco")
$command = Get-Command -Name $choco -ErrorAction SilentlyContinue
if ($command -eq $null) {
throw "Cannot find '$choco'"
}
}
Start-Process $choco -ArgumentList "install winmerge" –Verb RunAs -Wait
} else {
Write-Output "'$winmerge' is installed."
}
}
function EnsurePrerequisites
{
param([string]$choco, [string]$winmerge)
EnsureWinMergeIsInstalled $choco $winmerge
}
function ClearTempDirectory
{
param($tempDir, $filter)
Write-Output "Cleaning '$tempDir'..."
Get-ChildItem -Path $tempDir -File | where { $_.Name -match $filter } | foreach { $_.Delete()}
Write-Output "Done"
}
function GetTempFileName
{
param($phoneFile, $tempDir)
return [IO.Path]::Combine($tempDir, $phoneFile.Name)
}
function CreateTempFile
{
param($phoneFile, $tempDir)
$shell = Get-ShellProxy
$destinationFolder = $shell.Namespace($tempDir).self
$tempFile = GetTempFileName $phoneFile $tempDir
$destinationFolder.GetFolder.CopyHere($phoneFile)
Write-Debug "Created temporary file '$tempFile'"
return $tempFile
}
function FilesAreIdentical
{
param([string]$winmerge, $phoneFile, $cloudFile)
$arguments = "-enableexitcode -noninteractive -minimize `"$phoneFile`" `"$cloudFile`""
$process = Start-Process $winmerge -windowstyle Hidden -ArgumentList $arguments -PassThru -Wait
$comparisonResult = $process.ExitCode
enum Result {
Identical = 0
Different
Error
}
$enumResult = [Enum]::ToObject([Result], $comparisonResult)
$result = switch ($enumResult) {
Identical { $true }
Different { $false }
Error {
throw "Failed to compare '$phoneFile' and '$cloudFile'"
}
}
return $result
}
function IsInCloud
{
param($phoneFile, $cloudFiles, $tempDir)
$originalFilename = $phoneFile.name
$cloudFile = $cloudFiles[$originalFilename]
if ($cloudFile -eq $null) {
$megaFilename = GetMegaFilename($phoneFile)
Write-Debug "Mega filename guess for '$originalFilename': '$megaFilename'"
$cloudFile = $cloudFiles[$megaFilename]
Write-Debug "Mega filename match: $($cloudFile -ne $null)"
}
if ($cloudFile -eq $null) {
$result = $false
} else {
$extension = [System.IO.Path]::GetExtension($originalFilename)
Write-Debug "'$originalFilename' extension: '$extension'"
if ($extension -eq ".mp4") {
# These files have the exact same size on the phone and on the PC too.
$phoneFileSize = $phoneFile.ExtendedProperty("System.Size")
$cloudFileSize = $cloudFile.Length
Write-Debug "$($phoneFile.name) $phoneFileSize == $($cloudFile.name) $cloudFileSize"
if ($phoneFileSize -eq $cloudFileSize) {
$result = $true
} else {
$result = $false
}
} else {
# These files can have different size or modified date on the phone and on the PC.
# So we do a proper comparison using winmerge.
$tempFile = CreateTempFile $phoneFile $tempDir
$result = FilesAreIdentical $winmerge $tempFile $cloudFile
}
}
return $result
}
function CacheCloudFiles
{
param([string]$cloudFolderPath, [string]$filter)
$cloudFiles = @{}
foreach ($file in Get-ChildItem -Path $cloudFolderPath -Recurse | where { $_.Name -match $filter }) {
$cloudFiles.add($file.Name, $file)
}
return $cloudFiles
}
function GetConfig
{
param([string]$configFile)
if (![System.IO.File]::Exists($configFile)) {
$configFile = [IO.Path]::Combine($PSScriptRoot, $configFile)
}
. $configFile
return $config
}
#==============================================================================#
# MAIN
#==============================================================================#
EnsurePrerequisites $choco $winmerge
$config = GetConfig($configFile)
if ($cloudFolderPath -eq "<configured>") {
$cloudFolderPath = $config.settings.cloudFolderPath
}
if ($destinationFolderPath -eq "<configured>") {
$destinationFolderPath = $config.settings.destinationFolderPath
}
$phoneInfo = $config.phones[$phoneName]
if ($phoneInfo -ne $null) {
$phoneName = $phoneInfo.name
}
if ($phoneFolderPath -eq "<configured>") {
if ($phoneInfo -eq $null) {
throw "Failed to find $phoneName in $configFile"
}
$phoneFolderPath = $phoneInfo.folder
}
$phone = Get-Phone -phoneName $phoneName
if ($phone -eq $null) {
throw "Can't find '$phoneName'. Have you attached the phone? Is it in 'File transfer' mode?"
}
if (!$(Test-Path -Path $cloudFolderPath -PathType Container)) {
throw "Can't find the folder '$cloudFolderPath'."
}
if (!$(Test-Path -Path $destinationFolderPath -PathType Container)) {
throw "Can't find the folder '$destinationFolderPath'."
}
# TODO add multiprocessing Write-Output "Running on $jobCount threads..."
$phoneFolder = Get-SubFolder $phone $phoneFolderPath
Write-Output "Looking for files under '$phoneFolderPath' that match '$filter'..."
$phoneFiles = @( $phoneFolder.GetFolder.items() | where { $_.Name -match $filter } )
$phoneFileCount = $phoneFiles.Count
Write-Output "Found $phoneFileCount file(s) on the phone"
$phonePath = "$phoneName\$phoneFolderPath"
Write-Output "Processing path: $phonePath"
Write-Output "Looking for files in: $cloudFolderPath"
if ($phoneFileCount -gt 0) {
$action = if ($dryRun) {"NOT copy (dry-run)"} else {"copy"}
Write-Output "Will $action missing files to: $destinationFolderPath"
$shell = Get-ShellProxy
$destinationFolder = $shell.Namespace($destinationFolderPath).self
Write-Output "Looking for files under '$cloudFolderPath' that match '$filter'..."
$cloudFiles = CacheCloudFiles $cloudFolderPath $filter
Write-Output "Found $($cloudFiles.Count) file(s) in the cloud"
ClearTempDirectory $tempDir $filter # this helps re-runs
$processedCount = 0;
$copied = 0;
foreach ($phoneFile in $phoneFiles) {
$fileName = $phoneFile.Name
++$processedCount
$percent = [int](($processedCount * 100) / $phoneFileCount)
Write-Progress -Activity "Processing Files in $phonePath" `
-Status "Processing File ${count} / ${totalItems} (${percent}%)" `
-CurrentOperation $fileName `
-PercentComplete $percent
if (!(IsInCloud $phoneFile $cloudFiles $tempDir)) {
if ($dryRun) {
Write-Output "Would try to copy $fileName to $destinationFolderPath"
++$copied
} else {
$confirmed = $true
if ($confirmCopy) {
$confirmation = Read-Host "$fileName seems to be missing from $cloudFolderPath"`
" or any of its sub-folders."`
" Shall we copy it to $destinationFolderPath? (y/n)"
if ($confirmation -ne 'y') {
$confirmed = $false
}
}
if ($confirmed) {
# TODO re-use the temporary files, if they exist, with GetTempFileName
Write-Output "Copying $fileName to $destinationFolderPath..."
$destinationFolder.GetFolder.CopyHere($phoneFile)
++$copied
}
}
}
}
if ($copied -eq 0) {
Write-Output "All $phoneFileCount file(s) seem to be already synced. 🎉🎉🎉"
} else {
$action = if ($dryRun) {"would have been"} else {"were"}
Write-Output "$copied/$phoneFileCount item(s) $action copied to $destinationFolderPath"
}
} else {
Write-Output "Found no files under '$phonePath' matching the '$filter' filter."
}