Get File Size of ZIP URL

Use the resources.GetRemote method to query the GitHub REST API.

You could do this with a shortcode…

layouts/shortcodes/github-asset-download.html
{{- /* Set defaults. */}}
{{- $owner := "" }}
{{- $repo := "" }}
{{- $tag := "" }}
{{- $assetName := "" }}

{{- /* Get params. */}}
{{- with .Get "owner" }}
  {{- $owner = . }}
{{- end }}
{{- with .Get "repo" }}
  {{- $repo = . }}
{{- end }}
{{- with .Get "tag" }}
  {{- $tag = . | string }}
{{- end }}
{{- with .Get "assetName" }}
  {{- $assetName = . }}
{{- end }}

{{- /* Validate params. */}}
{{- if not $owner }}
  {{- errorf "The %q shortcode requires a %q parameter. See %s" .Name "owner" .Position }}
{{- end }}
{{- if not $repo }}
  {{- errorf "The %q shortcode requires a %q parameter. See %s" .Name "repo" .Position }}
{{- end }}
{{- if not $tag }}
  {{- errorf "The %q shortcode requires a %q parameter. See %s" .Name "tag" .Position }}
{{- end }}
{{- if not $assetName }}
  {{- errorf "The %q shortcode requires a %q parameter. See %s" .Name "assetName" .Position }}
{{- end }}

{{- $url := printf "https://api.github.com/repos/%s/%s/releases/tags/%s" $owner $repo $tag }}
{{- with resources.GetRemote $url }}
  {{- with .Err }}
    {{- errorf "%s" . }}
  {{- else }}
    {{- with .Content | transform.Unmarshal }}
      {{- with where .assets "name" $assetName }}
        {{- with index . 0 }}
          {{- $size := printf "%.2f MB" (div .size 1048576) -}}
          <a href="{{- .browser_download_url }}">{{ .name }}</a> ({{ $size }})
        {{- end }}
      {{- else }}
        {{- errorf "Unable to find asset with name %q" $assetName }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- else }}
  {{- errorf "Unable to obtain remote resource %s" $url }}
{{- end -}}


markdown

{{< github-asset-download
  owner="tinylog-org"
  repo="tinylog"
  tag="2.5.0"
  assetName="tinylog-2.5.0.zip"
>}}

rendered as

<a href="https://github.com/tinylog-org/tinylog/releases/download/2.5.0/tinylog-2.5.0.zip">tinylog-2.5.0.zip</a> (1.49 MB)
6 Likes