60 lines
1.5 KiB
Bash
Executable File
60 lines
1.5 KiB
Bash
Executable File
#!/bin/sh
|
|
export HOME="/var/gemini/yoinkdata"
|
|
gmni_path="/usr/local/bin/gmni"
|
|
|
|
size_limit="2097152000000" # 2MiB
|
|
timeout="10"
|
|
|
|
target="$QUERY_STRING"
|
|
target_url="gemini://$QUERY_STRING"
|
|
target_file_ext="$(echo "$target" | rev | cut -f 1 -d '.' | rev )"
|
|
|
|
deposit_dir="$HOME/files"
|
|
deposit_dir_url="gemini://illegaldrugs.net/yoinkdata/files"
|
|
|
|
deposit_file_name="$(head -c24 /dev/urandom | base64 | tr -d '=' | sed 'y/\+\//-_/').$target_file_ext"
|
|
deposit_file_path="$deposit_dir/$deposit_file_name"
|
|
|
|
while [ -f "$deposit_file_path" ]
|
|
do
|
|
deposit_file_name="$(head -c24 /dev/urandom | base64 | tr -d '=' | sed 'y/\+\//-_/').$target_file_ext"
|
|
deposit_file_path="$deposit_dir/$deposit_file_name"
|
|
done
|
|
|
|
if echo "$target_file_ext" | grep -qvx "[a-z]*"
|
|
then
|
|
printf "20 text/plain\r\n"
|
|
printf "the file at $target_url does not have a proper extension.\r\n"
|
|
exit
|
|
fi
|
|
|
|
if [ -z "$QUERY_STRING" ]
|
|
then
|
|
printf "20 text/plain\r\n"
|
|
printf "please provide a location to yoink from.\r\n"
|
|
exit
|
|
fi
|
|
|
|
if ! (timeout 2 $gmni_path -INLj always "$target_url" >/dev/null)
|
|
then
|
|
printf "20 text/plain\r\n"
|
|
printf "this does not look yoinkable.\r\n"
|
|
exit
|
|
fi
|
|
|
|
mkdir -p "$deposit_dir"
|
|
|
|
timeout $timeout $gmni_path -NLj always "$target_url" | head -c $size_limit > "$deposit_file_path"
|
|
|
|
if [ "$(du "$deposit_file_path" | cut -f 1)" = "0" ]
|
|
then
|
|
printf "20 text/plain\r\n"
|
|
printf "an error occured yoinking your file.\r\n"
|
|
rm "$deposit_file_path"
|
|
exit
|
|
fi
|
|
|
|
printf "20 text/gemini\r\n"
|
|
printf "yoinking complete.\r\n"
|
|
printf "=> $deposit_dir_url/$deposit_file_name <= your file"
|