summaryrefslogtreecommitdiff
path: root/internal/pipeline/get.go
blob: 8fac060b2958bb9d03f63e96fb0617641288c717 (plain)
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
// Copyright 2019 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package pipeline

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

func DownloadBestPages(dir string, name string, conn Downloader) error {
	key := filepath.Join(name, "best")
	fn := filepath.Join(dir, "best")
	err := conn.Download(conn.WIPStorageId(), key, fn)
	if err != nil {
		return fmt.Errorf("Failed to download 'best' file: %v", err)
	}
	f, err := os.Open(fn)
	if err != nil {
		return fmt.Errorf("Failed to open best file: %v", err)
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	for s.Scan() {
		key = filepath.Join(name, s.Text())
		fn = filepath.Join(dir, s.Text())
		conn.Log("Downloading file", key)
		err = conn.Download(conn.WIPStorageId(), key, fn)
		if err != nil {
			return fmt.Errorf("Failed to download file %s: %v", key, err)
		}
	}
	return nil
}

func DownloadBestPngs(dir string, name string, conn Downloader) error {
	key := filepath.Join(name, "best")
	fn := filepath.Join(dir, "best")
	err := conn.Download(conn.WIPStorageId(), key, fn)
	if err != nil {
		return fmt.Errorf("Failed to download 'best' file: %v", err)
	}
	f, err := os.Open(fn)
	if err != nil {
		return fmt.Errorf("Failed to open best file: %v", err)
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	for s.Scan() {
		imgname := strings.Replace(s.Text(), ".hocr", ".png", 1)
		key = filepath.Join(name, imgname)
		fn = filepath.Join(dir, imgname)
		conn.Log("Downloading file", key)
		err = conn.Download(conn.WIPStorageId(), key, fn)
		if err != nil {
			return fmt.Errorf("Failed to download file %s: %v", key, err)
		}
	}
	return nil
}

func DownloadPdfs(dir string, name string, conn Downloader) error {
	anydone := false
	errmsg := ""
	for _, suffix := range []string{".colour.pdf", ".binarised.pdf", ".original.pdf"} {
		key := filepath.Join(name, name+suffix)
		fn := filepath.Join(dir, name+suffix)
		err := conn.Download(conn.WIPStorageId(), key, fn)
		if err != nil {
			_ = os.Remove(fn)
			errmsg += fmt.Sprintf("Failed to download PDF %s: %v\n", key, err)
		} else {
			anydone = true
		}
	}
	if anydone == false {
		return fmt.Errorf("No PDFs could be downloaded, error(s): %v", errmsg)
	}
	return nil
}

func DownloadAnalyses(dir string, name string, conn Downloader) error {
	for _, a := range []string{"conf", "graph.png"} {
		key := filepath.Join(name, a)
		fn := filepath.Join(dir, a)
		err := conn.Download(conn.WIPStorageId(), key, fn)
		// ignore errors with graph.png, as it will not exist in the case of a 1 page book
		if err != nil && a != "graph.png" {
			return fmt.Errorf("Failed to download analysis file %s: %v", key, err)
		}
	}
	return nil
}

func DownloadAll(dir string, name string, conn DownloadLister) error {
	objs, err := conn.ListObjects(conn.WIPStorageId(), name)
	if err != nil {
		return fmt.Errorf("Failed to get list of files for book %s: %v", name, err)
	}
	for _, i := range objs {
		base := filepath.Base(i)
		fn := filepath.Join(dir, base)
		conn.Log("Downloading", i)
		err = conn.Download(conn.WIPStorageId(), i, fn)
		if err != nil {
			return fmt.Errorf("Failed to download file %s: %v", i, err)
		}
	}
	return nil
}