/
flattenNew.go
393 lines (313 loc) · 9.88 KB
/
flattenNew.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package ingester
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/timdrysdale/anon"
"github.com/timdrysdale/gradex-cli/comment"
"github.com/timdrysdale/gradex-cli/merge"
"github.com/timdrysdale/gradex-cli/pagedata"
"github.com/timdrysdale/gradex-cli/parselearn"
"github.com/timdrysdale/gradex-cli/parsesvg"
"github.com/timdrysdale/pool"
pdf "github.com/timdrysdale/unipdf/v3/model"
)
func (g *Ingester) FlattenNewPapers(exam string) error {
logger := g.logger.With().Str("process", "flatten-new").Logger()
//assume someone hits a button to ask us to do this ...
// load our identity database
identity, err := anon.New(g.IdentityCSV())
if err != nil {
logger.Error().
Str("file", g.IdentityCSV()).
Str("course", exam).
Str("error", err.Error()).
Msg("Cannot open identity.csv")
return err
}
flattenTasks := []FlattenTask{}
receipts, err := g.GetFileList(g.GetExamDir(exam, acceptedReceipts))
if err != nil {
logger.Error().
Str("dir", g.GetExamDir(exam, acceptedReceipts)).
Str("course", exam).
Str("error", err.Error()).
Msg("Cannot get list of accepted Receipts")
return errors.New("Can't find this exam - please check spelling or ingest your first papers")
}
for _, receipt := range receipts {
sub, err := parselearn.ParseLearnReceipt(receipt)
if err != nil {
logger.Error().
Str("file", receipt).
Str("course", exam).
Str("error", err.Error()).
Msg(fmt.Sprintf("Cannot parse receipt %s because %v", receipt, err))
continue
}
pdfPath, err := GetPDFPath(sub.Filename, g.GetExamDir(exam, acceptedPapers))
if err != nil {
logger.Error().
Str("file", sub.Filename).
Str("dir", g.GetExamDir(exam, acceptedPapers)).
Str("course", exam).
Str("error", err.Error()).
Msg(fmt.Sprintf("couldn't get PDF filename for %s because %v", sub.Filename, err))
continue
}
if !g.Redo { //carry on regardless if forcing a redo
if getDone(pdfPath) { //check for done file - don't process if it exists
logger.Info().
Str("file", pdfPath).
Msg("Skipping flattening - already done")
continue
}
}
logger.Info().
Str("file", pdfPath).
Msg("Flattening new file")
count, err := CountPages(pdfPath)
if err != nil {
logger.Error().
Str("file", pdfPath).
Str("course", exam).
Str("error", err.Error()).
Msg(fmt.Sprintf("couldn't countPages for %s because %v", pdfPath, err))
continue
}
shortDate, err := GetShortLearnDate(sub)
if err != nil {
logger.Error().
Str("file", sub.OwnPath).
Str("course", exam).
Str("error", err.Error()).
Msg(fmt.Sprintf("couldn't get shortlearndate for %s because %v", receipt, err))
continue
}
anonymousIdentity, err := identity.GetAnonymous(sub.Matriculation)
if err != nil {
logger.Error().
Str("identity", sub.Matriculation).
Str("course", exam).
Str("error", err.Error()).
Msg(fmt.Sprintf("couldn't get anonymous identity for for %s because %v\n", sub.Matriculation, err))
continue
}
// we'll use this same set of procDetails for flattens that we do in this batch
// that means we can use the uuid to map the processing in graphviz later, for example
pd := pagedata.PageDetail{}
pd.Is = pagedata.IsPage
pd.Follows = "" //first proces
pd.Revision = 0 //first revision
pd.Own = pagedata.FileDetail{} //depends on stage, fill in when we flatten
pd.Original = pagedata.FileDetail{
Path: pdfPath,
UUID: safeUUID(),
Of: count,
}
pd.Process = pagedata.ProcessDetail{
UUID: safeUUID(),
UnixTime: time.Now().UnixNano(),
Name: "flatten",
By: "gradex-cli",
For: "ingester",
ToDo: "prepare-for-marking",
}
pd.Item = pagedata.ItemDetail{
What: sub.Assignment,
When: shortDate,
Who: anonymousIdentity,
WhoType: pagedata.IsAnonymous,
}
pdataMap := make(map[int]pagedata.PageData)
for page := 1; page <= count; page++ {
thisPd := pd
thisPd.UUID = safeUUID()
thisPd.Original.Number = page
thisPageData := pagedata.PageData{
Current: thisPd,
}
pdataMap[page] = thisPageData
}
renamedBase := g.GetAnonymousFileName(sub.Assignment, anonymousIdentity)
outputPath := filepath.Join(g.GetExamDir(sub.Assignment, anonPapers), renamedBase)
flattenTasks = append(flattenTasks, FlattenTask{
PreparedFor: "ingester",
ToDo: "flattening",
InputPath: pdfPath,
OutputPath: outputPath,
PageCount: count,
PageDataMap: pdataMap})
}
// now process the files
N := len(flattenTasks)
pcChan := make(chan int, N)
tasks := []*pool.Task{}
for i := 0; i < N; i++ {
inputPath := flattenTasks[i].InputPath
outputPath := flattenTasks[i].OutputPath
pdataMap := flattenTasks[i].PageDataMap
newtask := pool.NewTask(func() error {
pc, err := g.FlattenOneNewPDF(inputPath, outputPath, pdataMap, &logger)
pcChan <- pc
if err == nil {
setDone(inputPath, &logger) // so we don't have to do it again
logger.Info().
Int("page-count", pc).
Str("file", inputPath).
Str("destination", outputPath).
Msg("Processing finished OK")
} else {
logger.Error().
Int("page-count", pc).
Str("file", inputPath).
Str("destination", outputPath).
Str("error", err.Error()).
Msg("Processing ERROR")
}
return err
})
tasks = append(tasks, newtask)
}
p := pool.NewPool(tasks, runtime.GOMAXPROCS(-1))
closed := make(chan struct{})
p.Run()
var numErrors int
for _, task := range p.Tasks {
if task.Err != nil {
numErrors++
}
}
if numErrors == 0 {
logger.Info().
Int("task-count", len(p.Tasks)).
Msg(fmt.Sprintf("Processed %d scripts OK", len(p.Tasks)))
} else {
logger.Error().
Int("task-count", len(p.Tasks)).
Int("error-count", numErrors).
Msg(fmt.Sprintf("Processed %d scripts with %d errors", len(p.Tasks), numErrors))
}
close(closed)
return nil
}
func (g *Ingester) FlattenOneNewPDF(inputPath, outputPath string, pageDataMap map[int]pagedata.PageData, logger *zerolog.Logger) (int, error) {
if strings.ToLower(filepath.Ext(inputPath)) != ".pdf" {
logger.Error().
Str("file", inputPath).
Msg(fmt.Sprintf("%s does not appear to be a pdf", inputPath))
return 0, errors.New(fmt.Sprintf("%s does not appear to be a pdf", inputPath))
}
// need page count to find the jpeg files again later
numPages, err := CountPages(inputPath)
// render to images
what := pageDataMap[1].Current.Item.What
jpegPath := g.GetExamDir(what, tempImages) //exam/coursecode
suffix := filepath.Ext(inputPath)
basename := strings.TrimSuffix(filepath.Base(inputPath), suffix)
jpegFileOption := fmt.Sprintf("%s/%s%%04d.jpg", jpegPath, basename)
f, err := os.Open(inputPath)
if err != nil {
logger.Error().
Str("file", inputPath).
Str("error", err.Error()).
Msg(fmt.Sprintf("can't open %s", inputPath))
return 0, err
}
pdfReader, err := pdf.NewPdfReader(f)
if err != nil {
logger.Error().
Str("file", inputPath).
Str("error", err.Error()).
Msg(fmt.Sprintf("can't read %s", inputPath))
return 0, err
}
comments, err := comment.GetComments(pdfReader)
f.Close()
err = ConvertPDFToJPEGs(inputPath, jpegPath, jpegFileOption)
if err != nil {
logger.Error().
Str("file", inputPath).
Str("destination", jpegPath).
Str("naming-format", jpegFileOption).
Str("error", err.Error()).
Msg(fmt.Sprintf("can't convert to images %s", inputPath))
return 0, err
}
// convert images to individual pdfs, with form overlay
pagePath := g.GetExamDir(what, tempPages)
pageFileOption := fmt.Sprintf("%s/%s%%04d.pdf", pagePath, basename)
mergePaths := []string{}
// gs starts indexing at 1
for imgIdx := 1; imgIdx <= numPages; imgIdx = imgIdx + 1 {
pd := pageDataMap[imgIdx]
pageNumber := imgIdx - 1
// construct image name
previousImagePath := fmt.Sprintf(jpegFileOption, imgIdx)
pageFilename := fmt.Sprintf(pageFileOption, imgIdx)
//TODO select Layout to suit landscape or portrait
svgLayoutPath := g.FlattenLayoutSVG()
pd.Current.Own = pagedata.FileDetail{
Path: pageFilename,
UUID: safeUUID(),
Number: imgIdx,
Of: numPages,
}
headerPrefills := parsesvg.DocPrefills{}
headerPrefills[pageNumber] = make(map[string]string)
headerPrefills[pageNumber]["page-number"] = fmt.Sprintf("%d/%d", imgIdx, numPages)
headerPrefills[pageNumber]["author"] = pageDataMap[imgIdx].Current.Item.Who
headerPrefills[pageNumber]["date"] = pageDataMap[imgIdx].Current.Item.When
headerPrefills[pageNumber]["title"] = pageDataMap[imgIdx].Current.Item.What
if len(headerPrefills[pageNumber]["title"]) > 12 {
headerPrefills[pageNumber]["title"] = headerPrefills[pageNumber]["title"][0:13]
}
contents := parsesvg.SpreadContents{
SvgLayoutPath: svgLayoutPath,
SpreadName: "flatten",
PreviousImagePath: previousImagePath,
PageNumber: pageNumber,
PdfOutputPath: pageFilename,
Comments: comments,
PageData: pd,
TemplatePathsRelative: true,
Prefills: headerPrefills,
}
err = parsesvg.RenderSpreadExtra(contents)
if err != nil {
logger.Error().
Str("error", err.Error()).
Msg(fmt.Sprintf("Error rendering spread for %s", inputPath))
return 0, err
}
mergePaths = append(mergePaths, pageFilename)
}
err = merge.PDF(mergePaths, outputPath)
if err != nil {
logger.Error().
Str("error", err.Error()).
Msg(fmt.Sprintf("Error merging for %s: %s", inputPath, err.Error()))
return 0, err
}
doneFile := doneFilePath(outputPath)
_, err = os.Stat(doneFile)
if err == nil {
err = os.Remove(doneFile)
if err != nil {
logger.Error().
Str("file", outputPath).
Str("error", err.Error()).
Msg("Could not delete stale Done File")
}
}
logger.Info().
Str("file", inputPath).
Int("page-count", numPages).
Msg(fmt.Sprintf("processing finished for %s, with %d pages", inputPath, numPages))
return numPages, nil
}