Molytov's Cocktail Lounge

How I Share Files Without MediaFire and Imgur

You ever click an Imgur link or want to download a file from MediaFire only to see that it's deleted? Yeah, it sucks. It's the result of having to trust massively centralized services to keep your files up. They constantly remove content that they deem is not worth hosting anymore, and it's no fun being subject to their arbitrary whims.

I wanted to reduce my reliance on third parties to share files. I wanted my own way to easily store and share files while being in control of when they are deleted, and I accomplished this with linx-server. The instructions were very easy to follow, and only took about 10-15 minutes to configure to my liking. It has an API that allows for uploading from the command line via a PUT request.

Making my instance private

Now, I don't want any random person accessing my upload server to be able to upload stuff. That imposes quite a lot of liability on me if someone uploads something illegal or malicious on my server, so I set up basic HTTP authentication. This requires me to authenticate in order to upload files. I made a password file with:
sudo htpasswd -c /path/to/.htpasswd username

Then, I set these location lines to my nginx config to restrict access to the service:

	location = / {
		auth_basic "Upload Server" ;
		auth_basic_user_file /path/to/.htpasswd ;
		proxy_pass http://localhost:port ;
	}

	location /upload {
                auth_basic "Upload Server" ;
                auth_basic_user_file /path/to/.htpasswd ;
                proxy_pass http://localhost:port ;
	}

	location /API {
		auth_basic "Upload Server" ;
                auth_basic_user_file /path/to/.htpasswd ;
                proxy_pass http://localhost:port ;
	}

        location /paste {
                auth_basic "Upload Server" ;
                auth_basic_user_file /path/to/.htpasswd ;
                proxy_pass http://localhost:port ;
	}

	location / {
		proxy_pass http://localhost:port ;
	}

This keeps people from being able to upload to access the upload, API and pastebin pages, but still allows then to view files that are uploaded to the server.

For uploading and deleting files from the terminal, I wrote what is quite possibly the worst shell script in existence:

#!/bin/sh
read -rd '' HELP <<'EOF'
upload and delete files for my linx server
upload - $ linx u <file> <delete-key>
delete - $ linx d <url> <delete-key>
Log of active files uploaded is in
EOF
LOGFILE=$HOME/Documents/FileUploads.txt
FILELINE="$2 : $3"
if [ $# -eq 0 ];
        then
                echo -e "$HELP $LOGFILE"
                exit 1

elif [ "$1" = "u" ] || [ "$1" = "d" ];
        then
        if [ "$1" = "u" ];
                then
                        curl -su username:password --basic -H "Linx-Delete-Key: $3" -T "$2" https://linx.upload.server/upload/ | tee /dev/tty | tr -d '\n' | cat - <(echo " : $3") >> $LOGFILE
                        echo -e "Your Delete Key:\n$3"
                        echo ""
        elif [ "$1" = "d" ];
                then
                        curl -su username:password --basic -H "Linx-Delete-Key: $3" -X DELETE "$2"
                        sed -i "/\<${FILELINE//\//\\/}\>/d" $LOGFILE
        fi

elif [ "$1" != "u" ] || [ "$1" != "d" ];
        then
                echo -e "Improper argument.\n upload - $ linx u <file> <delete-key>\n delete - $ linx d <url> <delete-key>"
                exit 1
fi

Linx has a feature that allows an upload to also have a delete key to delete the file remotely, which can be specified in the script after the path to the file.

After that, the link to the file and the delete password gets outputted to the terminal and to the FileUploads.txt file in the Documents folder. Specifying the URL to the file to delete and the delete key will remove it from the server and the FileUploads.txt file.

Example

Uploading a file.
Upload an image via the script

Look! It's a kitty!
Kitty cat!

The file is now deleted. Goodbye kitty :(
Delete the image via the script

It's a horrible script

It's got everything that you would want to avoid like hard-coded credentials, no validating inputs and excessive piping, but I felt like learning how to write a shell script and I wanted something that was simple and self-contained, without storing credentials in some other file. There is linx-client which is probably better to use.

#foss #selfhosting