This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathwriter.go
233 lines (180 loc) · 4.2 KB
/
writer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package archive
import (
"archive/zip"
"bufio"
"encoding/json"
"fmt"
"io"
"path/filepath"
"time"
"github.com/google/uuid"
)
// Write writes an archive file from a Zip struct.
// It automatically generates a uuid if not already
// defined in the struct.
func (z *Zip) Write(w io.Writer) error {
// generate random uuid if not defined
if z.UUID == "" {
z.UUID = uuid.New().String()
}
archive := zip.NewWriter(w)
if err := z.writeContent(archive); err != nil {
return err
}
if err := z.writePayload(archive); err != nil {
return err
}
if err := z.writePagedata(archive); err != nil {
return err
}
if err := z.writeThumbnails(archive); err != nil {
return err
}
if err := z.writeMetadata(archive); err != nil {
return err
}
if err := z.writeData(archive); err != nil {
return err
}
archive.Close()
return nil
}
// writeContent writes the .content file to the archive.
func (z *Zip) writeContent(zw *zip.Writer) error {
bytes, err := json.MarshalIndent(&z.Content, "", " ")
if err != nil {
return err
}
name := fmt.Sprintf("%s.content", z.UUID)
w, err := addToZip(zw, name)
if err != nil {
return err
}
if _, err := w.Write(bytes); err != nil {
return err
}
return nil
}
// writePdf writes a pdf file to the archive if existing in the struct.
func (z *Zip) writePayload(zw *zip.Writer) error {
// skip if no pdf
if z.Payload == nil {
return nil
}
name := fmt.Sprintf("%s.%s", z.UUID, z.Content.FileType)
w, err := addToZip(zw, name)
if err != nil {
return err
}
if _, err := w.Write(z.Payload); err != nil {
return err
}
return nil
}
// writePagedata writes a .pagedata file containing
// the name of background templates for each page (one per line).
func (z *Zip) writePagedata(zw *zip.Writer) error {
// don't add pagedata file if no pages
if len(z.Pages) == 0 {
return nil
}
name := fmt.Sprintf("%s.pagedata", z.UUID)
w, err := addToZip(zw, name)
if err != nil {
return err
}
bw := bufio.NewWriter(w)
for _, page := range z.Pages {
template := page.Pagedata
// set default if empty
if template == "" {
template = defaultPagadata
}
bw.WriteString(template "\n")
}
// write to the underlying io.Writer
bw.Flush()
return nil
}
// writeThumbnails writes thumbnail files for each page
// in the archive.
func (z *Zip) writeThumbnails(zw *zip.Writer) error {
for idx, page := range z.Pages {
if page.Thumbnail == nil {
continue
}
folder := fmt.Sprintf("%s.thumbnail", z.UUID)
name := fmt.Sprintf("%d.jpg", idx)
fn := filepath.Join(folder, name)
w, err := addToZip(zw, fn)
if err != nil {
return err
}
if _, err := w.Write(page.Thumbnail); err != nil {
return err
}
}
return nil
}
// writeMetadata writes .json metadata files for each page
// in the archive.
func (z *Zip) writeMetadata(zw *zip.Writer) error {
for idx, page := range z.Pages {
// if no layers available, don't write the metadata file
if len(page.Metadata.Layers) == 0 {
continue
}
name := fmt.Sprintf("%d-metadata.json", idx)
fn := filepath.Join(z.UUID, name)
w, err := addToZip(zw, fn)
if err != nil {
return err
}
bytes, err := json.MarshalIndent(&page.Metadata, "", " ")
if err != nil {
return err
}
if _, err := w.Write(bytes); err != nil {
return err
}
}
return nil
}
// writeData writes .rm data files for each page
// in the archive.
func (z *Zip) writeData(zw *zip.Writer) error {
for idx, page := range z.Pages {
if page.Data == nil {
continue
}
name := fmt.Sprintf("%d.rm", idx)
fn := filepath.Join(z.UUID, name)
w, err := addToZip(zw, fn)
if err != nil {
return err
}
bytes, err := page.Data.MarshalBinary()
if err != nil {
return err
}
if _, err := w.Write(bytes); err != nil {
return err
}
}
return nil
}
// addToZip takes a zip.Writer in parameter and creates an io.Writer
// to write the content of a file to add to the zip.
func addToZip(zw *zip.Writer, name string) (io.Writer, error) {
h := &zip.FileHeader{
Name: name,
Method: zip.Store,
ModifiedTime: uint16(time.Now().UnixNano()),
ModifiedDate: uint16(time.Now().UnixNano()),
}
writer, err := zw.CreateHeader(h)
if err != nil {
return nil, err
}
return writer, nil
}