Skip to content

Add hash to redirected page #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions _layouts/redirect.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
<meta http-equiv="refresh" content="0; url={{ page.redirect_to[0] }}">
<h1>Redirecting…</h1>
<a href="{{ page.redirect_to[0] }}">Click here if you are not redirected.</a>
<ul>
{% if page.hash.sha1 %}
<li>SHA1: {{ page.hash.sha1 }}</li>
{% endif %}
</ul>
<script>location="{{ page.redirect_to[0] }}"</script>
39 changes: 37 additions & 2 deletions _scripts/vim_jp-redirects-update/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sync"
Expand All @@ -18,6 +22,8 @@ var pageTmpl = template.Must(template.New("page").Parse(`---
title: {{.Title}}
redirect_to:
- {{.RedirectURL}}
hash:
sha1: {{.Sha1Hash}}
layout: redirect
---
`))
Expand All @@ -42,6 +48,7 @@ type githubRelease struct {
type tmplData struct {
Title string
RedirectURL string
Sha1Hash string
}

func loadData(name string) ([]redirect, error) {
Expand Down Expand Up @@ -77,10 +84,14 @@ func fetchRedirect(d redirect) (*github.Asset, error) {
return nil, nil
}

func updateRedirect(d redirect, a *github.Asset) error {
func updateRedirect(d redirect, a *github.Asset, bin io.ReadCloser) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.ReadCloser を渡すのではなく、この関数のなかでDLしたほうが良い。

Asset の中身だけを見て Redirect を更新するかどうかを決定し、更新時にのみ本体をDLする
という理想形に近づけておくため。

if a.State != "uploaded" {
return fmt.Errorf("not uploaded yet: %s", d.Path)
}
sha1hash, err := calcSha1Hash(bin)
if err != nil {
return err
}
name := d.Path + ".html"
f, err := os.Create(name)
if err != nil {
Expand All @@ -90,13 +101,23 @@ func updateRedirect(d redirect, a *github.Asset) error {
err = pageTmpl.Execute(f, tmplData{
Title: d.Title,
RedirectURL: a.DownloadURL,
Sha1Hash: sha1hash,
})
if err != nil {
return nil
}
return nil
}

func calcSha1Hash(r io.ReadCloser) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この用途なら引数は io.Reader で十分。
ただし複数の hash アルゴリズムに渡すことを考慮するなら単に []byte で良い。
ガチなら io.TeeReader を挟むのだが…

w := sha1.New()
if _, err := io.Copy(w, r); err != nil {
return "", err
}
hash := hex.EncodeToString(w.Sum(nil))
return hash, nil
}

func processRedirect(d redirect) {
a, err := fetchRedirect(d)
if err != nil {
Expand All @@ -106,13 +127,27 @@ func processRedirect(d redirect) {
if a == nil {
return
}
err = updateRedirect(d, a)
bin, err := fetchReleaseBinary(a)
if err != nil {
log.Printf("fetch release hash failed for %s: %s", d.Path, err)
return
}
defer bin.Close()
err = updateRedirect(d, a, bin)
if err != nil {
log.Printf("update failed for %s: %s", d.Path, err)
return
}
}

func fetchReleaseBinary(a *github.Asset) (io.ReadCloser, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数名は downloadAsset(*github.Asset) もしくは fetchURL(string) のが良いだろう。

resp, err := http.DefaultClient.Get(a.DownloadURL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特にクライアントをいじる気がないなら http.Get() を使う。

if err != nil {
return nil, err
}
return resp.Body, nil
}

func main() {
if v, ok := os.LookupEnv("GITHUB_USERNAME"); ok {
github.DefaultClient.Username = v
Expand Down