Repository navigation
Expand file tree
/
Copy pathabs.go
More file actions
432 lines (392 loc) · 11.4 KB
/
Copy pathabs.go
File metadata and controls
432 lines (392 loc) · 11.4 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package api
import (
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/url"
"strings"
"github.com/vavallee/bindery/internal/abs"
"github.com/vavallee/bindery/internal/db"
)
const (
SettingABSBaseURL = "abs.base_url"
SettingABSAPIKey = "abs.api_key" //nolint:gosec // #nosec G101 -- settings key name, not a credential value
SettingABSLibraryID = "abs.library_id"
SettingABSLibraryIDs = "abs.library_ids"
SettingABSEnabled = "abs.enabled"
SettingABSLabel = "abs.label"
SettingABSPathRemap = "abs.path_remap"
)
type absClient interface {
Authorize(ctx context.Context) (*abs.AuthorizeResponse, error)
ListLibraries(ctx context.Context) ([]abs.Library, error)
}
type absClientFactory func(baseURL, apiKey string) (absClient, error)
type ABSHandler struct {
settings *db.SettingsRepo
newFn absClientFactory
userAgent string
featureEnabled bool
}
type ABSConfigResponse struct {
FeatureEnabled bool `json:"featureEnabled"`
BaseURL string `json:"baseUrl"`
Label string `json:"label"`
Enabled bool `json:"enabled"`
LibraryID string `json:"libraryId"`
LibraryIDs []string `json:"libraryIds"`
PathRemap string `json:"pathRemap"`
APIKeyConfigured bool `json:"apiKeyConfigured"`
}
type absConfigRequest struct {
BaseURL *string `json:"baseUrl"`
Label *string `json:"label"`
Enabled *bool `json:"enabled"`
LibraryID *string `json:"libraryId"`
LibraryIDs *[]string `json:"libraryIds"`
PathRemap *string `json:"pathRemap"`
APIKey *string `json:"apiKey"`
}
type absProbeRequest struct {
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
}
type absLibraryResponse struct {
ID string `json:"id"`
Name string `json:"name"`
MediaType string `json:"mediaType"`
Icon string `json:"icon"`
Provider string `json:"provider"`
Folders []abs.LibraryFolder `json:"folders"`
}
func NewABSHandler(settings *db.SettingsRepo) *ABSHandler {
h := &ABSHandler{
settings: settings,
featureEnabled: true,
userAgent: abs.UserAgent(""),
}
h.newFn = h.defaultClient
return h
}
func (h *ABSHandler) defaultClient(baseURL, apiKey string) (absClient, error) {
client, err := abs.NewClient(baseURL, apiKey)
if err != nil {
return nil, err
}
return client.WithUserAgent(h.userAgent), nil
}
func (h *ABSHandler) WithFeatureEnabled(enabled bool) *ABSHandler {
h.featureEnabled = enabled
return h
}
func (h *ABSHandler) WithVersion(version string) *ABSHandler {
h.userAgent = abs.UserAgent(version)
return h
}
func (h *ABSHandler) WithUserAgent(userAgent string) *ABSHandler {
userAgent = strings.TrimSpace(userAgent)
if userAgent == "" {
userAgent = abs.UserAgent("")
}
h.userAgent = userAgent
return h
}
func (h *ABSHandler) GetConfig(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, h.loadConfig(r.Context()))
}
func (h *ABSHandler) SetConfig(w http.ResponseWriter, r *http.Request) {
current := h.loadStoredConfig(r.Context())
var req absConfigRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
baseURL := current.BaseURL
if req.BaseURL != nil {
baseURL = strings.TrimSpace(*req.BaseURL)
}
if baseURL != "" {
normalized, err := abs.NormalizeBaseURL(baseURL)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
baseURL = normalized
}
apiKey := current.APIKey
if req.APIKey != nil {
apiKey = strings.TrimSpace(*req.APIKey)
if apiKey != "" {
if _, err := abs.NormalizeAPIKey(apiKey); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
}
}
label := current.Label
if req.Label != nil {
label = strings.TrimSpace(*req.Label)
}
if label == "" {
label = "Audiobookshelf"
}
libraryID := current.LibraryID
libraryIDs := append([]string(nil), current.LibraryIDs...)
if req.LibraryID != nil {
libraryID = strings.TrimSpace(*req.LibraryID)
}
if req.LibraryIDs != nil {
libraryIDs = normalizeABSLibraryIDs("", *req.LibraryIDs)
if len(libraryIDs) > 0 {
libraryID = libraryIDs[0]
} else {
libraryID = ""
}
} else if req.LibraryID != nil {
libraryIDs = normalizeABSLibraryIDs(libraryID, nil)
}
pathRemap := current.PathRemap
if req.PathRemap != nil {
pathRemap = strings.TrimSpace(*req.PathRemap)
}
enabled := current.Enabled
if req.Enabled != nil {
enabled = *req.Enabled
}
libraryIDsJSON, err := json.Marshal(libraryIDs)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
// Persist every settings row atomically. A mid-write failure must not
// leave a half-applied config (e.g. enabled=true with a stale library ID).
kvs := []db.SettingKV{
{Key: SettingABSBaseURL, Value: baseURL},
{Key: SettingABSLabel, Value: label},
{Key: SettingABSEnabled, Value: boolString(enabled)},
{Key: SettingABSLibraryID, Value: libraryID},
{Key: SettingABSLibraryIDs, Value: string(libraryIDsJSON)},
{Key: SettingABSPathRemap, Value: pathRemap},
}
if req.APIKey != nil && strings.TrimSpace(*req.APIKey) != "" {
kvs = append(kvs, db.SettingKV{Key: SettingABSAPIKey, Value: apiKey})
}
if err := h.settings.SetMany(r.Context(), kvs); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, ABSConfigResponse{
BaseURL: baseURL,
Label: label,
Enabled: enabled,
LibraryID: libraryID,
LibraryIDs: libraryIDs,
PathRemap: pathRemap,
APIKeyConfigured: apiKey != "",
FeatureEnabled: h.featureEnabled,
})
}
func (h *ABSHandler) Test(w http.ResponseWriter, r *http.Request) {
client, err := h.clientFromProbe(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
authz, err := client.Authorize(r.Context())
if err != nil {
h.writeProbeError(w, "abs test failed", "", err)
return
}
writeJSON(w, http.StatusOK, map[string]any{
"message": "connected",
"username": authz.User.Username,
"userType": authz.User.Type,
"defaultLibraryId": authz.UserDefaultLibraryID,
"serverVersion": authz.ServerSettings.Version,
"source": authz.Source,
})
}
func (h *ABSHandler) Libraries(w http.ResponseWriter, r *http.Request) {
client, err := h.clientFromProbe(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
libraries, err := client.ListLibraries(r.Context())
if err != nil {
h.writeProbeError(w, "abs list libraries failed", "", err)
return
}
out := make([]absLibraryResponse, 0, len(libraries))
for _, lib := range libraries {
if lib.MediaType != "book" {
continue
}
out = append(out, absLibraryResponse{
ID: lib.ID,
Name: lib.Name,
MediaType: lib.MediaType,
Icon: lib.Icon,
Provider: lib.Provider,
Folders: lib.Folders,
})
}
writeJSON(w, http.StatusOK, out)
}
type ABSStoredConfig struct {
BaseURL string
APIKey string
Label string
LibraryID string
LibraryIDs []string
PathRemap string
Enabled bool
}
func LoadABSConfig(ctx context.Context, settings *db.SettingsRepo) ABSStoredConfig {
get := func(key string) string {
s, _ := settings.Get(ctx, key)
if s == nil {
return ""
}
return s.Value
}
label := get(SettingABSLabel)
if label == "" {
label = "Audiobookshelf"
}
libraryID := get(SettingABSLibraryID)
rawLibraryIDs := get(SettingABSLibraryIDs)
libraryIDs := decodeABSLibraryIDs(rawLibraryIDs)
if strings.TrimSpace(rawLibraryIDs) == "" || len(libraryIDs) == 0 {
libraryIDs = normalizeABSLibraryIDs(libraryID, libraryIDs)
} else {
libraryID = libraryIDs[0]
}
return ABSStoredConfig{
BaseURL: get(SettingABSBaseURL),
APIKey: get(SettingABSAPIKey),
Label: label,
LibraryID: libraryID,
LibraryIDs: libraryIDs,
PathRemap: get(SettingABSPathRemap),
Enabled: strings.EqualFold(get(SettingABSEnabled), "true"),
}
}
func (h *ABSHandler) loadStoredConfig(ctx context.Context) ABSStoredConfig {
return LoadABSConfig(ctx, h.settings)
}
func (h *ABSHandler) loadConfig(ctx context.Context) ABSConfigResponse {
cfg := h.loadStoredConfig(ctx)
return ABSConfigResponse{
FeatureEnabled: h.featureEnabled,
BaseURL: cfg.BaseURL,
Label: cfg.Label,
Enabled: cfg.Enabled,
LibraryID: cfg.LibraryID,
LibraryIDs: cfg.LibraryIDs,
PathRemap: cfg.PathRemap,
APIKeyConfigured: cfg.APIKey != "",
}
}
func (h *ABSHandler) clientFromProbe(r *http.Request) (absClient, error) {
current := h.loadStoredConfig(r.Context())
req := absProbeRequest{}
if r.Body != nil {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
return nil, errors.New("invalid request body")
}
}
baseURL := strings.TrimSpace(current.BaseURL)
if baseURL == "" {
return nil, errors.New("ABS base URL must be saved before probing")
}
apiKey := strings.TrimSpace(req.APIKey)
if apiKey == "" {
apiKey = current.APIKey
}
return h.newConfiguredClient(baseURL, apiKey)
}
func (h *ABSHandler) newConfiguredClient(baseURL, apiKey string) (absClient, error) {
if strings.TrimSpace(baseURL) == "" {
return nil, errors.New("base_url is required")
}
apiKey, err := abs.NormalizeAPIKey(apiKey)
if err != nil {
return nil, err
}
if apiKey == "" {
return nil, errors.New("api_key is required")
}
return h.newFn(baseURL, apiKey)
}
func decodeABSLibraryIDs(raw string) []string {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil
}
var values []string
if err := json.Unmarshal([]byte(raw), &values); err != nil {
return nil
}
return values
}
func normalizeABSLibraryIDs(primary string, values []string) []string {
primary = strings.TrimSpace(primary)
out := make([]string, 0, len(values)+1)
seen := make(map[string]struct{}, len(values)+1)
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if _, ok := seen[value]; ok {
continue
}
seen[value] = struct{}{}
out = append(out, value)
}
if primary != "" {
if _, ok := seen[primary]; !ok {
out = append([]string{primary}, out...)
}
}
return out
}
func (h *ABSHandler) writeProbeError(w http.ResponseWriter, logMsg, baseURL string, err error) {
var apiErr *abs.APIError
if errors.As(err, &apiErr) {
switch apiErr.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "ABS rejected the API key"})
return
case http.StatusNotFound:
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "library not found or not accessible"})
return
default:
slog.Warn(logMsg, "status", apiErr.StatusCode, "host", redactABSHost(baseURL), "error", apiErr.Message)
writeJSON(w, http.StatusBadGateway, map[string]string{"error": apiErr.Message})
return
}
}
slog.Warn(logMsg, "host", redactABSHost(baseURL), "error", err)
writeJSON(w, http.StatusBadGateway, map[string]string{"error": err.Error()})
}
func redactABSHost(raw string) string {
if raw == "" {
return ""
}
u, err := url.Parse(raw)
if err != nil {
return ""
}
return u.Host
}
func boolString(v bool) string {
if v {
return "true"
}
return "false"
}