From e84554bd960acec7fbb3f662c69955186b7cc2de Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Thu, 10 Jun 2021 18:00:04 +0200 Subject: [PATCH] Shows ads based on file size --- res/template/advertisements.html | 223 ++++++++++++++++--------------- webcontroller/file_viewer.go | 73 ++++++---- 2 files changed, 156 insertions(+), 140 deletions(-) diff --git a/res/template/advertisements.html b/res/template/advertisements.html index 1ca0641..28c6a7c 100644 --- a/res/template/advertisements.html +++ b/res/template/advertisements.html @@ -1,118 +1,119 @@ {{ define "banner_ads"}} - -{{ if eq .Other.AdBannerType 0 }} - - -{{ else if eq .Other.AdBannerType 1 }} - - - - -{{else if eq .Other.AdBannerType 2}} - -
-
- Pixeldrain needs money to survive
- - Support me on Patreon and get some neat perks + {{ if eq .Other.AdBannerType 1 }} + + + {{ else if eq .Other.AdBannerType 13 }} + + + -
-
- No ads when viewing files
- No ads on your uploaded files
- Longer file retention
-
-
-{{else if eq .Other.AdBannerType 3}} - - - - -{{else if eq .Other.AdBannerType 4}} - - - - -{{ else if eq .Other.AdBannerType 6 }} -
- -
-{{ else if eq .Other.AdBannerType 7 }} - - - -{{ else if eq .Other.AdBannerType 8 }} -
- -
- Are your files expiring too quickly?
- Pixeldrain Pro: Only €2 per month -
-
-{{ else if eq .Other.AdBannerType 9 }} -
- -
- Are your files too large to share?
- Pixeldrain Pro: Only €2 per month -
-
-{{ else if eq .Other.AdBannerType 10 }} -
- -
- Are you tired of advertisements?
- Pixeldrain Pro: Only €2 per month -
-
-{{ else if eq .Other.AdBannerType 11 }} -
- -
- Concerned about your online privacy?
- Pixeldrain Pro: Only €2 per month -
-
-{{ else if eq .Other.AdBannerType 12 }} -
- -
+ {{else if eq .Other.AdBannerType 2}} + +
+
+ Pixeldrain needs money to survive
+ + Support me on Patreon and get some neat perks + +
+
+ No ads when viewing files
+ No ads on your uploaded files
+ Longer file retention
+
+
+ {{else if eq .Other.AdBannerType 3}} + + + + + {{else if eq .Other.AdBannerType 4}} + + + + + {{ else if eq .Other.AdBannerType 6 }} +
+ +
+ {{ else if eq .Other.AdBannerType 7 }} + + + + {{ else if eq .Other.AdBannerType 8 }} +
+ +
+ Are your files expiring too quickly?
+ Pixeldrain Pro: Only €2 per month +
+
+ {{ else if eq .Other.AdBannerType 9 }} +
+ +
+ Are your files too large to share?
+ Pixeldrain Pro: Only €2 per month +
+
+ {{ else if eq .Other.AdBannerType 10 }} +
+ +
+ Are you tired of advertisements?
+ Pixeldrain Pro: Only €2 per month +
+
+ {{ else if eq .Other.AdBannerType 11 }} +
+ +
+ Concerned about your online privacy?
+ Pixeldrain Pro: Only €2 per month +
+
+ {{ else if eq .Other.AdBannerType 12 }} +
+ +
+ {{ end }} {{ end }} -{{ end }} {{ define "floating_ads"}} + {{ if eq .Other.AdFloaterType 3 }} + + + {{ else if eq .Other.AdFloaterType 2 }} + + + {{ else if eq .Other.AdFloaterType 1 }} + + + {{ end }} -{{ if eq .Other.AdFloaterType 3 }} - - -{{ else if eq .Other.AdFloaterType 2 }} - - -{{ else if eq .Other.AdFloaterType 1 }} - - -{{ end }} - + {{ if eq .Other.AdUnderType 1}} + + {{ end }} {{ end }} diff --git a/webcontroller/file_viewer.go b/webcontroller/file_viewer.go index 468bc69..9967fc4 100644 --- a/webcontroller/file_viewer.go +++ b/webcontroller/file_viewer.go @@ -28,11 +28,30 @@ func browserCompat(ua string) bool { return strings.Contains(ua, "MSIE") || strings.Contains(ua, "Trident/7.0") } -func adType() (banner, floater int) { +type viewerData struct { + Type string // file or list + CaptchaKey string + ViewToken string + AdBannerType int + AdFloaterType int + AdUnderType int + FileAdsEnabled bool + UserAdsEnabled bool + Embedded bool + APIResponse interface{} +} + +func (vd *viewerData) adType(files []pixelapi.ListFile) { + var avgSize int64 + for _, v := range files { + avgSize += v.Size + } + avgSize /= int64(len(files)) + const ( // Banners - aAds = 0 - amarulaElectronics = 1 + none = 0 + aAds = 1 patreon = 2 soulStudio = 3 amarulaSolutions = 4 @@ -44,43 +63,40 @@ func adType() (banner, floater int) { pdpro3 = 10 pdpro4 = 11 clickAduBanner = 12 + amarulaElectronics = 13 // Floaters - none = 0 propellerFloat = 1 adSterraFloat = 2 adMavenFloat = 3 + + // Popunders + clickAduPopunder = 1 ) // Intn returns a number up to n, but never n itself. So to get a random 0 // or 1 we need to give it n=2. We can use this function to make other // splits like 1/3 1/4, etc - switch i := rand.Intn(10); i { - case 0, 1: // 20% - banner = clickAduBanner - case 2, 3, 4, 5: // 40% - banner = brave - case 6, 7, 8, 9: // 40% - banner = aAds + switch i := rand.Intn(3); i { + case 0: // 33% + vd.AdBannerType = clickAduBanner + case 1: // 33% + vd.AdBannerType = brave + case 2: // 33% + vd.AdBannerType = aAds default: panic(fmt.Errorf("random number generator returned unrecognised number: %d", i)) } - floater = propellerFloat + // If the file is larger than 10 MB we enable popups + if avgSize > 10e6 { + vd.AdFloaterType = propellerFloat + } - return banner, floater -} - -type viewerData struct { - Type string // file or list - CaptchaKey string - ViewToken string - AdBannerType int - AdFloaterType int - FileAdsEnabled bool - UserAdsEnabled bool - Embedded bool - APIResponse interface{} + // If the file is larger than 250 MB we enable popunders + if avgSize > 250e6 { + vd.AdUnderType = clickAduPopunder + } } // ServeFileViewer controller for GET /u/:id @@ -129,7 +145,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request, FileAdsEnabled: files[0].ShowAds, UserAdsEnabled: !(templateData.Authenticated && templateData.User.Subscription.DisableAdDisplay), } - vd.AdBannerType, vd.AdFloaterType = adType() + vd.adType(files) if len(ids) > 1 { templateData.Title = fmt.Sprintf("%d files on pixeldrain", len(files)) @@ -178,8 +194,7 @@ func (wc *WebController) serveFileViewerDemo(w http.ResponseWriter, r *http.Requ templateData.Other = viewerData{ Type: "file", CaptchaKey: wc.captchaSiteKey, - AdBannerType: 0, // Always show a-ads on the demo page - AdFloaterType: 0, // No floaters + AdBannerType: 1, // Always show a-ads on the demo page FileAdsEnabled: true, UserAdsEnabled: true, APIResponse: map[string]interface{}{ @@ -234,7 +249,7 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request, UserAdsEnabled: !(templateData.Authenticated && templateData.User.Subscription.DisableAdDisplay), APIResponse: list, } - vd.AdBannerType, vd.AdFloaterType = adType() + vd.adType(list.Files) if _, ok := r.URL.Query()["embed"]; ok { vd.Embedded = true