summaryrefslogtreecommitdiff
path: root/local.go
blob: 0fceca2122689bda708bdf1437f36cdc58cfb4df (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
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
// Copyright 2020 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package bookpipeline

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"
)

const qidPre = "queuePre"
const qidWipe = "queueWipe"
const qidOCR = "queueOCR"
const qidAnalyse = "queueAnalyse"
const storageId = "storage"

// LocalConn is a simple implementation of the pipeliner interface
// that doesn't rely on any "cloud" services, instead doing everything
// on the local machine. This is particularly useful for testing.
type LocalConn struct {
	// these should be set before running Init(), or left to defaults
	TempDir string
	Logger  *log.Logger
}

// MinimalInit does the bare minimum initialisation
func (a *LocalConn) MinimalInit() error {
	var err error
	if a.TempDir == "" {
		a.TempDir = filepath.Join(os.TempDir(), "bookpipeline")
	}
	err = os.MkdirAll(a.TempDir, 0700)
	if err != nil && !os.IsExist(err) {
		return fmt.Errorf("Error creating temporary directory: %v", err)
	}

	err = os.Mkdir(filepath.Join(a.TempDir, storageId), 0700)
	if err != nil && !os.IsExist(err) {
		return fmt.Errorf("Error creating storage directory: %v", err)
	}

	if a.Logger == nil {
		a.Logger = log.New(os.Stdout, "", 0)
	}

	return nil
}

// Init just does the same as MinimalInit
func (a *LocalConn) Init() error {
	err := a.MinimalInit()
	if err != nil {
		return err
	}

	return nil
}

// CheckQueue checks for any messages in a queue
func (a *LocalConn) CheckQueue(url string, timeout int64) (Qmsg, error) {
	f, err := os.Open(filepath.Join(a.TempDir, url))
	if err != nil {
		f, err = os.Create(filepath.Join(a.TempDir, url))
	}
	if err != nil {
		return Qmsg{}, err
	}
	if err != nil {
		return Qmsg{}, err
	}
	defer f.Close()
	r := bufio.NewReader(f)
	s, err := r.ReadString('\n')
	if err != nil && err != io.EOF {
		return Qmsg{}, err
	}
	s = strings.TrimRight(s, "\n")

	return Qmsg{Body: s, Handle: s}, nil
}

// QueueHeartbeat is a no-op with LocalConn
func (a *LocalConn) QueueHeartbeat(msg Qmsg, qurl string, duration int64) (Qmsg, error) {
	return Qmsg{}, nil
}

// GetQueueDetails gets the number of in progress and available
// messages for a queue. These are returned as strings.
func (a *LocalConn) GetQueueDetails(url string) (string, string, error) {
	b, err := ioutil.ReadFile(filepath.Join(a.TempDir, url))
	if err != nil {
		return "", "", err
	}
	s := string(b)
	n := strings.Count(s, "\n")

	return fmt.Sprintf("%d", n), "0", nil
}

func (a *LocalConn) PreQueueId() string {
	return qidPre
}

func (a *LocalConn) WipeQueueId() string {
	return qidWipe
}

func (a *LocalConn) OCRPageQueueId() string {
	return qidOCR
}

func (a *LocalConn) AnalyseQueueId() string {
	return qidAnalyse
}

func (a *LocalConn) WIPStorageId() string {
	return storageId
}

func prefixwalker(dirpath string, prefix string, list *[]ObjMeta) filepath.WalkFunc {
	return func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		n := strings.TrimPrefix(path, dirpath)
		n = strings.TrimPrefix(n, "/")
		n = strings.TrimPrefix(n, "\\")
		o := ObjMeta{Name: n, Date: info.ModTime()}
		*list = append(*list, o)
		return nil
	}
}

func (a *LocalConn) ListObjects(bucket string, prefix string) ([]string, error) {
	var names []string
	list, err := a.ListObjectsWithMeta(bucket, prefix)
	if err != nil {
		return names, err
	}
	for _, v := range list {
		names = append(names, v.Name)
	}
	return names, nil
}

func (a *LocalConn) ListObjectsWithMeta(bucket string, prefix string) ([]ObjMeta, error) {
	var list []ObjMeta
	err := filepath.Walk(filepath.Join(a.TempDir, bucket), prefixwalker(filepath.Join(a.TempDir, bucket), prefix, &list))
	return list, err
}

// AddToQueue adds a message to a queue
func (a *LocalConn) AddToQueue(url string, msg string) error {
	f, err := os.OpenFile(filepath.Join(a.TempDir, url), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.WriteString(msg + "\n")
	return err
}

// DelFromQueue deletes a message from a queue
func (a *LocalConn) DelFromQueue(url string, handle string) error {
	b, err := ioutil.ReadFile(filepath.Join(a.TempDir, url))
	if err != nil {
		return err
	}
	s := string(b)

	i := strings.Index(s, handle)
	if i == -1 {
		return fmt.Errorf("Warning: %s not found in queue %s, so not deleted", handle, url)
	}

	// store the joining of part before and part after handle
	var complete string
	if len(s) >= len(handle)+1 {
		if i > 0 {
			complete = s[:i]
		}
		// the '+1' is for the newline character
		complete += s[i+len(handle)+1:]
	}

	f, err := os.Create(filepath.Join(a.TempDir, url))
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.WriteString(complete)
	return err
}

// Download just copies the file from TempDir/bucket/key to path
func (a *LocalConn) Download(bucket string, key string, path string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	fin, err := os.Open(filepath.Join(a.TempDir, bucket, key))
	if err != nil {
		return err
	}
	defer fin.Close()
	_, err = io.Copy(f, fin)
	return err
}

// Upload just copies the file from path to TempDir/bucket/key
func (a *LocalConn) Upload(bucket string, key string, path string) error {
	d := filepath.Join(a.TempDir, bucket, filepath.Dir(key))
	err := os.MkdirAll(d, 0700)
	if err != nil && !os.IsExist(err) {
		return fmt.Errorf("Error creating temporary directory: %v", err)
	}
	f, err := os.Create(filepath.Join(a.TempDir, bucket, key))
	if err != nil {
		return err
	}
	defer f.Close()

	fin, err := os.Open(path)
	if err != nil {
		return err
	}
	defer fin.Close()
	_, err = io.Copy(f, fin)
	return err
}

func (a *LocalConn) GetLogger() *log.Logger {
	return a.Logger
}

// Log records an item in the with the Logger. Arguments are handled
// as with fmt.Println.
func (a *LocalConn) Log(v ...interface{}) {
	a.Logger.Println(v...)
}