forked from go-iiif/go-iiif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.go
173 lines (120 loc) · 3.51 KB
/
parallel.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
package process
import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"log/slog"
"net/url"
"sync"
iiifuri "github.com/go-iiif/go-iiif-uri"
iiifconfig "github.com/go-iiif/go-iiif/v6/config"
iiifdriver "github.com/go-iiif/go-iiif/v6/driver"
iiifservice "github.com/go-iiif/go-iiif/v6/service"
)
// ParallelProcessURIWithInstructionSet processes 'u' according to each instruction in 'instruction_set' in concurrent processes
func ParallelProcessURIWithInstructionSet(cfg *iiifconfig.Config, driver iiifdriver.Driver, pr Processor, instruction_set IIIFInstructionSet, u iiifuri.URI) (map[string]interface{}, error) {
done_ch := make(chan bool)
err_ch := make(chan error)
remaining := len(instruction_set) + 1 // + 1 because we fetch the palette for the source image
results := make(map[string]interface{})
results["origin"] = u.Origin()
results["origin_uri"] = u.String()
uris := make(map[Label]string)
dimensions := make(map[Label][]int)
mu := new(sync.RWMutex)
go func() {
defer func() {
done_ch <- true
}()
origin := u.Origin()
im, err := driver.NewImageFromConfig(cfg, origin)
if err != nil {
err_ch <- fmt.Errorf("Failed to load image (%s) for processing profile.services : %w", u, err)
return
}
// PLEASE PUT THIS IN A FUNCTION SOMEWHERE
hash := sha1.Sum(im.Body())
fingerprint := hex.EncodeToString(hash[:])
mu.Lock()
results["origin_fingerprint"] = fingerprint
mu.Unlock()
ctx := context.Background()
for _, service_name := range cfg.Profile.Services.Enable {
service_uri := fmt.Sprintf("%s://", service_name)
service, err := iiifservice.NewService(ctx, service_uri, cfg, im)
if err != nil {
err_ch <- fmt.Errorf("Failed to create service for %s : %w", service_name, err)
return
}
mu.Lock()
results[service_name] = service.Value()
mu.Unlock()
}
}()
ctx := context.Background()
for label, i := range instruction_set {
i = EnsureInstructions(i)
go func(u iiifuri.URI, label Label, i IIIFInstructions) {
defer func() {
done_ch <- true
}()
var process_uri iiifuri.URI
switch u.Scheme() {
case "idsecret":
str_label := fmt.Sprintf("%s", label)
opts := &url.Values{}
opts.Set("label", str_label)
opts.Set("format", i.Format)
if str_label == "o" {
opts.Set("original", "1")
}
target_str, err := u.Target(opts)
if err != nil {
err_ch <- fmt.Errorf("Failed to derive target %s (%s) : %w", u, label, err)
return
}
origin := u.Origin()
rw_str := fmt.Sprintf("%s:///%s?target=%s", iiifuri.REWRITE_SCHEME, origin, target_str)
rw_uri, err := iiifuri.NewURI(ctx, rw_str)
if err != nil {
err_ch <- fmt.Errorf("Failed to generate rewrite URL %s (%s) : %w", u, label, err)
return
}
process_uri = rw_uri
default:
process_uri = u
}
new_uri, im, err := pr.ProcessURIWithInstructions(process_uri, label, i)
if err != nil {
err_ch <- fmt.Errorf("Failed to process %s (%s) : %w", u.String(), label, err)
return
}
dims, err := im.Dimensions()
if err != nil {
err_ch <- fmt.Errorf("Failed to process %s (%s) : %w", u, label, err)
return
}
mu.Lock()
uris[label] = new_uri.String()
dimensions[label] = []int{
dims.Width(),
dims.Height(),
}
mu.Unlock()
}(u, label, i)
}
for remaining > 0 {
select {
case <-done_ch:
remaining -= 1
case err := <-err_ch:
slog.Error(err.Error())
default:
//
}
}
results["uris"] = uris
results["dimensions"] = dimensions
return results, nil
}