Skip to content

Instantly share code, notes, and snippets.

@devster
Last active February 15, 2024 17:15
Show Gist options
  • Select an option

  • Save devster/e6a591879fd9c68b86c9 to your computer and use it in GitHub Desktop.

Select an option

Save devster/e6a591879fd9c68b86c9 to your computer and use it in GitHub Desktop.

Revisions

  1. devster revised this gist Aug 4, 2015. No changes.
  2. devster revised this gist Aug 4, 2015. No changes.
  3. devster created this gist Aug 4, 2015.
    34 changes: 34 additions & 0 deletions file.io.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/bin/sh

    URL="https://file.io"
    DEFAULT_EXPIRE="14d" # Default to 14 days

    if [ $# -eq 0 ]; then
    echo "Usage: file.io FILE [EXPIRE]"
    echo " Example: file.io path/to/my/file 1w"
    echo " This example upload your file for 1 download and expires until 7 days if not downloaded."
    echo "\nSee documentation at https://www.file.io/#one"
    exit 1
    fi

    FILE=$1
    EXPIRE=${2:-$DEFAULT_EXPIRE}

    if [ ! -f "$FILE" ]; then
    echo "File ${FILE} not found"
    exit 1
    fi

    RESPONSE=$(curl -F "file=@${FILE}" "${URL}/?expires=${EXPIRE}")

    RETURN=$(echo "${RESPONSE}" | grep -Po '(?<=success).[^",}]*' | cut -d':' -f2 | tr -d '[[:space:]]')

    if [ "true" != "$RETURN" ]; then
    echo "An error occured!\nThere is the file.io response: ${RESPONSE}"
    exit 1
    fi

    KEY=$(echo "${RESPONSE}" | grep -Po '(?<=key)[":\s]+.*?"' | cut -d':' -f2 | tr -d '[[:space:]]' | tr -d '"')
    EXPIRY=$(echo "${RESPONSE}" | grep -Po '(?<=expiry)[":\s]+.*?"' | cut -d':' -f2 | tr -d '[[:space:]]' | tr -d '"')

    echo "Upload done!\nYou can share the download link (expires in ${EXPIRY}): ${URL}/${KEY}"