PowerShell/Scripts/publish-to-ipfs.ps1

53 lines
1.5 KiB
PowerShell
Raw Normal View History

2021-07-16 16:40:06 +02:00
<#
.SYNOPSIS
publish-to-ipfs.ps1 [<file(s)/dir>] [<to-hash-list>]
.DESCRIPTION
2021-08-01 11:57:54 +02:00
Publishes the given files and folders to IPFS
2021-07-16 16:40:06 +02:00
.EXAMPLE
PS> .\publish-to-ipfs.ps1 C:\MyFile.txt
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-03 15:53:57 +02:00
Author: Markus Fleschutz
License: CC0
2021-07-16 16:40:06 +02:00
#>
2021-08-02 07:47:31 +02:00
param([string]$Files = "", [string]$HashList = "IPFS_hashes.txt", [string]$DF_Hashes = "file_checksums.xml")
2021-07-16 16:40:06 +02:00
try {
& ipfs --version
if ($lastExitCode -ne "0") { throw "Can't execute 'ipfs' - make sure IPFS is installed and available" }
if ($Files -eq "") { $Files = read-host "Enter file(s) or directory tree to publish" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2021-08-01 11:57:54 +02:00
if (test-path "$Files" -pathType container) {
"Adding folder $Files to IPFS..."
[int]$Count = 1
& ipfs add -r "$Files" > $HashList
2021-07-16 16:40:06 +02:00
2021-08-01 11:57:54 +02:00
echo "Calculating digital forensics hashes to $DF_HASHES ..."
& nice hashdeep -c md5,sha1,sha256 -r -d -l -j 1 "$Files" > $DF_Hashes
} else {
$FileList = (get-childItem "$Files")
[int]$Count = $FileList.Count
foreach ($File in $FileList) {
if (test-path "$Files" -pathType container) {
"Adding folder $File to IPFS..."
& ipfs add -r "$File" >> $HashList
} else {
"Adding file $File to IPFS..."
& ipfs add "$File" >> $HashList
}
2021-07-16 16:40:06 +02:00
}
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2021-08-01 11:57:54 +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>"
exit 0
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}