#!/bin/bash

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

    filepath=$( readlink -f "$filepath" )
    # echo "filepath: $filepath" >&2

    upload_server=$(curl -s "https://voe.sx/api/upload/server?key=$api_key")
    upload_server=$(echo $upload_server | jq ".result")
    upload_server=${upload_server//\"}
    echo "Upload Target: $upload_server"  >&2

    voe_url=$(curl -X POST $upload_server -H "Content-Type: multipart/form-data" -H 'Cache-Control: no-cache' -F action=push -F key=$api_key -F "file=@\"$filepath\"")

    echo "$voe_url"   >&2

    voe_url=$(echo $voe_url | jq ".file")
    voe_url=$(echo $voe_url | jq ".file_code")
    voe_url=${voe_url//\"}
    echo "voe_url: $voe_url"  >&2

    echo "https://voe.sx/$voe_url"
}


function voe_check_if_file_is_alive () {
    api_key=$1
    file_url=$2
    
    file_id=${file_url##*/}
    # >&2 echo "File ID: $file_id"
    
    response=$( curl -s "https://voe.sx/api/file/info?key=$api_key&file_code=$file_id" -H "Content-Type: application/json")
    >&2 echo "Response: $response"
    
    is_alive=$(echo $response | jq ".result")
    is_alive=$(echo $is_alive | jq ".[0]")
    is_alive=$(echo $is_alive | jq ".status")
    >&2 echo "is_alive: $is_alive"

    if [ $is_alive = '404' ]; then
        echo "false"
    else
        echo "true"
    fi
}
