-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
@@ -18,6 +22,8 @@ var pageTmpl = template.Must(template.New("page").Parse(`--- | |
title: {{.Title}} | ||
redirect_to: | ||
- {{.RedirectURL}} | ||
hash: | ||
sha1: {{.Sha1Hash}} | ||
layout: redirect | ||
--- | ||
`)) | ||
|
@@ -42,6 +48,7 @@ type githubRelease struct { | |
type tmplData struct { | ||
Title string | ||
RedirectURL string | ||
Sha1Hash string | ||
} | ||
|
||
func loadData(name string) ([]redirect, error) { | ||
|
@@ -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 { | ||
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 { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この用途なら引数は |
||
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 { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 関数名は |
||
resp, err := http.DefaultClient.Get(a.DownloadURL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 特にクライアントをいじる気がないなら |
||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Body, nil | ||
} | ||
|
||
func main() { | ||
if v, ok := os.LookupEnv("GITHUB_USERNAME"); ok { | ||
github.DefaultClient.Username = v | ||
|
There was a problem hiding this comment.
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する
という理想形に近づけておくため。