diff options
| author | vin <git@vineetk.net> | 2024-10-22 14:23:15 -0400 |
|---|---|---|
| committer | vin <git@vineetk.net> | 2024-10-22 14:23:15 -0400 |
| commit | 13c231ee0d0ff477969e14b47332ec94329bf80c (patch) | |
| tree | 2a7bc3916f95e954dd5223fe0ffdf13b7bea155e /content/blog/tmpfilehost.md | |
initial commit with converted pages
Diffstat (limited to 'content/blog/tmpfilehost.md')
| -rw-r--r-- | content/blog/tmpfilehost.md | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/content/blog/tmpfilehost.md b/content/blog/tmpfilehost.md new file mode 100644 index 0000000..c8356d8 --- /dev/null +++ b/content/blog/tmpfilehost.md | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | +++ | ||
| 2 | title = "Creating a Temporary File Hoster" | ||
| 3 | date = 2022-04-27 | ||
| 4 | draft = false | ||
| 5 | +++ | ||
| 6 | For the past couple years, whenever I wanted to upload a file, I would | ||
| 7 | curl the file to [lainsafe](https://git.qorg11.net/lainsafe.git/), [i/u.kalli.st](https://gt.kalli.st/kallist/uploader), and recently [ttm.sh](https://tildegit.org/tildeverse/ttm.sh). | ||
| 8 | |||
| 9 | Since I want to selfhost, I thought i can just use either of what those | ||
| 10 | three used. Earlier today though, I realized I could just copy the | ||
| 11 | file(s) I want to upload via rsync/scp to a public directory that gets | ||
| 12 | served by an httpd or gopherd. | ||
| 13 | |||
| 14 | From what I understand, the previous file hosters had a program running | ||
| 15 | that read the file that the user uploads to them, does some renaming, | ||
| 16 | and writes that to a directory that is served. After some time, that | ||
| 17 | file is deleted. The first part can be handled via rsync/scp like | ||
| 18 | mentioned previously. For automatic deletion, I recently saw in find's | ||
| 19 | man page that it can list that haven't been modified via the -mtime | ||
| 20 | flag, so that can be used with a cron job. | ||
| 21 | |||
| 22 | But while thinking of this idea, I got stumped by how to print back the | ||
| 23 | url to this file that is uploaded since printing the filename as is | ||
| 24 | appended to its baseurl, there could be spaces and other invalid | ||
| 25 | unescaped characters which programs trying to download it may not like. | ||
| 26 | |||
| 27 | I thought I could just create a separate program for this. However, | ||
| 28 | doing this seemed more complicated than just copying the file to the | ||
| 29 | server. So, with the help of awk and some StackExchanging, I've been | ||
| 30 | able to do it. | ||
| 31 | |||
| 32 | `upfile.sh`: | ||
| 33 | ```sh | ||
| 34 | #!/bin/sh | ||
| 35 | urlencode() { | ||
| 36 | awk ' | ||
| 37 | BEGIN { for (i = 1; i < 256; i++) hex[sprintf("%c", i)] = sprintf("%%%02X", i) } | ||
| 38 | { | ||
| 39 | for (i = 1; i <= length($0); i++) { | ||
| 40 | c = substr($0, i, 1) | ||
| 41 | printf("%s", c ~ /^[-._~0-9a-zA-Z]$/ ? c : hex[c]) | ||
| 42 | } | ||
| 43 | printf "\n" | ||
| 44 | } | ||
| 45 | ' | ||
| 46 | } | ||
| 47 | |||
| 48 | FILE="$1" | ||
| 49 | SERVER="REPLACEME" | ||
| 50 | BASEURL="https://u.$SERVER" | ||
| 51 | |||
| 52 | [ -z "$1" ] && exit 1 | ||
| 53 | |||
| 54 | scp "$FILE" "$SERVER":files/ || exit 1 | ||
| 55 | printf "%s/" "$BASEURL" | ||
| 56 | basename "$FILE" | urlencode | ||
| 57 | ``` | ||
| 58 | |||
| 59 | Then to purge these files after they become too old (e.g. 3 days), you | ||
| 60 | can put something like this in a cron job to run daily (replace file | ||
| 61 | directory): | ||
| 62 | |||
| 63 | ``` | ||
| 64 | 0 0 * * * find /path/to/dir/ -mtime +3 -exec rm {} \; | ||
| 65 | ``` | ||
| 66 | |||
| 67 | You can also put this command in /etc/daily.local or /etc/cron/daily, | ||
| 68 | or whatever file your root crontab's @daily runs (if there is one). | ||
| 69 | |||
| 70 | And that's it! The only difficult part that I experienced was encoding | ||
| 71 | the name of the file and originally did that in C. However, having a | ||
| 72 | mixed C and shell program just for file uploading didn't sit right with | ||
| 73 | me. It seems like whenever you're in doubt, you can rely on awk huh. | ||
