Skip to content

Instantly share code, notes, and snippets.

@tahnok
Created January 25, 2024 01:46
Show Gist options
  • Save tahnok/4461fce90656d755d3440618984c3dff to your computer and use it in GitHub Desktop.
Save tahnok/4461fce90656d755d3440618984c3dff to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""
A lil script to put files on https://cdn.tahnok.ca for use in blog posts or sharing or whatever
Accepts either a path or a URL.
If the filename is longer than 40 characters, make a random UUID based on the filename
Depends on wget and scp
"""
import argparse
import re
import subprocess
from pathlib import Path
from uuid import uuid5, UUID
namespace = UUID("231c540e-866e-48d6-a3a4-38cf20a87893")
def main(url_or_file):
if url_or_file.startswith("http"):
result = subprocess.run(["wget", url_or_file, "-P", "/tmp/cdn"], capture_output=True, check=True, text=True)
url_or_file = re.search(r"Saving to: ‘(.*)’", result.stderr)[1]
# should I clean up?
path = Path(url_or_file)
if not path.exists():
raise
if len(path.stem) > 40:
output_name = f"{uuid5(namespace, url_or_file)}{path.suffix}"
else:
output_name = path.name
subprocess.run(["scp", url_or_file, f"debian@oolong.tahnok.ca:/data/cdn/u/{output_name}"], check=True)
print(f"https://cdn.tahnok.ca/u/{output_name}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("url_or_file")
args = parser.parse_args()
main(args.url_or_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment