Skip to content

Instantly share code, notes, and snippets.

@sg-s
Last active August 29, 2015 14:05
Show Gist options
  • Save sg-s/40245c08d37e83bde3fb to your computer and use it in GitHub Desktop.
Save sg-s/40245c08d37e83bde3fb to your computer and use it in GitHub Desktop.
How to make your own file sharing service

How to make your own file sharing service

What you want to do

Create a small utility that takes a folder you want to share, and creates a not-obvious URL that you can send to other humans.

What you need:

  • A server you can SSH into
  • a *nix machine

Step 1: Make a not-obvious URL

Because we are going to make your URL public, let's make it not-obvious so that people can't guess other URLs and steal files:

# make a random string
index=0
str=""
for i in {a..z}; do arr[index]=$i; index=`expr ${index} + 1`; done
for i in {A..Z}; do arr[index]=$i; index=`expr ${index} + 1`; done
for i in {0..9}; do arr[index]=$i; index=`expr ${index} + 1`; done
for i in {1..6}; do str="$str${arr[$RANDOM%$index]}"; done

Step 2: Compress the chosen folder or file and name it with this hard-to-guess name

# compress chosen folder
folder_name=$1
zip_name=$str".zip"
echo "Compressing into" $zip_name
zip -r -X -q $zip_name $1 # -X flag removes junk that Mac OS X adds

Step 3: Copy the .zip file to your server

# get the login name from some file (assumes you have public-key SSH set up)
login_id=$(<~/.server_id)

# copy to server
scp $zip_name $login_id:/path/to/html/folder/

Step 4: Copy the link to clipboard

# copy public link to clipboard 
echo $login_id/$zip_name | pbcopy
echo "Link copied to clipboard"

# delete the .zip file we made
rm $zip_name

The complete solution

Download the whole bash script here:

https://github.com/sg-s/auto-bots/blob/master/share.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment