Browse Source

Added clipboard support

main
Victor Roest 9 months ago
parent
commit
7c6eb11aed
Signed by: 0x76 GPG Key ID: A3923C699D1A3BDA
  1. 3
      .vscode/settings.json
  2. 74
      ipfs-share.py

3
.vscode/settings.json

@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/bin/python"
}

74
ipfs-share.py

@ -2,23 +2,38 @@
import subprocess
import os
import argparse
from shutil import which
from typing import List
gateways = ["https://ipfs.xirion.net", "https://cloudflare-ipfs.com"]
try:
from tkinter import Tk
except:
CLIPBOARD_SUPPORT = False
else:
CLIPBOARD_SUPPORT = True
# TODO: Custom gateway
GATEWAYS = ["https://cloudflare-ipfs.com", "https://ipfs.xirion.net"]
# Checker for argparse to verify that the path exists
def path(path: str) -> str:
if os.path.isdir(path) or os.path.isfile(path):
return path
else:
raise argparse.ArgumentTypeError(f"{path} is not a valid path to a file or dir")
raise argparse.ArgumentTypeError(
f"{path} is not a valid path to a file or dir")
# Upload file to ipfs
def upload_file(path: str) -> str:
def upload_file(path: str, ipfs: str) -> str:
# ipfs subcommand
# -w: Wrap, wraps the file in a folder, allows linking to the filename
# -q: Quiet, gives better parsable output
result = subprocess.run(["ipfs", "add", "-wq", path], capture_output=True, text=True)
result = subprocess.run([ipfs, "add", "-wq", path],
capture_output=True, text=True)
if result.returncode != 0:
raise ValueError(f"ipfs command failed:\n\n{result.stderr}")
@ -27,43 +42,60 @@ def upload_file(path: str) -> str:
return f"{folder_hash}/{file_name}"
# Upload folder to ipfs
def upload_folder(path: str) -> str:
def upload_folder(path: str, ipfs: str) -> str:
# ipfs subcommand
# -r: Recursive, required for folders
# -q: Quiet, gives better parsable output
result = subprocess.run(["ipfs", "add", "-rq", path], capture_output=True, text=True)
result = subprocess.run([ipfs, "add", "-rq", path],
capture_output=True, text=True)
if result.returncode != 0:
raise ValueError(f"ipfs command failed:\n\n{result.stderr}")
folder_hash = result.stdout.splitlines()[-1]
return folder_hash
# Upload folder or file to ipfs
def upload(path: str) -> str:
def upload(path: str, ipfs: str = "ipfs") -> str:
if os.path.isdir(path):
return upload_folder(path)
return upload_folder(path, ipfs)
elif os.path.isfile(path):
return upload_file(path)
return upload_file(path, ipfs)
else:
raise ValueError(f"{path} is an invalid path to file or dir")
def format_urls(cid: str, gateways: List[str]) -> List[str]:
return [f"{g}/ipfs/{cid}" for g in gateways]
def main(path: str):
cid = upload(path)
urls = format_urls(cid, gateways)
print("Uploaded file succesfully, urls:")
# TODO: Copy to clipboard
def copy_to_clipboard(string: str):
if CLIPBOARD_SUPPORT:
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(string)
r.update()
r.destroy()
def main(path: str, clipboard: bool):
if (ipfs := which("ipfs")) is None:
raise ValueError("ipfs binary could not be found")
# TODO: Remote pinning
cid = upload(path, ipfs)
urls = [f"{g}/ipfs/{cid}" for g in GATEWAYS]
if clipboard:
copy_to_clipboard(urls[-1])
for url in urls:
print(url)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Share a file using IPFS')
parser.add_argument('path', type=path, help='The file or folder to share')
# TODO: Custom gateway
# TODO: Remote pinning
args = parser.parse_args()
parser.add_argument('path', type=path, help='the file or folder to share')
parser.add_argument('-nc', '--no-clipboard',
action="store_true", help='disable clipboard support')
main(args.path)
args = parser.parse_args()
main(args.path, not args.no_clipboard)

Loading…
Cancel
Save