Parcourir la Source

Move prince archiving to a separate function

Lucas Stadler 8 ans auparavant
Parent
commit
f9632ffa33
1 fichiers modifiés avec 51 ajouts et 38 suppressions
  1. 51 38
      go/archive/archive.go

+ 51 - 38
go/archive/archive.go

@ -16,6 +16,8 @@ import (
16 16
17 17
var flags struct {
18 18
	open bool
19
20
	archiveDir string
19 21
}
20 22
21 23
type Archive struct {
@ -24,6 +26,12 @@ type Archive struct {
24 26
25 27
func init() {
26 28
	flag.BoolVar(&flags.open, "open", false, "Open the archived page")
29
30
	wd, err := os.Getwd()
31
	if err != nil {
32
		exit("os.Getwd", err)
33
	}
34
	flags.archiveDir = path.Join(wd, ".archive")
27 35
}
28 36
29 37
func main() {
@ -66,13 +74,46 @@ func main() {
66 74
67 75
	fmt.Println("==> Archiving", u)
68 76
77
	resultPath, err := archiveWithPrince(flags.archiveDir, u)
78
	if err != nil {
79
		exit("prince", err)
80
	}
81
82
	if archive.Mappings == nil {
83
		archive.Mappings = make(map[string]string, 1)
84
	}
85
	p = fmt.Sprintf("file://%s", resultPath)
86
	archive.Mappings[u.String()] = p
87
88
	f, err = os.OpenFile("archive.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
89
	if err != nil {
90
		exit("os.OpenFile", err)
91
	}
92
93
	enc := json.NewEncoder(f)
94
	err = enc.Encode(&archive)
95
	if err != nil {
96
		exit("enc.Encode", err)
97
	}
98
	f.Close()
99
100
	fmt.Println("==> Archived at", p)
101
102
	if flags.open {
103
		fmt.Println("==> Opening archive of", u)
104
		open(u, p)
105
	}
106
}
107
108
func archiveWithPrince(dir string, u *url.URL) (resultPath string, err error) {
69 109
	buf := make([]byte, 16)
70 110
	_, err = rand.Read(buf)
71 111
	if err != nil {
72 112
		exit("rand.Read", err)
73 113
	}
74 114
75
	cmd := exec.Command("prince", "--javascript", "--raster-output", fmt.Sprintf(".archive/%x-%%02d.png", buf), u.String())
115
	cmd := exec.Command("prince", "--javascript", "--raster-output", fmt.Sprintf("%x-%%02d.png", buf), u.String())
116
	cmd.Dir = dir
76 117
	cmd.Stderr = prefixWriter("    | ", os.Stderr)
77 118
	cmd.Stdout = prefixWriter("    | ", os.Stdout)
78 119
	fmt.Print("    ", cmd.Args[0])
@ -82,22 +123,22 @@ func main() {
82 123
	fmt.Println()
83 124
	err = cmd.Run()
84 125
	if err != nil {
85
		exit("prince", err)
126
		return "", err
86 127
	}
87 128
88
	parts, err := filepath.Glob(fmt.Sprintf(".archive/%x-*.png", buf))
129
	parts, err := filepath.Glob(path.Join(dir, fmt.Sprintf("%x-*.png", buf)))
89 130
	if err != nil {
90
		exit("filepath.Glob", err)
131
		return "", fmt.Errorf("filepath.Glob: %s", err)
91 132
	}
92 133
93 134
	if len(parts) == 0 {
94
		exit("filepath.Glob", fmt.Errorf("no matches"))
135
		return "", fmt.Errorf("filepath.Glob: no matches")
95 136
	}
96 137
97
	h := fmt.Sprintf(".archive/%x.html", buf)
98
	f, err = os.OpenFile(h, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
138
	h := path.Join(dir, fmt.Sprintf("%x.html", buf))
139
	f, err := os.OpenFile(h, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
99 140
	if err != nil {
100
		exit("os.OpenFile", err)
141
		return "", fmt.Errorf("os.OpenFile: %s", err)
101 142
	}
102 143
103 144
	fmt.Fprintf(f, `<doctype html>
@ -109,42 +150,14 @@ func main() {
109 150
	<body>
110 151
`, u)
111 152
112
	wd, err := os.Getwd()
113
	if err != nil {
114
		exit("os.Getwd", err)
115
	}
116
117 153
	for _, p := range parts {
118
		fmt.Fprintf(f, "<img src=%q />\n", path.Join(wd, p))
154
		fmt.Fprintf(f, "<img src=%q />\n", p)
119 155
	}
120 156
121 157
	fmt.Fprintf(f, "\n\t</body>\n</html>")
122 158
	f.Close()
123 159
124
	if archive.Mappings == nil {
125
		archive.Mappings = make(map[string]string, 1)
126
	}
127
	p = fmt.Sprintf("file://%s", path.Join(wd, h))
128
	archive.Mappings[u.String()] = p
129
130
	f, err = os.OpenFile("archive.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
131
	if err != nil {
132
		exit("os.OpenFile", err)
133
	}
134
135
	enc := json.NewEncoder(f)
136
	err = enc.Encode(&archive)
137
	if err != nil {
138
		exit("enc.Encode", err)
139
	}
140
	f.Close()
141
142
	fmt.Println("==> Archived at", p)
143
144
	if flags.open {
145
		fmt.Println("==> Opening archive of", u)
146
		open(u, p)
147
	}
160
	return h, nil
148 161
}
149 162
150 163
func open(u *url.URL, path string) {