-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_media_image_notes.go
204 lines (145 loc) · 4.37 KB
/
api_media_image_notes.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
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
// Image notes API
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
)
type ImageNote struct {
XPosition int `json:"x"`
YPosition int `json:"y"`
Width int `json:"w"`
Height int `json:"h"`
Text string `json:"text"`
}
type ImageNotesSetResponse struct {
Url string `json:"url"`
}
func api_setImageNotes(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)
if session == nil {
ReturnAPIError(response, 401, "UNAUTHORIZED", "You must provide a valid active session to use this API.")
return
}
if !session.write {
ReturnAPIError(response, 403, "ACCESS_DENIED", "Your current session does not have permission to make use of this API.")
return
}
vars := mux.Vars(request)
media_id, err := strconv.ParseUint(vars["mid"], 10, 64)
if err != nil {
response.WriteHeader(400)
return
}
var p []ImageNote = make([]ImageNote, 0)
err = json.NewDecoder(request.Body).Decode(&p)
if err != nil {
response.WriteHeader(400)
return
}
assetData, err := json.Marshal(p)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
result := ImageNotesSetResponse{
Url: "",
}
// Encrypt the notes file
notes_encrypted_file, err := EncryptAssetData(assetData, session.key)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
// Put the notes into the media assets
media := GetVault().media.AcquireMediaResource(media_id)
if media == nil {
os.Remove(notes_encrypted_file)
ReturnAPIError(response, 404, "NOT_FOUND", "Media not found")
return
}
meta, err := media.StartWrite(session.key)
if err != nil {
LogError(err)
os.Remove(notes_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
if meta == nil {
media.CancelWrite()
os.Remove(notes_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 404, "NOT_FOUND", "Media not found")
return
}
if meta.Type != MediaTypeImage {
media.CancelWrite()
os.Remove(notes_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 400, "NOT_SUPPORTED", "This feature is not supported for the media type. Only for images.")
return
}
notes_asset := meta.NextAssetID
meta.NextAssetID++
success, asset_path, asset_lock := media.AcquireAsset(notes_asset, ASSET_SINGLE_FILE)
if !success {
media.CancelWrite()
os.Remove(notes_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 404, "NOT_FOUND", "Media not found")
return
}
asset_lock.RequestWrite()
asset_lock.StartWrite()
// Move temp file
err = RenameAndReplace(notes_encrypted_file, asset_path)
asset_lock.EndWrite()
media.ReleaseAsset(notes_asset)
if err != nil {
LogError(err)
media.CancelWrite()
os.Remove(notes_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
// Change metadata
if meta.HasImageNotes {
// Remove old asset
oldAsset := meta.ImageNotesAsset
success, asset_path, asset_lock = media.AcquireAsset(oldAsset, ASSET_SINGLE_FILE)
if success {
asset_lock.RequestWrite()
asset_lock.StartWrite()
os.Remove(asset_path)
asset_lock.EndWrite()
media.ReleaseAsset(oldAsset)
}
}
meta.HasImageNotes = true
meta.ImageNotesAsset = notes_asset
result.Url = "/assets/b/" + fmt.Sprint(media_id) + "/" + fmt.Sprint(notes_asset) + "/notes.json" + "?fp=" + GetVault().credentials.GetFingerprint()
// Save
err = media.EndWrite(meta, session.key, false)
if err != nil {
LogError(err)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
GetVault().media.ReleaseMediaResource(media_id)
// Response
jsonResult, err := json.Marshal(result)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
ReturnAPI_JSON(response, request, jsonResult)
}