#!/bin/bash

function upload_to_streamhide () {
    api_key=$1
    filepath=$2

    filepath=$( readlink -f "$filepath" )
    # echo "filepath: $filepath" >&2
    filesize=$(stat -c%s "$filepath")
    # echo "filesize: $filesize" >&2


    upload_server=$(curl -s -X GET "https://api.streamhide.com/api/upload/server?key=$api_key" -H "Content-Type: application/json")
    echo "Upload_URL $upload_server" >&2
    upload_server=$(echo $upload_server | jq ".result")
    upload_server=${upload_server//\"}
    echo "Location: $upload_server"  >&2

    hoster_url=$(curl -X POST -F key=$api_key -F "file=@\"$filepath\"" $upload_server)

    echo "result: $hoster_url"  >&2

    hoster_url=$(echo $hoster_url | jq ".files")
    # hoster_filename=$(echo $hoster_url | jq ".name")
    hoster_url=$(echo $hoster_url | jq ".[0]")
    hoster_url=$(echo $hoster_url | jq ".filecode")
    hoster_url=${hoster_url//\"}

    if [ "$hoster_url" != "" ] && [ "$hoster_url" != "null" ]; then
        echo "https://streamhide.to/w/$hoster_url"
    else
        echo ""
    fi
}

function streamhide_check_if_file_is_alive () {
    file_url=$2
    api_key=$1

    file_id=${file_url##*/}
    >&2 echo "File ID: $file_id"

    response=$(curl -s -X GET "https://api.streamhide.com/api/file/info?key=$api_key&file_code=$file_id" -H "Content-Type: multipart/form-data")
    >&2 echo "Response: $response"

    is_alive=$(echo $response | jq ".result")
    is_alive=$(echo $is_alive | jq ".[0]")
    is_alive=$(echo $is_alive | jq ".status")

    if [ "$is_alive" == "\"Not found\"" ]; then
        echo "false"
    elif [ $is_alive == "200" ]; then
        echo "true"
    else
        echo "false"
    fi
}
