add pocketbase
This commit is contained in:
82
backend/custom/cloudflare/TurnStilleCaptcha.js
Normal file
82
backend/custom/cloudflare/TurnStilleCaptcha.js
Normal file
@ -0,0 +1,82 @@
|
||||
export class TurnStilleCaptcha {
|
||||
sitekey = "{{.SiteKey}}";
|
||||
/**
|
||||
* Initializes the TurnStilleCaptcha instance.
|
||||
* Creates a container for the captcha and appends it to the target element.
|
||||
* If the Cloudflare Turnstile script is already loaded, it runs the captcha.
|
||||
* Otherwise, it loads the script and initializes the captcha with the given properties.
|
||||
* @param {HTMLElement} target - The element to attach the captcha container to.
|
||||
* @param {Object} [props={}] - Optional properties for captcha initialization, such as theme.
|
||||
*/
|
||||
|
||||
constructor(target, props = {}) {
|
||||
// create holder
|
||||
this.holder = document.createElement("div");
|
||||
this.holder.id = "turnstile-container";
|
||||
this.theme = props.theme || "auto";
|
||||
target.appendChild(this.holder);
|
||||
|
||||
|
||||
// execute code
|
||||
if (window.turnstile) {
|
||||
this.runCaptcha();
|
||||
} else {
|
||||
this.loadCloudflareScript();
|
||||
}
|
||||
}
|
||||
runCaptcha() {
|
||||
setTimeout(() => {
|
||||
if (globalThis.turnstileInstance) {
|
||||
window.turnstile.remove(globalThis.turnstileInstance);
|
||||
}
|
||||
globalThis.turnstileInstance = window.turnstile.render(this.holder, {
|
||||
sitekey: this.sitekey,
|
||||
theme: this.theme,
|
||||
callback: (token) => {
|
||||
if (token) {
|
||||
const event = new CustomEvent("token", {
|
||||
detail: token,
|
||||
bubbles: true,
|
||||
});
|
||||
this.holder.dispatchEvent(event);
|
||||
}
|
||||
},
|
||||
error: (error) => {
|
||||
const event = new CustomEvent("failure", {
|
||||
detail: error,
|
||||
bubbles: true,
|
||||
});
|
||||
this.holder.dispatchEvent(event);
|
||||
window.turnstile.reset(globalThis.turnstileInstance);
|
||||
},
|
||||
});
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
loadCloudflareScript() {
|
||||
const script = document.createElement("script");
|
||||
script.id = "turnstile-script";
|
||||
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
|
||||
// script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit";
|
||||
script.async = true;
|
||||
script.defer = true;
|
||||
|
||||
script.onload = () => {
|
||||
const event = new CustomEvent("loaded", {
|
||||
detail: "Turnstile script loaded",
|
||||
bubbles: true,
|
||||
});
|
||||
this.holder.dispatchEvent(event);
|
||||
|
||||
this.runCaptcha();
|
||||
};
|
||||
script.onerror = () => {
|
||||
const event = new CustomEvent("failure", {
|
||||
detail: "Failed to load Turnstile script",
|
||||
bubbles: true,
|
||||
});
|
||||
this.holder.dispatchEvent(event);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
}
|
95
backend/custom/cloudflare/cloudflare.go
Normal file
95
backend/custom/cloudflare/cloudflare.go
Normal file
@ -0,0 +1,95 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
var VerifyUrl = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
|
||||
|
||||
type TurnstileResponse struct {
|
||||
Success bool `json:"success"`
|
||||
ErrorCodes []string `json:"error-codes,omitempty"`
|
||||
ChallengeTS string `json:"challenge_ts,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
}
|
||||
|
||||
type TurnStilleCaptcha struct {
|
||||
SiteKey string
|
||||
SecretKey string
|
||||
}
|
||||
|
||||
func ServeTurnstilleCaptchaJS(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/api/email/script.js", func(e *core.RequestEvent) error {
|
||||
// www.abrasive.ma-al.pl
|
||||
// siteKey: "0x4AAAAAABdgeAdu4Pxxovj3"
|
||||
// secretKey: "0x4AAAAAABdgeHJDjMwmeX5aXaXGh6HWZbw"
|
||||
|
||||
settings, err := GetSettings(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := JS.ReadFile("TurnStilleCaptcha.js")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
templ, err := template.New("test").Parse(string(file))
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
templ.Execute(&buf, map[string]interface{}{
|
||||
"SiteKey": settings.SiteKey,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Response.Header().Set("Content-Type", "application/javascript")
|
||||
return e.String(http.StatusOK, buf.String())
|
||||
})
|
||||
}
|
||||
|
||||
func GetSettings(app *pocketbase.PocketBase) (*TurnStilleCaptcha, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='turnstile'", nil)
|
||||
|
||||
settings := TurnStilleCaptcha{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
func VerifyTurnstile(app *pocketbase.PocketBase, token, ip string) error {
|
||||
conf, err := GetSettings(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := map[string]string{"secret": conf.SecretKey, "response": token, "remoteip": ip}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
resp, err := http.Post(VerifyUrl, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var turnstileResp TurnstileResponse
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
json.Unmarshal(body, &turnstileResp)
|
||||
|
||||
if !turnstileResp.Success {
|
||||
return errors.New(turnstileResp.ChallengeTS + ": " + strings.Join(turnstileResp.ErrorCodes, " "))
|
||||
}
|
||||
return nil
|
||||
}
|
8
backend/custom/cloudflare/js.go
Normal file
8
backend/custom/cloudflare/js.go
Normal file
@ -0,0 +1,8 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed TurnStilleCaptcha.js
|
||||
var JS embed.FS
|
61
backend/custom/custom.go
Normal file
61
backend/custom/custom.go
Normal file
@ -0,0 +1,61 @@
|
||||
package custom
|
||||
|
||||
import (
|
||||
"pocketbase/custom/cloudflare"
|
||||
"pocketbase/custom/gtm"
|
||||
"pocketbase/custom/mail"
|
||||
"pocketbase/custom/manifest"
|
||||
"pocketbase/custom/proxy"
|
||||
"pocketbase/custom/seo"
|
||||
"pocketbase/custom/supervise"
|
||||
"pocketbase/custom/version"
|
||||
webpconverter "pocketbase/custom/webpConverter"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func LoadCustomCode(app *pocketbase.PocketBase, se *core.ServeEvent) error {
|
||||
|
||||
// include supervised subprocess if command provided
|
||||
supervise.ServeSubprocessSupervisor(app, se)
|
||||
|
||||
// include serving js code from cloudflare
|
||||
cloudflare.ServeTurnstilleCaptchaJS(app, se)
|
||||
|
||||
// include sending emails service
|
||||
mail.ServeMailSender(app, se)
|
||||
|
||||
// include robots.txt endpoint
|
||||
seo.ServeRobotsTxt(app, se)
|
||||
|
||||
// include feeds endpoint
|
||||
seo.ServeFeeds(app, se)
|
||||
seo.ServeFeedsIndex(app, se)
|
||||
|
||||
// include proxy to serve nuxt app
|
||||
proxy.ServeProxyPassingToNuxt(app, se)
|
||||
|
||||
// create endpoint to serve version information
|
||||
version.ServeVersionInfo(app, se)
|
||||
|
||||
// include endpoint to server GTM script
|
||||
gtm.ServeTagMangerJS(app, se)
|
||||
|
||||
// include endpoint serving manifest
|
||||
manifest.ServeManifst(app, se)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExtendApp(app *pocketbase.PocketBase) *pocketbase.PocketBase {
|
||||
app.RootCmd.PersistentFlags().String("proxy", "", "inner proxy target")
|
||||
app.RootCmd.PersistentFlags().String("subcommand", "", "provide command with params like cli that will be executed and supervised by main process")
|
||||
|
||||
// include webp converter
|
||||
app.OnRecordCreate().Bind(webpconverter.CreateEventHandler(app))
|
||||
app.OnRecordUpdate().Bind(webpconverter.CreateEventHandler(app))
|
||||
app.OnFileDownloadRequest().Bind(webpconverter.ThumbEventHandler(app))
|
||||
|
||||
return app
|
||||
}
|
56
backend/custom/gtm/gtm.go
Normal file
56
backend/custom/gtm/gtm.go
Normal file
@ -0,0 +1,56 @@
|
||||
package gtm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
func ServeTagMangerJS(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/api/gtm/script.js", func(e *core.RequestEvent) error {
|
||||
|
||||
settings, err := GetSettings(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := JS.ReadFile("gtm.js")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
templ, err := template.New("test").Parse(string(file))
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
templ.Execute(&buf, map[string]interface{}{
|
||||
"GtagID": settings.GtagID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Response.Header().Set("Content-Type", "application/javascript")
|
||||
return e.String(http.StatusOK, buf.String())
|
||||
})
|
||||
}
|
||||
|
||||
type GTagSettings struct {
|
||||
GtagID string `json:"gtagID"`
|
||||
}
|
||||
|
||||
func GetSettings(app *pocketbase.PocketBase) (*GTagSettings, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='gtagID'", nil)
|
||||
|
||||
settings := GTagSettings{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
29
backend/custom/gtm/gtm.js
Normal file
29
backend/custom/gtm/gtm.js
Normal file
@ -0,0 +1,29 @@
|
||||
export class GTM {
|
||||
gtagID = "{{.GtagID}}";
|
||||
|
||||
constructor() {
|
||||
this.insertScript(window, document, 'script', 'dataLayer', this.gtagID);
|
||||
this.insertNoScript();
|
||||
}
|
||||
|
||||
|
||||
insertScript(w, d, s, l, i) {
|
||||
w[l] = w[l] || []; w[l].push({
|
||||
'gtm.start':
|
||||
new Date().getTime(), event: 'gtm.js'
|
||||
}); var f = d.getElementsByTagName(s)[0],
|
||||
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
|
||||
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
|
||||
}
|
||||
|
||||
insertNoScript() {
|
||||
const noscript = document.createElement("noscript");
|
||||
const iframe = document.createElement("iframe");
|
||||
iframe.src = "https://www.googletagmanager.com/ns.html?id=" + this.gtagID;
|
||||
iframe.height = 0;
|
||||
iframe.width = 0;
|
||||
iframe.style = "display:none;visibility:hidden";
|
||||
noscript.appendChild(iframe);
|
||||
document.body.appendChild(noscript);
|
||||
}
|
||||
}
|
8
backend/custom/gtm/js.go
Normal file
8
backend/custom/gtm/js.go
Normal file
@ -0,0 +1,8 @@
|
||||
package gtm
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed gtm.js
|
||||
var JS embed.FS
|
175
backend/custom/mail/mail.go
Normal file
175
backend/custom/mail/mail.go
Normal file
@ -0,0 +1,175 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"pocketbase/custom/cloudflare"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/mailer"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
type EmailData struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Token string `json:"token"`
|
||||
Message string `json:"message"`
|
||||
Phone string `json:"phone"`
|
||||
LangIso string `json:"lang_iso"`
|
||||
}
|
||||
|
||||
type MailsSettings struct {
|
||||
ReceiverMail string `json:"receiver_mail"`
|
||||
ReceiverName string `json:"receiver_name"`
|
||||
Subject string `json:"subject"`
|
||||
}
|
||||
|
||||
func ServeMailSender(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.POST("/api/email/send", func(e *core.RequestEvent) error {
|
||||
// name := e.Request.PathValue("name")
|
||||
|
||||
data := EmailData{}
|
||||
if err := e.BindBody(&data); err != nil {
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
app.Logger().Error(err.Error(), "type", "mail")
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
if pass, err := validateMX(data.Email); !pass && err != nil {
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
app.Logger().Error("Invalid email address.", "type", "mail", "error", err.Error())
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid email address."})
|
||||
}
|
||||
|
||||
// fmt.Printf("e.Request.Body: %v IP: %s\n", data, e.RealIP())
|
||||
|
||||
err := cloudflare.VerifyTurnstile(app, data.Token, e.RealIP())
|
||||
if err != nil {
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
app.Logger().Error("Captcha verification failed.", "type", "mail", "error", err.Error())
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Captcha verification failed."})
|
||||
}
|
||||
|
||||
eamil_body, err := app.FindFirstRecordByFilter("email_template", fmt.Sprintf("name='contact_form'&&id_lang='%s'", data.LangIso), nil)
|
||||
if err != nil {
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
app.Logger().Error("Template not available", "type", "mail", "error", err.Error())
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Template not available"})
|
||||
}
|
||||
templ, err := template.New("test").Parse(eamil_body.GetString("template"))
|
||||
if err != nil {
|
||||
app.Logger().Error("Template parsing error.", "type", "mail", "error", err.Error())
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Template parsing error."})
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
templ.Execute(&buf, map[string]interface{}{
|
||||
"Data": time.Now().Local().Format("2006-01-02 15:04:05"),
|
||||
"Name": data.Name,
|
||||
"Email": data.Email,
|
||||
"Message": strings.ReplaceAll(data.Message, "\n", "<br>"),
|
||||
"Phone": data.Phone,
|
||||
})
|
||||
|
||||
mailsSettings, err := getSettings(app)
|
||||
if err != nil {
|
||||
app.Logger().Error("Mails settings corrupted", "type", "mail", "error", err.Error())
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Mails settings corrupted"})
|
||||
}
|
||||
|
||||
cc := []mail.Address{{Address: data.Email, Name: data.Name}}
|
||||
to := []mail.Address{{Address: mailsSettings.ReceiverMail, Name: mailsSettings.ReceiverName}}
|
||||
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Address: e.App.Settings().Meta.SenderAddress,
|
||||
Name: e.App.Settings().Meta.SenderName,
|
||||
},
|
||||
Cc: cc,
|
||||
To: to,
|
||||
Subject: mailsSettings.Subject,
|
||||
HTML: buf.String(),
|
||||
}
|
||||
|
||||
err = e.App.NewMailClient().Send(message)
|
||||
if err != nil {
|
||||
app.Logger().Error("Mails sending error", "type", "mail", "error", err.Error())
|
||||
e.Response.Header().Set("Content-Type", "application/json")
|
||||
return e.JSON(http.StatusBadRequest, map[string]string{"error": "Mails sending error"})
|
||||
}
|
||||
|
||||
receivers := formReceiversList(to, cc, []mail.Address{})
|
||||
app.Logger().Info("Mail Sent", "type", "mail", "receivers", receivers)
|
||||
return e.JSON(http.StatusOK, map[string]string{"status": "success", "message": "Email sent successfully. to " + receivers})
|
||||
})
|
||||
}
|
||||
|
||||
func formReceiversList(to []mail.Address, cc []mail.Address, bcc []mail.Address) string {
|
||||
|
||||
res := ""
|
||||
|
||||
for _, a := range to {
|
||||
res = fmt.Sprintf("%s %s<%s>", res, a.Name, a.Address)
|
||||
}
|
||||
|
||||
for _, a := range cc {
|
||||
res = fmt.Sprintf("%s %s<%s>", res, a.Name, a.Address)
|
||||
}
|
||||
|
||||
for _, a := range bcc {
|
||||
res = fmt.Sprintf("%s %s<%s>", res, a.Name, a.Address)
|
||||
}
|
||||
return res
|
||||
}
|
||||
func getSettings(app *pocketbase.PocketBase) (*MailsSettings, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='contact_page'", nil)
|
||||
|
||||
settings := MailsSettings{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
func extractDomain(email string) string {
|
||||
parts := strings.Split(email, "@")
|
||||
if len(parts) != 2 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(parts[1])
|
||||
}
|
||||
|
||||
// validateMX checks if the domain has valid MX records or A records as a fallback
|
||||
func validateMX(email string) (bool, error) {
|
||||
// Check MX records
|
||||
|
||||
domain := extractDomain(email)
|
||||
mxRecords, err := net.LookupMX(domain)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("'MX' records for %s not found", domain)
|
||||
} else if len(mxRecords) > 0 {
|
||||
// At least one MX record exists
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Fallback: Check for A records (some domains accept mail via A records)
|
||||
aRecords, err := net.LookupHost(domain)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("'A' record for %s not found", domain)
|
||||
}
|
||||
return len(aRecords) > 0, nil
|
||||
}
|
50
backend/custom/manifest/manifest.go
Normal file
50
backend/custom/manifest/manifest.go
Normal file
@ -0,0 +1,50 @@
|
||||
package manifest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
type Manifest struct {
|
||||
Name string `json:"name"`
|
||||
ShortName string `json:"short_name"`
|
||||
Description string `json:"description"`
|
||||
Icons []Icon `json:"icons"`
|
||||
StartURL string `json:"start_url"`
|
||||
Display string `json:"display"`
|
||||
BackgroundColor string `json:"background_color"`
|
||||
ThemeColor string `json:"theme_color"`
|
||||
Lang string `json:"lang"`
|
||||
Author string `json:"author"`
|
||||
OgHost string `json:"ogHost"`
|
||||
Orientation string `json:"orientation"`
|
||||
}
|
||||
|
||||
type Icon struct {
|
||||
Src string `json:"src"`
|
||||
Sizes string `json:"sizes"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func ServeManifst(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/api/manifest.json", func(e *core.RequestEvent) error {
|
||||
manifest, err := GetSettings(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.Response.Header().Add("content-type", "application/json")
|
||||
return e.String(http.StatusOK, manifest)
|
||||
})
|
||||
}
|
||||
|
||||
func GetSettings(app *pocketbase.PocketBase) (string, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='manifest'", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return record.GetString("value"), nil
|
||||
}
|
121
backend/custom/proxy/proxy.go
Normal file
121
backend/custom/proxy/proxy.go
Normal file
@ -0,0 +1,121 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
var Logger *slog.Logger
|
||||
|
||||
func ServeProxyPassingToNuxt(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
Logger = app.Logger()
|
||||
|
||||
proxyUrl, _ := app.RootCmd.Flags().GetString("proxy")
|
||||
|
||||
if len(proxyUrl) > 0 {
|
||||
target, _ := url.Parse(proxyUrl) // Node.js app
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
|
||||
originalDirector := proxy.Director
|
||||
proxy.Director = func(req *http.Request) {
|
||||
originalDirector(req)
|
||||
req.Host = target.Host
|
||||
}
|
||||
proxy.Transport = &loggingTransport{http.DefaultTransport}
|
||||
|
||||
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return
|
||||
}
|
||||
app.Logger().Error(fmt.Sprintf("Proxy error: %v for %s %s", err, req.Method, req.URL.Path), "type", "proxy")
|
||||
http.Error(rw, "Proxy error", http.StatusBadGateway)
|
||||
}
|
||||
|
||||
return se.Router.Any("/", func(e *core.RequestEvent) error {
|
||||
// Ping the backend with a HEAD request (or TCP dial)
|
||||
backendUp := isBackendAlive(target)
|
||||
if !backendUp {
|
||||
app.Logger().Error(fmt.Sprintf("Backend %s is unreachable, sending 502", target), "type", "proxy", "path", e.Request.URL.Path, "remoteIP", e.Request.RemoteAddr)
|
||||
e.Response.WriteHeader(http.StatusBadGateway)
|
||||
e.Response.Write([]byte("502 Backend is unavailable"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Forward to Node.js
|
||||
proxy.ServeHTTP(e.Response, e.Request)
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func isBackendAlive(target *url.URL) bool {
|
||||
conn, err := net.DialTimeout("tcp", target.Host, 500*time.Millisecond)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
conn.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
type loggingTransport struct {
|
||||
rt http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
start := time.Now()
|
||||
|
||||
// Do the actual request
|
||||
resp, err := t.rt.RoundTrip(req)
|
||||
duration := time.Since(start)
|
||||
|
||||
// Prepare fields
|
||||
remoteAddr := req.RemoteAddr
|
||||
if ip := req.Header.Get("X-Real-IP"); len(ip) > 0 {
|
||||
remoteAddr = ip
|
||||
}
|
||||
method := req.Method
|
||||
uri := req.URL.RequestURI()
|
||||
// proto := req.Proto
|
||||
status := 0
|
||||
size := 0
|
||||
|
||||
if resp != nil {
|
||||
status = resp.StatusCode
|
||||
if resp.ContentLength > 0 {
|
||||
size = int(resp.ContentLength)
|
||||
}
|
||||
}
|
||||
|
||||
referer := req.Referer()
|
||||
ua := req.UserAgent()
|
||||
// timestamp := time.Now().Format("02/Jan/2006:15:04:05 -0700")
|
||||
|
||||
Logger.Info(
|
||||
fmt.Sprintf("%s %s", method, uri),
|
||||
"type", "proxy",
|
||||
// "method", method,
|
||||
// "url", uri,
|
||||
"referer", referer,
|
||||
"remoteIP", remoteAddr,
|
||||
"userAgent", ua,
|
||||
"execTime", fmt.Sprintf("%.4fms", float64(duration.Milliseconds())),
|
||||
"status", status,
|
||||
"size", size,
|
||||
)
|
||||
|
||||
return resp, err
|
||||
}
|
194
backend/custom/seo/feeds.go
Normal file
194
backend/custom/seo/feeds.go
Normal file
@ -0,0 +1,194 @@
|
||||
package seo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
type MenuRecord struct {
|
||||
Active bool `json:"active"`
|
||||
CollectionID string `json:"collectionId"`
|
||||
CollectionName string `json:"collectionName"`
|
||||
Created string `json:"created"`
|
||||
ID string `json:"id"`
|
||||
IDLang string `json:"id_lang"`
|
||||
IDPage string `json:"id_page"`
|
||||
IDParent string `json:"id_parent"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
IsRoot bool `json:"is_root"`
|
||||
LinkRewrite string `json:"link_rewrite"`
|
||||
LinkTitle string `json:"link_title"`
|
||||
MetaDescription string `json:"meta_description"`
|
||||
MetaTitle string `json:"meta_title"`
|
||||
Name string `json:"name"`
|
||||
PageName string `json:"page_name"`
|
||||
PositionID int `json:"position_id"`
|
||||
Updated string `json:"updated"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type UrlSet struct {
|
||||
XMLName xml.Name `xml:"urlset"`
|
||||
Xmlns string `xml:"xmlns,attr"`
|
||||
Urls []Url `xml:"url"`
|
||||
}
|
||||
|
||||
type Url struct {
|
||||
Loc string `xml:"loc"`
|
||||
LastMod string `xml:"lastmod,omitempty"`
|
||||
ChangeFreq string `xml:"changefreq,omitempty"`
|
||||
Priority string `xml:"priority,omitempty"`
|
||||
}
|
||||
|
||||
func ServeFeeds(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/feeds/{lang}/sitemap.xml", func(e *core.RequestEvent) error {
|
||||
|
||||
lang := e.Request.PathValue("lang")
|
||||
|
||||
urls, err := getLocations(app, lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
xx := UrlSet{
|
||||
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
|
||||
Urls: urls,
|
||||
}
|
||||
|
||||
bytes, err := xml.MarshalIndent(xx, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Response.Header().Add("content-type", "text/xml")
|
||||
return e.String(http.StatusOK, xml.Header+string(bytes))
|
||||
})
|
||||
}
|
||||
|
||||
func getLocations(app *pocketbase.PocketBase, lang string) ([]Url, error) {
|
||||
records, err := app.FindRecordsByFilter("menu_view", fmt.Sprintf("id_lang='%s'&&active=true", lang), "position_id", 200, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
baseUrl, err := getBaseUrl(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
locations := []Url{}
|
||||
lastMod := time.Now().Add(time.Hour * 24 * 7 * -1).Format("2006-01-02")
|
||||
|
||||
for _, r := range records {
|
||||
rec := MenuRecord{}
|
||||
x, _ := r.MarshalJSON()
|
||||
json.Unmarshal(x, &rec)
|
||||
if rec.IsRoot {
|
||||
continue
|
||||
}
|
||||
if len(rec.Url) > 0 {
|
||||
continue
|
||||
}
|
||||
if rec.IsDefault {
|
||||
|
||||
locations = append(locations, Url{
|
||||
Loc: baseUrl + path.Join(lang),
|
||||
LastMod: lastMod,
|
||||
ChangeFreq: "weekly",
|
||||
Priority: "1.0",
|
||||
})
|
||||
}
|
||||
locations = append(locations, Url{
|
||||
Loc: baseUrl + path.Join(lang, rec.IDPage, rec.LinkRewrite),
|
||||
LastMod: lastMod,
|
||||
ChangeFreq: "weekly",
|
||||
Priority: "1.0",
|
||||
})
|
||||
}
|
||||
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
type BaseUrl struct {
|
||||
BaseURL string `json:"baseUrl"`
|
||||
}
|
||||
|
||||
func getBaseUrl(app *pocketbase.PocketBase) (string, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='baseUrl'", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
settings := BaseUrl{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return settings.BaseURL, nil
|
||||
}
|
||||
|
||||
type SitemapIndex struct {
|
||||
XMLName xml.Name `xml:"sitemapindex"`
|
||||
Xmlns string `xml:"xmlns,attr"`
|
||||
Sitemaps []Sitemap `xml:"sitemap"`
|
||||
}
|
||||
|
||||
// Sitemap represents each <sitemap> entry
|
||||
type Sitemap struct {
|
||||
Loc string `xml:"loc"`
|
||||
LastMod string `xml:"lastmod"`
|
||||
}
|
||||
|
||||
func ServeFeedsIndex(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/feeds/index.xml", func(e *core.RequestEvent) error {
|
||||
index, err := makeSiteMapIndex(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bytes, err := xml.MarshalIndent(index, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.Response.Header().Add("content-type", "text/xml")
|
||||
return e.String(http.StatusOK, xml.Header+string(bytes))
|
||||
})
|
||||
}
|
||||
|
||||
func makeSiteMapIndex(app *pocketbase.PocketBase) (*SitemapIndex, error) {
|
||||
index := &SitemapIndex{
|
||||
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
|
||||
}
|
||||
|
||||
bseUrl, err := getBaseUrl(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
langs, err := app.FindRecordsByFilter("lang", "active=true", "", 200, 0)
|
||||
if err != nil {
|
||||
return index, err
|
||||
}
|
||||
|
||||
lastMod := time.Now().Add(time.Hour * 24 * 7 * -1).Format("2006-01-02")
|
||||
|
||||
for _, l := range langs {
|
||||
index.Sitemaps = append(index.Sitemaps, Sitemap{
|
||||
Loc: bseUrl + path.Join("feeds/", l.Id, "sitemap.xml"),
|
||||
LastMod: lastMod,
|
||||
})
|
||||
}
|
||||
|
||||
return index, nil
|
||||
}
|
50
backend/custom/seo/robots.go
Normal file
50
backend/custom/seo/robots.go
Normal file
@ -0,0 +1,50 @@
|
||||
package seo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
type Robots struct {
|
||||
Robots []string
|
||||
}
|
||||
|
||||
func ServeRobotsTxt(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/robots.txt", func(e *core.RequestEvent) error {
|
||||
|
||||
text, err := getRobots(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return e.String(http.StatusOK, text)
|
||||
})
|
||||
}
|
||||
|
||||
func getRobots(app *pocketbase.PocketBase) (string, error) {
|
||||
record, err := app.FindFirstRecordByFilter("settings", "key='robots_txt'", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
settings := Robots{}
|
||||
json.Unmarshal([]byte(record.GetString("value")), &settings)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
baseUrl, err := getBaseUrl(app)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
settings.Robots = append(settings.Robots, fmt.Sprintf("\n\nSitemap: %s%s", baseUrl, "feeds/index.xml"))
|
||||
|
||||
return strings.Join(settings.Robots, "\n"), nil
|
||||
}
|
88
backend/custom/supervise/supervise.go
Normal file
88
backend/custom/supervise/supervise.go
Normal file
@ -0,0 +1,88 @@
|
||||
package supervise
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
func ServeSubprocessSupervisor(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
|
||||
command, _ := app.RootCmd.PersistentFlags().GetString("subcommand")
|
||||
|
||||
if len(command) > 0 {
|
||||
cmdsub := strings.Split(command, " ")
|
||||
if len(cmdsub) > 0 {
|
||||
startNodeProcessSupervised(cmdsub[0], strings.Join(cmdsub[1:], " "))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func startNodeProcessSupervised(command string, args ...string) {
|
||||
const maxRetries = 3
|
||||
const retryDelay = 30 * time.Second
|
||||
|
||||
var (
|
||||
cmdMu sync.Mutex
|
||||
cmd *exec.Cmd
|
||||
)
|
||||
|
||||
stopChan := make(chan os.Signal, 1)
|
||||
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
retries := 0
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-stopChan:
|
||||
log.Println("Received shutdown signal. Terminating subprocess...")
|
||||
cmdMu.Lock()
|
||||
if cmd != nil && cmd.Process != nil {
|
||||
_ = cmd.Process.Signal(syscall.SIGTERM)
|
||||
}
|
||||
cmdMu.Unlock()
|
||||
return
|
||||
|
||||
default:
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cmdMu.Lock()
|
||||
cmd = exec.CommandContext(ctx, command, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmdMu.Unlock()
|
||||
|
||||
log.Printf("Starting Process: %s %v\n", command, args)
|
||||
err := cmd.Run()
|
||||
cancel() // cancel the context when done
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Process exited with error: %v\n", err)
|
||||
retries++
|
||||
if retries >= maxRetries {
|
||||
log.Printf("Process failed %d times. Shutting down application...", retries)
|
||||
// _ = app.ResetBootstrapState()
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Retrying in %s (%d/%d)...", retryDelay, retries, maxRetries)
|
||||
time.Sleep(retryDelay)
|
||||
} else {
|
||||
log.Printf("Process exited normally. Resetting retry count.")
|
||||
retries = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
26
backend/custom/version/version.go
Normal file
26
backend/custom/version/version.go
Normal file
@ -0,0 +1,26 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
var Version string
|
||||
var BuildDate string
|
||||
var Company string
|
||||
var CompanyUrl string
|
||||
|
||||
func ServeVersionInfo(app *pocketbase.PocketBase, se *core.ServeEvent) *router.Route[*core.RequestEvent] {
|
||||
return se.Router.GET("/api/version/send", func(e *core.RequestEvent) error {
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]string{
|
||||
"version": Version,
|
||||
"build_date": BuildDate,
|
||||
"company": Company,
|
||||
"company_url": CompanyUrl,
|
||||
})
|
||||
})
|
||||
}
|
14
backend/custom/webpConverter/memFileReader.go
Normal file
14
backend/custom/webpConverter/memFileReader.go
Normal file
@ -0,0 +1,14 @@
|
||||
package webpconverter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
type memFileReader struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (m *memFileReader) Open() (io.ReadSeekCloser, error) {
|
||||
return readSeekCloser{bytes.NewReader(m.data)}, nil
|
||||
}
|
9
backend/custom/webpConverter/readSeekCloser.go
Normal file
9
backend/custom/webpConverter/readSeekCloser.go
Normal file
@ -0,0 +1,9 @@
|
||||
package webpconverter
|
||||
|
||||
import "bytes"
|
||||
|
||||
type readSeekCloser struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (r readSeekCloser) Close() error { return nil }
|
144
backend/custom/webpConverter/webp_convert.go
Normal file
144
backend/custom/webpConverter/webp_convert.go
Normal file
@ -0,0 +1,144 @@
|
||||
package webpconverter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/filesystem"
|
||||
"github.com/pocketbase/pocketbase/tools/hook"
|
||||
)
|
||||
|
||||
func CreateEventHandler(app *pocketbase.PocketBase) *hook.Handler[*core.RecordEvent] {
|
||||
return &hook.Handler[*core.RecordEvent]{
|
||||
Func: func(e *core.RecordEvent) error {
|
||||
fieldsData := e.Record.FieldsData()
|
||||
for _, v := range fieldsData {
|
||||
if files, ok := v.([]interface{}); ok {
|
||||
for _, f := range files {
|
||||
if file, ok := f.(*filesystem.File); ok {
|
||||
if shouldBeReplacedWithWebp(file.Name) {
|
||||
convertToWebp(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if f, ok := v.(interface{}); ok {
|
||||
if file, ok := f.(*filesystem.File); ok {
|
||||
if shouldBeReplacedWithWebp(file.Name) {
|
||||
convertToWebp(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.Next()
|
||||
},
|
||||
Priority: 100,
|
||||
}
|
||||
}
|
||||
|
||||
func ThumbEventHandler(app *pocketbase.PocketBase) *hook.Handler[*core.FileDownloadRequestEvent] {
|
||||
return &hook.Handler[*core.FileDownloadRequestEvent]{
|
||||
Func: func(fdre *core.FileDownloadRequestEvent) error {
|
||||
if filepath.Ext(fdre.ServedName) == ".webp" {
|
||||
|
||||
filename := fdre.Request.PathValue("filename")
|
||||
baseFilesPath := fdre.Record.BaseFilesPath()
|
||||
|
||||
fs, err := app.App.NewFilesystem()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fs.Close()
|
||||
|
||||
blob, err := fs.GetReader(baseFilesPath + "/thumbs_" + filename + "/" + fdre.ServedName)
|
||||
if err != nil {
|
||||
blob, err = fs.GetReader(baseFilesPath + "/" + fdre.ServedName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
defer blob.Close()
|
||||
|
||||
img, err := png.Decode(blob)
|
||||
if err != nil {
|
||||
return fdre.Next()
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
err = webp.Encode(&buf, img, &webp.Options{Quality: 80})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = fs.Upload(buf.Bytes(), baseFilesPath+"/thumbs_"+filename+"/"+fdre.ServedName)
|
||||
if err != nil {
|
||||
_, err := fdre.Response.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fdre.Next()
|
||||
}
|
||||
return fdre.Next()
|
||||
},
|
||||
Priority: 99,
|
||||
}
|
||||
}
|
||||
|
||||
func convertToWebp(file *filesystem.File) error {
|
||||
file.Name = replaceExtWithWebp(file.Name)
|
||||
file.OriginalName = replaceExtWithWebp(file.OriginalName)
|
||||
ff, err := file.Reader.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ff.Close()
|
||||
fff, err := io.ReadAll(ff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
img, _, err := image.Decode(bytes.NewReader(fff))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
err = webp.Encode(&buf, img, &webp.Options{Quality: 80})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Reader = &memFileReader{data: buf.Bytes()}
|
||||
file.Size = int64(buf.Len())
|
||||
return nil
|
||||
}
|
||||
|
||||
func replaceExtWithWebp(path string) string {
|
||||
// List of image extensions to convert
|
||||
exts := []string{".png", ".jpg", ".jpeg", ".bmp", ".tiff"}
|
||||
|
||||
for _, ext := range exts {
|
||||
if strings.HasSuffix(strings.ToLower(path), ext) {
|
||||
return strings.TrimSuffix(path, ext) + ".webp"
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func shouldBeReplacedWithWebp(path string) bool {
|
||||
// List of image extensions to convert
|
||||
exts := []string{".png", ".jpg", ".jpeg", ".bmp", ".tiff"}
|
||||
|
||||
for _, ext := range exts {
|
||||
if strings.HasSuffix(strings.ToLower(path), ext) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user