actionExternal.go 1.71 KB
Newer Older
Juergen Enge's avatar
Juergen Enge committed
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
package memostream

import (
	"fmt"
	"net/http"
	"regexp"
)

// image Action using IIIF server for image manipulation
type ActionExternal struct {
	ms *memoServer
}

func NewActionExternal(ms *memoServer) *ActionExternal {
	ae := &ActionExternal{ms: ms}
	return ae
}

func (ae *ActionExternal) GetType() []string {
	return []string{"srfaudio", "srfvideo", "youtube", "vimeo"}
}

func (ae *ActionExternal) Do(w http.ResponseWriter, req *http.Request, me *MediaEntry, action string, params ...string) (bool, error) {
	switch action {
	case "viewer":
		return ae.viewer(w, req, me, action, params...)
	default:
	}
	return false, nil
}

var extMatches = map[string]*regexp.Regexp{
	"srfvideo": regexp.MustCompile("^.+(urn:srf:video:[-0-9a-f]+)$"),
	"srfaudio": regexp.MustCompile("^.+(urn:srf:audio:[-0-9a-f]+)$"),
	"youtube":  regexp.MustCompile("^http.+/([^/]+)$"),
	"vimeo":    regexp.MustCompile("^http.+/([^/]+)$"), // https://vimeo.com/289691364
}

func (ae *ActionExternal) viewer(w http.ResponseWriter, req *http.Request, me *MediaEntry, action string, params ...string) (bool, error) {
	type vData struct {
		Id              string
		BackgroundColor string
		BaseUrl         string
		StaticPrefix    string
	}

	data := vData{
		BackgroundColor: "#000000",
		BaseUrl:         ae.ms.baseUrl,
		StaticPrefix:    ae.ms.staticPrefix,
	}

	rexp, ok := extMatches[me.Type]
	if !ok {
		return false, fmt.Errorf("no id regexp for type %s - %v", me.Type, me.Signature)
	}
	matches := rexp.FindStringSubmatch(me.URI.String())
	if matches != nil {
		data.Id = matches[1]
	}
	tpl, ok := ae.ms.viewerTemplates[me.Type]
	if !ok {
		return false, fmt.Errorf("no template for type %s - %v", me.Type, me.Signature)
	}
	tpl.Execute(w, data)
	return true, nil
}