-
Notifications
You must be signed in to change notification settings - Fork 731
/
image.go
49 lines (42 loc) · 1.5 KB
/
image.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
package smaato
import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/errortypes"
"net/url"
"strings"
)
type imageAd struct {
Image image `json:"image"`
}
type image struct {
Img img `json:"img"`
Impressiontrackers []string `json:"impressiontrackers"`
Clicktrackers []string `json:"clicktrackers"`
}
type img struct {
URL string `json:"url"`
W int `json:"w"`
H int `json:"h"`
Ctaurl string `json:"ctaurl"`
}
func extractAdmImage(adMarkup string) (string, error) {
var imageAd imageAd
if err := json.Unmarshal([]byte(adMarkup), &imageAd); err != nil {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
}
}
var clickEvent strings.Builder
for _, clicktracker := range imageAd.Image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
}
var impressionTracker strings.Builder
for _, impression := range imageAd.Image.Impressiontrackers {
impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
imageAdMarkup := fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
&clickEvent, url.QueryEscape(imageAd.Image.Img.Ctaurl), imageAd.Image.Img.URL, imageAd.Image.Img.W, imageAd.Image.Img.H, &impressionTracker)
return imageAdMarkup, nil
}