Bepasty image uploader
For many years I used bepasty-client-cli to upload my screenshots via script to my paste-bin: bepasty-server. Somehow broke the bepasty-cli a few days ago and I don't want to invest time in an unmainted cli client.
First I tried to solve the upload by only using curl
and base64
.
But actually figuring out what to set for Content-Length
and Content-Range
with only getting 400
errors from bepasty-server was so annoying, that I canceled my solution attempt.
My working solution (bepasty-upload-image.py
) borrowed as little code as possible from bepasty-cli:
import base64 import sys import click import requests @click.command() @click.option("-u", "--url") @click.option("-p", "--password") @click.option("-f", "--filename") def main(url, password, filename): with open(filename, "rb") as fp: raw_data = fp.read() payload = base64.b64encode(raw_data) raw_data_size = len(raw_data) filesize = raw_data_size headers = { "Content-Range": "bytes %d-%d/%d" % (0, 0 + raw_data_size - 1, filesize), "Content-Type": "image/png", "Content-Filename": "screen.png", "Content-Length": str(len(payload)), "Maxlife-Unit": "FOREVER", "Maxlife-Value": "1", } r = requests.post( "{}/apis/rest/items".format(url), data=payload, headers=headers, auth=("user", password), ) if r.status_code == 201: loc = r.headers["Content-Location"].split("/")[-1] click.echo(f"{url}/{loc}") else: click.echo(r.text) sys.exit(1) if __name__ == "__main__": main()
This code doesn't support chunked uploads. For image files this should never be a problem to always upload the whole file.
Printing the url with the filename is important, because in the script this is called the url is automaticaly opened via
xdg-open
.My takescreenshot.sh
looks like this:
#!/bin/bash old=$(xsel -o -b) sleep 0.1 f=$(scrot -s -e 'echo $f') result=$(python ~/bin/bepasty-upload-image.py -p PASSWORD -u https://some-paste-bin-url.example -f $f) if [ "$?" -ne "0" ]; then echo $result; exit 1 fi echo $result | head -n 1 | xsel -i -b url=$(xsel -o -b) if [ "$old" != "$url" ]; then xdg-open $url fi rm $f