-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.go
172 lines (144 loc) · 4.84 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"hash/fnv"
"github.com/julienschmidt/httprouter"
"github.com/lithammer/shortuuid"
"github.com/rs/zerolog/log"
)
// Simple hash function used to generate a string hash of the canvas uri
// to have a unique stable annotation list id
func hash(s string) string {
h := fnv.New64a()
h.Write([]byte(s))
return strconv.FormatUint(h.Sum64(), 16)
}
// List display the annotation list
func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var body json.RawMessage
canvas, _ := r.URL.Query()["canvas"]
annotationlisthash := hash(canvas[0])
list := AnnotationList{
Context: "http://iiif.io/api/presentation/2/context.json",
ID: "https://docuver.se/iiif/annotation/list" + annotationlisthash,
Type: "sc:AnnotationList",
}
rows, err := db.Query("SELECT body FROM annotations where target=?", canvas[0])
if err != nil {
log.Error().Err(err).Str("action", "list").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&body)
if err != nil {
log.Error().Err(err).Str("action", "list").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
list.Resources = append(list.Resources, body)
}
if len(list.Resources) == 0 {
list.Resources = append(list.Resources, json.RawMessage("{}"))
}
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err := enc.Encode(list); err != nil {
log.Error().Err(err).Str("action", "list").Str("canvas", canvas[0]).Msg("annotation list error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
}
// Get retrieve a single annotation with database id
// NOTE: just for test, not used by mirador
func Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
var body string
err := db.QueryRow("SELECT body FROM annotations where id=?", ps.ByName("id")).Scan(&body)
if err != nil {
log.Error().Err(err).Str("action", "get").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, body)
}
// Delete an annotation
// the output of this controller is the annotation body that is being deleted
func Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var anno string
err := db.QueryRow("SELECT body FROM annotations where annoid = ?", ps.ByName("id")).Scan(&anno)
if err != nil {
log.Error().Err(err).Str("action", "delete").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
_, err = db.Exec("DELETE FROM annotations where annoid=?", ps.ByName("id"))
if err != nil {
log.Error().Err(err).Str("action", "delete").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
log.Info().Str("annotation_id", ps.ByName("id")).Str("action", "delete").Msg("")
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, anno)
}
// Create an annotation
func Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Error().Err(err).Str("action", "create").Msg("")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
var annotation Annotation
err = json.Unmarshal(body, &annotation)
if err != nil {
log.Error().Err(err).Str("action", "create").Msg("")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
annoid := shortuuid.New()
annotation.ID = "https://docuver.se/iiif/annotation/" + annoid
AnnotationWithID, _ := json.Marshal(annotation)
statement, _ := db.Prepare("INSERT INTO annotations (annoid, created_at, target, manifest, body) VALUES (?, ?, ?, ?, ?)")
statement.Exec(annoid, time.Now(), annotation.Canvas(), annotation.Manifest(), AnnotationWithID)
log.Info().Str("annotation_id", annoid).Str("action", "create").Msg("")
fmt.Fprintf(w, string(AnnotationWithID))
}
// Update an annotation
func Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
annoid := ps.ByName("id")
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Error().Err(err).Str("action", "update").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
_, err = db.Exec("UPDATE annotations SET body=? WHERE annoid=?", body, annoid)
if err != nil {
log.Error().Err(err).Str("action", "update").Msg("db error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500"))
return
}
log.Info().Str("annotation_id", annoid).Str("action", "update").Msg("")
fmt.Fprintf(w, string(body))
}