-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_media_thumbnail.go
290 lines (197 loc) · 5.83 KB
/
api_media_thumbnail.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
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
// Media thumbnail update (by the user)
package main
import (
"encoding/json"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
)
type ThumbnailAPIResponse struct {
Url string `json:"url"`
}
func api_editMediaThumbnail(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
}
_, p, err := mime.ParseMediaType(request.Header.Get("Content-Type"))
if err != nil {
response.WriteHeader(400)
return
}
boundary := p["boundary"]
reader := multipart.NewReader(request.Body, boundary)
part, err := reader.NextPart()
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
tempFile := GetTemporalFileName("", false)
// Write to temp file
f, err := os.OpenFile(tempFile, os.O_WRONLY|os.O_CREATE, FILE_PERMISSION)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
buf := make([]byte, 1024*1024)
finished := false
for !finished {
n, err := part.Read(buf)
if err != nil && err != io.EOF {
LogError(err)
f.Close()
WipeTemporalFile(tempFile)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
if err == io.EOF {
finished = true
}
if n == 0 {
continue
}
_, err = f.Write(buf[:n])
if err != nil {
LogError(err)
f.Close()
WipeTemporalFile(tempFile)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
}
f.Close()
// Probe uploaded file
probe_data, err := ProbeMediaFileWithFFProbe(tempFile)
if err != nil {
LogError(err)
WipeTemporalFile(tempFile)
ReturnAPIError(response, 400, "INVALID_THUMBNAIL", "Invalid thumbnail provided")
return
}
// Generate a thumbnail
thumbnail, err := GenerateThumbnailFromMedia(tempFile, probe_data)
WipeTemporalFile(tempFile)
if err != nil {
LogError(err)
ReturnAPIError(response, 400, "INVALID_THUMBNAIL", "Invalid thumbnail provided")
return
}
if thumbnail == "" {
ReturnAPIError(response, 400, "INVALID_THUMBNAIL", "Invalid thumbnail provided")
return
}
// Encrypt the thumbnail
thumb_encrypted_file, err := EncryptAssetFile(thumbnail, session.key)
WipeTemporalFile(thumbnail)
if err != nil {
LogError(err)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
// Put the thumbnail into the media assets
media := GetVault().media.AcquireMediaResource(media_id)
if media == nil {
os.Remove(thumb_encrypted_file)
ReturnAPIError(response, 404, "NOT_FOUND", "Media not found")
return
}
meta, err := media.StartWrite(session.key)
if err != nil {
LogError(err)
os.Remove(thumb_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(thumb_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 404, "NOT_FOUND", "Media not found")
return
}
thumb_asset := meta.NextAssetID
meta.NextAssetID++
success, asset_path, asset_lock := media.AcquireAsset(thumb_asset, ASSET_SINGLE_FILE)
if !success {
media.CancelWrite()
os.Remove(thumb_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(thumb_encrypted_file, asset_path)
asset_lock.EndWrite()
media.ReleaseAsset(thumb_asset)
err2 := GetVault().albums.OnMediaThumbnailUpdate(media_id, session.key)
if err2 != nil {
LogError(err)
}
if err != nil {
LogError(err)
media.CancelWrite()
os.Remove(thumb_encrypted_file)
GetVault().media.ReleaseMediaResource(media_id)
ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}
// Change metadata
hasAssetToRemove := meta.ThumbnailReady
assetToRemove := meta.ThumbnailAsset
meta.ThumbnailReady = true
meta.ThumbnailAsset = thumb_asset
// 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
}
// Remove old asset
if hasAssetToRemove {
success, asset_path, asset_lock = media.AcquireAsset(assetToRemove, ASSET_SINGLE_FILE)
if success {
asset_lock.RequestWrite()
asset_lock.StartWrite()
os.Remove(asset_path)
asset_lock.EndWrite()
media.ReleaseAsset(assetToRemove)
}
}
// Release media
GetVault().media.ReleaseMediaResource(media_id)
// Clear cache
GetVault().media.preview_cache.RemoveEntryOrMarkInvalid(media_id)
// Response
var result ThumbnailAPIResponse
result.Url = "/assets/b/" + fmt.Sprint(media_id) + "/" + fmt.Sprint(thumb_asset) + "/thumbnail.jpg" + "?fp=" + GetVault().credentials.GetFingerprint()
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)
}