Commit 4188fd5d authored by Juergen Enge's avatar Juergen Enge
Browse files

testurl command added

parent 607ffff3
Pipeline #15432 passed with stages
in 4 minutes and 34 seconds
......@@ -22,6 +22,14 @@ Token Subject: ```cmd:clearcache```
Clears the internal media cache of the server.
should be done after changeing database contents.
### testdata
https://media.memobase.k8s.unibas.ch/command/testdata?type=[image|video]&access=[public|closed]&lifetime=<seconds>&num=<anzahl>&token=XXX
Token Subject: ```cmd:testdata```
delivers json file with testdata (including available metadata)
### media query
......
......@@ -11,12 +11,106 @@
package memostream
import (
"encoding/json"
"expvar"
"fmt"
"github.com/gorilla/mux"
"net/http"
"strconv"
"strings"
)
func (ms *memoServer) TestUrl(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
var mediatype string
types, ok := query["type"]
if ok && len(types) > 0 {
mediatype = types[0]
}
var access string
accesss, ok := query["access"]
if ok && len(accesss) > 0 {
access = accesss[0]
}
var num int64 = 500
nums, ok := query["num"]
if ok && len(nums) > 0 {
var err error
num, err = strconv.ParseInt(nums[0], 10, 64)
if err != nil {
ms.DoPanicf(w, http.StatusBadRequest, "invalid parameter for num=%v", nums[0])
return
}
}
var lifetime int64
lifetimes, ok := query["lifetime"]
if ok && len(lifetimes) > 0 {
var err error
lifetime, err = strconv.ParseInt(lifetimes[0], 10, 64)
if err != nil {
ms.DoPanicf(w, http.StatusBadRequest, "invalid parameter for lifetime=%v", lifetimes[0])
return
}
}
if lifetime == 0 {
lifetime = 3600
}
if num > 500 {
num = 500
}
entries, err := ms.resolver.TestData(mediatype, access, num)
if err != nil {
ms.DoPanicf(w, http.StatusInternalServerError, "cannot load testdata: %v", err)
return
}
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
type resultEntry struct {
Url string
Width, Height, Duration int64
Access string
Type string
Mimetype string
}
result := []resultEntry{}
for _, me := range entries {
url := fmt.Sprintf("%s://%s/%s/%s/master",
scheme,
req.Host,
strings.Trim(ms.urlPrefix, "/"),
me.Signature)
if me.Access == Media_Private {
newtoken, err := NewJWT(ms.jwtSecret, me.Signature, ms.jwtAlg[0], lifetime, "", "")
if err != nil {
ms.DoPanicf(w, http.StatusInternalServerError, "cannot create jwt: %v", err)
}
url += fmt.Sprintf("?auth=%v", newtoken)
}
result = append(result, resultEntry{
Url: url,
Width: me.Width,
Height: me.Height,
Duration: me.Duration,
Type: me.Type,
Access: MediaAccessNum[me.Access],
Mimetype: me.Mimetype,
})
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
jsonEnc := json.NewEncoder(w)
jsonEnc.SetIndent("", " ")
jsonEnc.Encode(result)
}
func (ms *memoServer) commandHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
cmd, ok := vars["cmd"]
......@@ -34,6 +128,8 @@ func (ms *memoServer) commandHandler(w http.ResponseWriter, req *http.Request) {
}
switch cmd {
case "testurl":
ms.TestUrl(w, req)
case "clearcache":
ms.ClearCache()
ms.log.Info("cache cleared")
......
......@@ -61,6 +61,10 @@ func (r *ResolverCache) resolve(signature string) (*MediaEntry, error) {
}
// if data changes, we have to clear the cache to prevent the use of old content
func (r*ResolverCache) ClearCache() {
func (r *ResolverCache) ClearCache() {
r.cache.Purge()
}
\ No newline at end of file
}
func (r *ResolverCache) TestData(mediatype, access string, num int64) ([]*MediaEntry, error) {
return r.r.TestData(mediatype, access, num)
}
......@@ -14,5 +14,6 @@ package memostream
// a signature and a close function for shtutdown
type ResolverDB interface {
Resolve(signature string) (*MediaEntry, error)
TestData(mediatype, access string, num int64) ([]*MediaEntry, error)
Close()
}
......@@ -78,3 +78,56 @@ func (rdm *ResolverDBMySQL) Resolve(signature string) (*MediaEntry, error) {
return nil, emperror.Wrapf(err, "error querying %s - %s", rdm.query, signature)
}
}
func (rdm *ResolverDBMySQL) TestData(mediatype, access string, num int64) ([]*MediaEntry, error) {
sqlstr := "SELECT sig, `uri`, `access`, `proto` AS protocol, `status`, `type`, `mimetype`, `width`, `height`, `duration`" +
" FROM test.entities_metadata" +
" WHERE 1=1"
params := []interface{}{}
if mediatype != "" {
params = append(params, mediatype)
sqlstr += " AND `type`=?"
}
if access != "" {
params = append(params, access)
sqlstr += " AND access=?"
}
sqlstr += " ORDER BY RAND()"
params = append(params, num)
sqlstr += " LIMIT 0, ?"
rows, err := rdm.db.Query(sqlstr, params...)
if err != nil {
return nil, emperror.Wrapf(err, "cannot execute %v - %v", sqlstr, params)
}
defer rows.Close()
entries := []*MediaEntry{}
for rows.Next() {
var sig, access, protocol, uri, status string
var typ, mimetype sql.NullString
var width, height, duration sql.NullInt64
// mapUri data
if err := rows.Scan(&sig, &uri, &access, &protocol, &status, &typ, &mimetype, &width, &height, &duration); err != nil {
return nil, emperror.Wrapf(err, "cannot scan row")
}
m, err := NewMediaEntry(
sig,
uri,
access,
protocol,
status,
typ.String,
mimetype.String,
width.Int64,
height.Int64,
duration.Int64)
if err != nil {
return nil, emperror.Wrapf(err, "cannot create new mediaentry")
}
entries = append(entries, m)
}
return entries, nil
}
......@@ -24,6 +24,10 @@ type ResolverDBStatic struct {
signatures map[string]Sig
}
func (rds *ResolverDBStatic) TestData(mediatype, access string, num int64) ([]*MediaEntry, error) {
panic("implement me")
}
func NewResolverDBStatic(signatures map[string]Sig) *ResolverDBStatic {
return &ResolverDBStatic{signatures: signatures}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment