aboutsummarybugs & patchesrefslogtreecommitdiffstats

Image Bridge

Why

In a recent (at the time of writing) discussion in the Pine64 community, I noticed someone noting discord urls when bridged to IRC and Telegram aren't valid for very long (the bridge forwarded discord's attachment URL plainly, and discord attachment URLs expire after some point - needed against abuse but not usefull for bridging). This point was brought to the table because someone else noted about the URLs being too long (also fair, who wants a URL taking up 2-3 lines for every attachment ever sent).

Given I've recently been working with cherrypy and the discord API a lot, and has files around for connecting to the discord gateway from a cherrypy plugin, I thought I'd write this 'quick' URL shortener and discord attachment 'bridge' (it's more like a proxy to discord's api I guess).

Todo

  • [ ] also bridge telegram attachments

How

This program works by listening on a chat application gateway (currently only discord), and downloading every attachment that comes by. It also stores a reference on how to redownload that image. Your bridge can then send a GET request to an endpoint of this webserver, providing a unique thing identifying the attachment. This webserver then sends a URL back for the bridge to send instead of the original attachment URL. This new URL will stay valid as long as this bridge knows how to download the image.

Usage

Installing should be done using pip install ..

For using this, one sets a few env variables and runs the thing (discord_image_bridge).

List of env vars:

DISCORD_TOKEN=secrettoken # this is the token for your discord bot

BRIDGE_CACHE_DIR=~/.local/share/discord_image_bridge/ # where this should store files. 
BRIDGE_CACHE_TIME=604800 # how much seconds to store an image for. defaults to 7 days.

CRON_TOKEN=generate_a_random_sequence # the 'password' for the cron job

Note if you want downloaded attachments to be deleted after some time, you should set up a cron job that sends a GET request to /run_cron. This API endpoint requires one query parameter: one should set token to the value of your CRON_TOKEN env variable.

Example:

CRON_TOKEN=d2VsbCBkaXNjb3ZlcmVkISB0aGlzIGlzIGluZGVlZCBiYXNlNjQhCg==

curl -X GET 'https://localhost:8080/run_cron?token=d2VsbCBkaXNjb3ZlcmVkISB0aGlzIGlzIGluZGVlZCBiYXNlNjQhCg=='
#                    ^ replace with your domain    ^ this is the value of CRON_TOKEN