#!/bin/bash

function upload_to_vivo () {
    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://vivo.sx/api/v1/upload/$filesize" -H "Content-Type: application/json" -H "X-AUTH: $api_key")
    upload_server=$(echo $upload_server | jq ".upload_url")
    upload_server=${upload_server//\"}
    # echo "Location: $upload_server"  >&2

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

    echo "$vivo_url"
}

function vivo_check_if_file_is_alive () {
    file_url=$1
    
    file_id=${file_url##*/}
    # >&2 echo "File ID: $file_id"
    
    response=$(curl -s -X GET "https://vivo.sx/api/v1/files/$file_id" -H "Content-Type: application/json" )
    # >&2 echo "Response: $response"
    
    is_alive=$(echo $response | jq ".result")
    is_alive=$(echo $is_alive | jq "first(.[])")
    is_alive=$(echo $is_alive | jq ".exists")
    echo "$is_alive"
}
