4
0

appimage-update-lib.sh 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Shared by scripts/deploy-site.sh and scripts/publish-appimage-update.sh: the AppImage's fixed update address.
  2. # The live site holds the address's target (update/.htaccess, a 302 to a release's .zsync), not this repository, so a site
  3. # deploy from an old checkout cannot move or remove it.
  4. UPDATE_ADDRESS="${LYNX_UPDATE_ADDRESS:-https://lynx-share.com/update/LYNX-x86_64.AppImage.zsync}"
  5. # the arm64 AppImage's address (round 190oy, from 0.1.31): the same file holds both redirects
  6. UPDATE_ADDRESS_AARCH64="${LYNX_UPDATE_ADDRESS_AARCH64:-https://lynx-share.com/update/LYNX-aarch64.AppImage.zsync}"
  7. UPDATE_REPO="${LYNX_UPDATE_REPO:-Ka1-S/LYNX-SHARE}"
  8. # the first release whose AppImage reads the address
  9. UPDATE_FIRST_VERSION="v0.1.25"
  10. # Prints where the address points now, nothing when it has no target yet. Fails when the site can't tell (no answer, an
  11. # error, a target that isn't a release's .zsync): callers must not go on guessing.
  12. update_target()
  13. {
  14. local answer code url
  15. answer="$(curl -s -o /dev/null -w '%{http_code} %{redirect_url}' --max-time 30 "$UPDATE_ADDRESS?check=$$")" || return 1
  16. code="${answer%% *}"; url="${answer#* }"; url="${url%%\?*}" # the server echoes our ?check= query on the redirect
  17. case "$code" in
  18. 404) return 0 ;;
  19. 302) [[ "$url" =~ ^https://github\.com/$UPDATE_REPO/releases/download/v[^/]+/LYNX-[^/]+\.AppImage\.zsync$ ]] || return 1
  20. printf '%s\n' "$url" ;;
  21. *) return 1 ;;
  22. esac
  23. }
  24. # Round 190qp: where the arm64 address points now (update_target's rules for UPDATE_ADDRESS_AARCH64). deploy-site.sh rewrote
  25. # update/.htaccess with the x86_64 redirect only, so every site deploy after 0.1.31 left the arm64 address at 404.
  26. update_target_aarch64()
  27. {
  28. UPDATE_ADDRESS="$UPDATE_ADDRESS_AARCH64" update_target
  29. }
  30. # The tag in a target URL.
  31. update_target_tag() { sed -n 's#.*/releases/download/\([^/]*\)/.*#\1#p' <<<"$1"; }
  32. # update/.htaccess for a target URL, on stdout: update_htaccess <x86_64 .zsync URL> [<aarch64 .zsync URL>]
  33. update_htaccess()
  34. {
  35. printf '%s\n' "# Written by scripts/publish-appimage-update.sh. The AppImage's fixed update address: the newest published release." \
  36. "Redirect 302 /update/LYNX-x86_64.AppImage.zsync $1"
  37. [[ -z "${2:-}" ]] || printf '%s\n' "Redirect 302 /update/LYNX-aarch64.AppImage.zsync $2"
  38. printf '%s\n' '<IfModule mod_headers.c>' 'Header always set Cache-Control "no-store"' '</IfModule>'
  39. }
  40. # Uploads one file to a path of the site, nothing else: site_upload <local file> <path on the site>
  41. site_upload()
  42. {
  43. local token base=https://developers.hostinger.com answer url auth rest size
  44. token="$(cat ~/.config/hostinger/token)"
  45. answer="$(curl -sf -X POST -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \
  46. -d '{"username":"u258086853","domain":"lynx-share.com"}' "$base/api/hosting/v1/files/upload-urls")" || return 1
  47. url="$(python3 -c "import json,sys;print(json.load(sys.stdin)['url'])" <<<"$answer")"
  48. auth="$(python3 -c "import json,sys;print(json.load(sys.stdin)['auth_key'])" <<<"$answer")"
  49. rest="$(python3 -c "import json,sys;print(json.load(sys.stdin)['rest_auth_key'])" <<<"$answer")"
  50. size="$(stat -c%s "$1")"
  51. local headers=(-H "X-Auth: $auth" -H "X-Auth-Rest: $rest" -H 'Tus-Resumable: 1.0.0')
  52. curl -sf -o /dev/null -X POST "$url/$2?override=true" "${headers[@]}" -H "Upload-Length: $size" -H 'Upload-Offset: 0' || return 1
  53. curl -sf -o /dev/null -X PATCH "$url/$2?override=true" "${headers[@]}" -H 'Upload-Offset: 0' \
  54. -H 'Content-Type: application/offset+octet-stream' --data-binary @"$1"
  55. }
  56. # A loud line when the newest published release's AppImage isn't what the address offers (the step that is easy to miss).
  57. update_reminder()
  58. {
  59. local newest current
  60. command -v gh > /dev/null || { echo "REMINDER: check that the AppImage update address is current: scripts/publish-appimage-update.sh v<label>"; return 0; }
  61. newest="$(gh api "repos/$UPDATE_REPO/releases?per_page=1" --jq '.[0].tag_name' 2> /dev/null)" || return 0
  62. [[ -n "$newest" && "$(printf '%s\n%s\n' "$UPDATE_FIRST_VERSION" "$newest" | sort -V | head -1)" == "$UPDATE_FIRST_VERSION" ]] || return 0
  63. current="$(update_target_tag "${1:-}")"
  64. if [[ "$current" != "$newest" ]]; then
  65. echo "=================================================================================="
  66. echo "REMINDER: the AppImage update address offers '${current:-nothing}', the newest release is $newest."
  67. echo "Linux update checks won't offer $newest until you run: scripts/publish-appimage-update.sh $newest"
  68. echo "=================================================================================="
  69. fi
  70. }