PowerShell/scripts/publish-to-ipfs.ps1

60 lines
1.9 KiB
PowerShell
Raw Normal View History

2024-10-01 13:37:53 +02:00
<#
2021-07-16 16:40:06 +02:00
.SYNOPSIS
2021-10-16 16:50:10 +02:00
Publishes files & folders to IPFS
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2021-10-16 16:50:10 +02:00
This script publishes the given files and folders to IPFS.
.PARAMETER FilePattern
Specifies the file pattern
.PARAMETER HashList
Specifies the path to the resulting hash list
.PARAMETER DF_Hashes
Specifies the path to the resulting digital forensic hashes
2021-07-16 16:40:06 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./publish-to-ipfs C:\MyFile.txt
2021-07-16 16:40:06 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-09-06 21:42:04 +02:00
.NOTES
Author: Markus Fleschutz | License: CC0
2021-07-16 16:40:06 +02:00
#>
2021-09-19 12:49:19 +02:00
param([string]$FilePattern = "", [string]$HashList = "IPFS_hashes.txt", [string]$DF_Hashes = "file_checksums.xml")
2021-07-16 16:40:06 +02:00
try {
2021-09-19 12:49:19 +02:00
if ($FilePattern -eq "") { $FilePattern = read-host "Enter file(s)/directories to publish" }
2021-07-16 16:40:06 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-08-11 07:34:54 +02:00
Write-Host "⏳ (1/3) Searching for IPFS executable..." -NoNewline
2021-08-11 07:34:54 +02:00
& ipfs --version
if ($lastExitCode -ne "0") { throw "Can't execute 'ipfs' - make sure IPFS is installed and available" }
2021-09-19 12:49:19 +02:00
if (test-path "$FilePattern" -pathType container) {
"⏳ (2/3) Publishing folder $FilePattern/..."
2021-09-19 12:49:19 +02:00
& ipfs add -r "$FilePattern" > $HashList
2021-09-11 11:17:17 +02:00
[int]$Count = 1
2021-09-01 09:23:25 +02:00
""
"⏳ (3/3) Calculating digital forensics hashes to $DF_HASHES ..."
2021-09-19 12:49:19 +02:00
& nice hashdeep -c md5,sha1,sha256 -r -d -l -j 1 "$FilePattern" > $DF_Hashes
2021-08-01 11:57:54 +02:00
} else {
2021-09-19 12:49:19 +02:00
$FileList = (get-childItem "$FilePattern")
2021-08-01 11:57:54 +02:00
foreach ($File in $FileList) {
2021-09-19 12:49:19 +02:00
if (test-path "$FilePattern" -pathType container) {
"⏳ (2/3) Publishing folder $File/..."
2021-08-01 11:57:54 +02:00
& ipfs add -r "$File" >> $HashList
} else {
"⏳ (3/3) Publishing file $File..."
2021-08-01 11:57:54 +02:00
& ipfs add "$File" >> $HashList
}
2021-07-16 16:40:06 +02:00
}
2021-09-11 11:17:17 +02:00
[int]$Count = $FileList.Count
2021-07-16 16:40:06 +02:00
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2024-10-01 13:37:53 +02:00
"✅ published $Count file(s)/folder(s) to IPFS in $Elapsed sec"
2021-07-16 16:40:06 +02:00
" NOTE: to publish it to IPNS execute: ipfs name publish <HASH>"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-07-16 16:40:06 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-07-16 16:40:06 +02:00
exit 1
}