From ef083ff0874c3712f39637b95ed7e49b54d4bd02 Mon Sep 17 00:00:00 2001 From: Fornax Date: Mon, 29 Nov 2021 16:13:19 +0100 Subject: [PATCH] Invalidate caches when the server restarts --- go.mod | 2 +- go.sum | 2 + res/template/account/user_files.html | 68 -------- res/template/account/user_lists.html | 60 ------- res/template/account/user_settings.html | 21 --- res/template/admin.html | 4 +- res/template/advertisements.html | 161 ------------------ res/template/{account => }/email_confirm.html | 0 res/template/file_viewer_svelte.html | 4 +- res/template/filesystem.html | 4 +- res/template/home.html | 4 +- res/template/{account => }/user_buckets.html | 4 +- ...le_manager.html => user_file_manager.html} | 4 +- res/template/{account => }/user_home.html | 4 +- .../{account/logout.html => user_logout.html} | 0 svelte/src/admin_panel/Subscriptions.svelte | 59 +++++-- webcontroller/templates.go | 4 + webcontroller/web_controller.go | 17 +- 18 files changed, 80 insertions(+), 342 deletions(-) delete mode 100644 res/template/account/user_files.html delete mode 100644 res/template/account/user_lists.html delete mode 100644 res/template/account/user_settings.html delete mode 100644 res/template/advertisements.html rename res/template/{account => }/email_confirm.html (100%) rename res/template/{account => }/user_buckets.html (72%) rename res/template/{account/file_manager.html => user_file_manager.html} (81%) rename res/template/{account => }/user_home.html (77%) rename res/template/{account/logout.html => user_logout.html} (100%) diff --git a/go.mod b/go.mod index 1550f56..b47977b 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( fornaxian.tech/config v0.0.0-20211108212237-6133aed90586 fornaxian.tech/log v0.0.0-20211102185326-552e9b1f8640 - fornaxian.tech/pixeldrain_api_client v0.0.0-20211128192613-093515ebeaa7 + fornaxian.tech/pixeldrain_api_client v0.0.0-20211128195924-7b5d3f7293df fornaxian.tech/util v0.0.0-20211102152345-9a486dee9787 github.com/julienschmidt/httprouter v1.3.0 github.com/microcosm-cc/bluemonday v1.0.16 diff --git a/go.sum b/go.sum index a8985b1..1e62191 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ fornaxian.tech/log v0.0.0-20211102185326-552e9b1f8640 h1:UPDxJwLRCfh/cv80UMSanzm fornaxian.tech/log v0.0.0-20211102185326-552e9b1f8640/go.mod h1:sN82qMToeHhP2u3ehvrcE8y1IudRZJAZO9yG5OBYblo= fornaxian.tech/pixeldrain_api_client v0.0.0-20211128192613-093515ebeaa7 h1:Gx7SRu906VslyPcIL9D3uw3B2uO6u7mIwsMD7SPr+9k= fornaxian.tech/pixeldrain_api_client v0.0.0-20211128192613-093515ebeaa7/go.mod h1:uajB2ofEsefUtxjvs4m7SDyPVRlfrI3qzCSWcud47hY= +fornaxian.tech/pixeldrain_api_client v0.0.0-20211128195924-7b5d3f7293df h1:eFpcIMRUYqqYr+VeBDvVENsFBjV/UotMqJfWdWbprYE= +fornaxian.tech/pixeldrain_api_client v0.0.0-20211128195924-7b5d3f7293df/go.mod h1:uajB2ofEsefUtxjvs4m7SDyPVRlfrI3qzCSWcud47hY= fornaxian.tech/util v0.0.0-20211102152345-9a486dee9787 h1:9ujI8Qi6+FTL/YW6xQAS9DmWDMerHBe8foQvVD/G/i0= fornaxian.tech/util v0.0.0-20211102152345-9a486dee9787/go.mod h1:FqVgfghmxTGR3l9Zx4MOMeZ9KHjiEFl3s3C0BSTvBwk= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= diff --git a/res/template/account/user_files.html b/res/template/account/user_files.html deleted file mode 100644 index 234cfcf..0000000 --- a/res/template/account/user_files.html +++ /dev/null @@ -1,68 +0,0 @@ -{{define "user_files"}} - - - {{template "meta_tags" "Files"}} - {{template "user_style" .}} - - - - - {{template "page_top" .}} -

My Files

-
-

- These files were uploaded while logged in to your pixeldrain account, - click here to view files uploaded anonymously - in this browser. -

-
- - {{$limit := 200}} - {{$page := .URLQuery.Get "page" | pageNr}} - {{$files := .PixelAPI.UserFiles $page $limit}} -
- {{if ne $page 0}} - 🡄 4 Pages - ⬅ 2 Pages - ← Last Page - {{end}} - Page {{$page}} - {{if len $files.Files | eq $limit}} - Next Page → - 2 Pages ⮕ - 4 Pages 🡆 - {{end}} -
-
- - {{range $files.Files}} - {{.Name}} - {{.Name}} -
- {{.DateUpload.Format "2006-01-02 15:04:05"}} -
{{end}} - -

-
- {{if ne $page 0}} - 🡄 4 Pages - ⬅ 2 Pages - ← Last Page - {{end}} - Page {{$page}} - {{if len $files.Files | eq $limit}} - Next Page → - 2 Pages ⮕ - 4 Pages 🡆 - {{end}} -
-
- - {{template "page_bottom" .}} - - {{template "analytics"}} - - -{{end}} diff --git a/res/template/account/user_lists.html b/res/template/account/user_lists.html deleted file mode 100644 index 04d66b3..0000000 --- a/res/template/account/user_lists.html +++ /dev/null @@ -1,60 +0,0 @@ -{{define "user_lists"}} - - - {{template "meta_tags" "Lists"}} - {{template "user_style" .}} - - - - - {{template "page_top" .}} -

My Lists

-
- {{$limit := 200}} - {{$page := .URLQuery.Get "page" | pageNr}} - {{$lists := .PixelAPI.UserLists $page $limit}} -
- {{if ne $page 0}} - 🡄 4 Pages - ⬅ 2 Pages - ← Last Page - {{end}} - Page {{$page}} - {{if len $lists.Lists | eq $limit}} - Next Page → - 2 Pages ⮕ - 4 Pages 🡆 - {{end}} -
-
- - {{range $lists.Lists}} - {{.Title}} - {{.Title}} - ({{.FileCount}} Files) -
- {{.DateCreated.Format "2006-01-02 15:04:05"}} -
{{end}} - -

-
- {{if ne $page 0}} - 🡄 4 Pages - ⬅ 2 Pages - ← Last Page - {{end}} - Page {{$page}} - {{if len $lists.Lists | eq $limit}} - Next Page → - 2 Pages ⮕ - 4 Pages 🡆 - {{end}} -
-
- {{template "page_bottom" .}} - {{template "analytics"}} - - -{{end}} diff --git a/res/template/account/user_settings.html b/res/template/account/user_settings.html deleted file mode 100644 index b21fee9..0000000 --- a/res/template/account/user_settings.html +++ /dev/null @@ -1,21 +0,0 @@ -{{define "user_settings"}} - - - {{template "meta_tags" .User.Username}} - {{template "user_style" .}} - - - - - {{template "page_top" .}} -

{{.Title}}

-
- {{template "form" .Other.PasswordForm}} - {{template "form" .Other.EmailForm}} - {{template "form" .Other.UsernameForm}} -
- {{template "page_bottom" .}} - {{template "analytics"}} - - -{{end}} diff --git a/res/template/admin.html b/res/template/admin.html index cd0677d..a6ef80c 100644 --- a/res/template/admin.html +++ b/res/template/admin.html @@ -10,8 +10,8 @@ - - + + {{template "page_top" .}} diff --git a/res/template/advertisements.html b/res/template/advertisements.html deleted file mode 100644 index f4b3386..0000000 --- a/res/template/advertisements.html +++ /dev/null @@ -1,161 +0,0 @@ -{{ define "ad_headers" }} - {{ if eq .Other.AdBannerType 3 }} - - - {{ end }} -{{ end }} - -{{ define "banner_ads"}} - {{ if eq .Other.AdBannerType 1 }} - - - - {{ else if eq .Other.AdBannerType 13 }} - - - - - {{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 }} - -
- -
- {{ else if eq .Other.AdBannerType 14 }} - - - - - - -
- -
- {{ else if eq .Other.AdBannerType 15 }} - - -
- - - {{ else if eq .Other.AdBannerType 16 }} - -
- - shopping_cart - Click here for online shopping discounts! - shopping_cart - -
- {{ else if eq .Other.AdBannerType 17 }} - -
- - shopping_cart - Check our online shopping discounts! - shopping_cart - -
- {{ else if eq .Other.AdBannerType 18 }} - -
- - shopping_cart - Free coupon codes for online shopping! - shopping_cart - -
- {{ end }} -{{ end }} diff --git a/res/template/account/email_confirm.html b/res/template/email_confirm.html similarity index 100% rename from res/template/account/email_confirm.html rename to res/template/email_confirm.html diff --git a/res/template/file_viewer_svelte.html b/res/template/file_viewer_svelte.html index c41d7aa..e8282e0 100644 --- a/res/template/file_viewer_svelte.html +++ b/res/template/file_viewer_svelte.html @@ -28,8 +28,8 @@ window.user_authenticated = {{.Authenticated}}; - - + + {{template "analytics"}} diff --git a/res/template/filesystem.html b/res/template/filesystem.html index 9b9b7f3..0a76002 100644 --- a/res/template/filesystem.html +++ b/res/template/filesystem.html @@ -25,8 +25,8 @@ window.api_endpoint = '{{.APIEndpoint}}'; - - + + diff --git a/res/template/home.html b/res/template/home.html index afb3ca2..001d56c 100644 --- a/res/template/home.html +++ b/res/template/home.html @@ -88,8 +88,8 @@ window.api_endpoint = '{{.APIEndpoint}}'; window.user = {{.User}}; - - + + {{template "page_top" .}} diff --git a/res/template/account/user_buckets.html b/res/template/user_buckets.html similarity index 72% rename from res/template/account/user_buckets.html rename to res/template/user_buckets.html index efbc134..659f68d 100644 --- a/res/template/account/user_buckets.html +++ b/res/template/user_buckets.html @@ -4,8 +4,8 @@ {{template "meta_tags" "Buckets"}} {{template "user_style" .}} - - + + {{template "page_top" .}} diff --git a/res/template/account/file_manager.html b/res/template/user_file_manager.html similarity index 81% rename from res/template/account/file_manager.html rename to res/template/user_file_manager.html index e8e9257..37dd660 100644 --- a/res/template/account/file_manager.html +++ b/res/template/user_file_manager.html @@ -7,8 +7,8 @@ - - + + diff --git a/res/template/account/user_home.html b/res/template/user_home.html similarity index 77% rename from res/template/account/user_home.html rename to res/template/user_home.html index 4f27642..66ebb83 100644 --- a/res/template/account/user_home.html +++ b/res/template/user_home.html @@ -9,8 +9,8 @@ window.api_endpoint = '{{.APIEndpoint}}'; window.user = {{.User}}; - - + + diff --git a/res/template/account/logout.html b/res/template/user_logout.html similarity index 100% rename from res/template/account/logout.html rename to res/template/user_logout.html diff --git a/svelte/src/admin_panel/Subscriptions.svelte b/svelte/src/admin_panel/Subscriptions.svelte index 8342213..2a72e35 100644 --- a/svelte/src/admin_panel/Subscriptions.svelte +++ b/svelte/src/admin_panel/Subscriptions.svelte @@ -34,16 +34,9 @@ let credit_form = { submit_label: `send Submit`, on_submit: async fields => { const form = new FormData() - if (fields.user_id !== "") { - form.append("user_by", "id") - form.append("id", fields.user_id) - } else if (fields.user_name !== "") { - form.append("user_by", "name") - form.append("name", fields.user_name) - } else if (fields.user_email !== "") { - form.append("user_by", "email") - form.append("email", fields.user_email) - } + form.append("id", fields.user_id) + form.append("name", fields.user_name) + form.append("email", fields.user_email) form.append("credit", fields.credit*1e6) const resp = await fetch( @@ -58,6 +51,47 @@ let credit_form = { }, } +let impersonate_form = { + name: "impersonate", + fields: [ + { + name: "user_id", + label: "User ID", + type: "text", + default_value: "", + }, { + name: "user_name", + label: "User name", + type: "text", + default_value: "", + }, { + name: "user_email", + label: "User e-mail", + type: "text", + default_value: "", + }, + ], + submit_label: `send Submit`, + on_submit: async fields => { + const form = new FormData() + form.append("id", fields.user_id) + form.append("name", fields.user_name) + form.append("email", fields.user_email) + + const resp = await fetch( + window.api_endpoint+"/admin/impersonate", + { method: "POST", body: form } + ); + if(resp.status >= 400) { + return {error_json: await resp.json()} + } + + window.location = "/user" + + return {success: true, message: "Success"} + }, +} + let coupon_form = { name: "make_coupon", fields: [ @@ -154,6 +188,11 @@ onMount(get_coupons)
+

Impersonate user

+
+
+
+

Create coupon codes

diff --git a/webcontroller/templates.go b/webcontroller/templates.go index 05e9c86..16a9b31 100644 --- a/webcontroller/templates.go +++ b/webcontroller/templates.go @@ -36,6 +36,9 @@ type TemplateData struct { Title string OGData ogData + // The cache ID is used to invalidate caches when the webserver is restarted + CacheID int64 + Other interface{} URLQuery url.Values @@ -54,6 +57,7 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) // Use the user's IP address for making requests PixelAPI: wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent()), + CacheID: wc.cacheID, Hostname: template.HTML(wc.hostname), URLQuery: r.URL.Query(), } diff --git a/webcontroller/web_controller.go b/webcontroller/web_controller.go index b33c807..f79470f 100644 --- a/webcontroller/web_controller.go +++ b/webcontroller/web_controller.go @@ -36,6 +36,9 @@ type WebController struct { // page-specific variables captchaSiteKey string + // The cache ID is used to invalidate caches when the webserver is restarted + cacheID int64 + httpClient *http.Client // API client to use for all requests. If the user is authenticated you @@ -66,6 +69,7 @@ func New( websiteAddress: websiteAddress, sessionCookieDomain: sessionCookieDomain, proxyAPIRequests: proxyAPIRequests, + cacheID: time.Now().Unix() / 3600, httpClient: &http.Client{Timeout: time.Minute * 10}, api: pixelapi.New(apiURLInternal), } @@ -78,14 +82,15 @@ func New( // Serve static files var fs = http.FileServer(http.Dir(resourceDir + "/static")) - r.GET(prefix+"/res/*filepath", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + var resourceHandler = func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Cache resources for a year - if !debugMode { - w.Header().Set("Cache-Control", "public, max-age=31536000") - } + w.Header().Set("Cache-Control", "public, max-age=31536000") r.URL.Path = p.ByName("filepath") fs.ServeHTTP(w, r) - }) + } + r.HEAD(prefix+"/res/*filepath", resourceHandler) + r.OPTIONS(prefix+"/res/*filepath", resourceHandler) + r.GET(prefix+"/res/*filepath", resourceHandler) // Static assets r.GET(prefix+"/favicon.ico" /* */, wc.serveFile("/favicon.ico")) @@ -166,8 +171,6 @@ func New( {PST, "password_reset" /* */, wc.serveForm(wc.passwordResetForm, handlerOpts{NoEmbed: true})}, {GET, "logout" /* */, wc.serveTemplate("logout", handlerOpts{Auth: true, NoEmbed: true})}, {PST, "logout" /* */, wc.serveLogout}, - {GET, "user/files" /* */, wc.serveTemplate("user_files", handlerOpts{Auth: true})}, - {GET, "user/lists" /* */, wc.serveTemplate("user_lists", handlerOpts{Auth: true})}, {GET, "user/buckets" /* */, wc.serveTemplate("user_buckets", handlerOpts{Auth: true})}, {GET, "user/filemanager" /* */, wc.serveTemplate("file_manager", handlerOpts{Auth: true})}, {GET, "user/export/files" /**/, wc.serveUserExportFiles},