-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
60 lines (51 loc) · 1.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/gopherdojo/dojo2/kadai3-2/tokunaga"
)
const (
ExitOK = iota
ExitAbort
)
const timeLayout = "2006-01-02 15:04:05"
func main() {
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Fprintln(os.Stderr, "You can only download one file at a time")
os.Exit(ExitAbort)
}
downloadPath := flag.Args()[0]
t := time.Now()
fmt.Printf("--%s-- %s\n", t.Format(timeLayout), downloadPath)
fmt.Print("HTTP による接続要求を送信しました、応答を待っています...")
// head request
responseHead, err := http.Head(downloadPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(ExitAbort)
}
// response info
acceptRanges := responseHead.Header.Get("Accept-Ranges")
statusCode := responseHead.StatusCode
fileSize := responseHead.ContentLength
contentType := responseHead.Header.Get("Content-Type")
downloadFile := tokunaga.File{Uri: downloadPath, FileSize: fileSize}
fmt.Printf("%d %s\n", statusCode, http.StatusText(statusCode))
if responseHead.StatusCode != 200 {
fmt.Fprintln(os.Stderr, http.StatusText(responseHead.StatusCode))
os.Exit(ExitAbort)
}
fmt.Printf("長さ: %d [%s]\n", fileSize, contentType)
fmt.Printf("`%s' に保存中\n", downloadFile.Filename())
fmt.Println("")
if err := downloadFile.Download(acceptRanges); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(ExitAbort)
}
fmt.Printf("`%s' に保存完了\n", downloadFile.Filename())
os.Exit(ExitOK)
}