From 86220513cce45704ab98340e905c2cd1f83fc921 Mon Sep 17 00:00:00 2001 From: yimi Date: Tue, 17 Oct 2023 16:44:10 +0800 Subject: [PATCH] opt img copy in pan mode --- assets/assets.go | 8 ++ assets/templates/files.tmpl | 139 ++++++++++++++++++++++++++++++++ assets/templates/header.tmpl | 151 +++++++++++++++++++++++++++++++++++ assets/templates/images.tmpl | 22 +++++ assets/templates/pwd.tmpl | 2 + control/control.go | 99 +++++++++++++++++------ main.go | 4 +- 7 files changed, 398 insertions(+), 27 deletions(-) create mode 100644 assets/assets.go create mode 100644 assets/templates/files.tmpl create mode 100644 assets/templates/header.tmpl create mode 100644 assets/templates/images.tmpl create mode 100644 assets/templates/pwd.tmpl diff --git a/assets/assets.go b/assets/assets.go new file mode 100644 index 0000000..7a20b2b --- /dev/null +++ b/assets/assets.go @@ -0,0 +1,8 @@ +package assets + +import "embed" + +var ( + //go:embed templates + Templates embed.FS +) diff --git a/assets/templates/files.tmpl b/assets/templates/files.tmpl new file mode 100644 index 0000000..813ef3b --- /dev/null +++ b/assets/templates/files.tmpl @@ -0,0 +1,139 @@ +{{template "public/header" .}} + +

上传文件到 Telegram

+
上传中...
+
+ + + + + + + \ No newline at end of file diff --git a/assets/templates/header.tmpl b/assets/templates/header.tmpl new file mode 100644 index 0000000..d2ab011 --- /dev/null +++ b/assets/templates/header.tmpl @@ -0,0 +1,151 @@ +{{define "public/header"}} + + + + + tgState + + + + + + +{{end}} \ No newline at end of file diff --git a/assets/templates/images.tmpl b/assets/templates/images.tmpl new file mode 100644 index 0000000..591fc02 --- /dev/null +++ b/assets/templates/images.tmpl @@ -0,0 +1,22 @@ +{{template "public/header" .}} + +

上传图片到 Telegram

+
上传中...
+
+ + + + + \ No newline at end of file diff --git a/assets/templates/pwd.tmpl b/assets/templates/pwd.tmpl new file mode 100644 index 0000000..c1e58d7 --- /dev/null +++ b/assets/templates/pwd.tmpl @@ -0,0 +1,2 @@ +{{template "public/header" .}} +

Powered by tgState

\ No newline at end of file diff --git a/control/control.go b/control/control.go index 820da9f..5f8f81d 100644 --- a/control/control.go +++ b/control/control.go @@ -2,12 +2,14 @@ package control import ( "encoding/json" + "html/template" "io" "log" "net/http" "path/filepath" "strings" + "csz.net/tgstate/assets" "csz.net/tgstate/conf" "csz.net/tgstate/utils" ) @@ -147,40 +149,85 @@ func D(w http.ResponseWriter, r *http.Request) { } } -const htmlHead string = `tgState` - // 首页 func Index(w http.ResponseWriter, r *http.Request) { - // 如果不是 POST 请求,显示上传图片的 HTML 表单 - body := `

上传图片到 Telegram

上传中...
` + htmlPath := "templates/images.tmpl" if conf.Mode == "pan" { - body = `

上传文件到 Telegram

上传中...
` - } - htmlForm := htmlHead + body - // 输出 HTML 表单 - io.WriteString(w, htmlForm) + htmlPath = "templates/files.tmpl" + } + file, err := assets.Templates.ReadFile(htmlPath) + if err != nil { + http.Error(w, "HTML file not found", http.StatusNotFound) + return + } + // 读取头部模板 + headerPath := "templates/header.tmpl" + headerFile, err := assets.Templates.ReadFile(headerPath) + if err != nil { + http.Error(w, "Header template not found", http.StatusNotFound) + return + } + + // 创建HTML模板并包括头部 + tmpl := template.New("html") + tmpl, err = tmpl.Parse(string(headerFile)) + if err != nil { + http.Error(w, "Error parsing header template", http.StatusInternalServerError) + return + } + + // 包括主HTML内容 + tmpl, err = tmpl.Parse(string(file)) + if err != nil { + http.Error(w, "Error parsing HTML template", http.StatusInternalServerError) + return + } + + // 直接将HTML内容发送给客户端 + w.Header().Set("Content-Type", "text/html") + err = tmpl.Execute(w, nil) + if err != nil { + http.Error(w, "Error rendering HTML template", http.StatusInternalServerError) + } } func Pwd(w http.ResponseWriter, r *http.Request) { // 输出 HTML 表单 if r.Method != http.MethodPost { - io.WriteString(w, htmlHead+`

Powered by tgState

`) + file, err := assets.Templates.ReadFile("templates/pwd.tmpl") + if err != nil { + http.Error(w, "HTML file not found", http.StatusNotFound) + return + } + // 读取头部模板 + headerPath := "templates/header.tmpl" + headerFile, err := assets.Templates.ReadFile(headerPath) + if err != nil { + http.Error(w, "Header template not found", http.StatusNotFound) + return + } + + // 创建HTML模板并包括头部 + tmpl := template.New("html") + tmpl, err = tmpl.Parse(string(headerFile)) + if err != nil { + http.Error(w, "Error parsing header template", http.StatusInternalServerError) + return + } + + // 包括主HTML内容 + tmpl, err = tmpl.Parse(string(file)) + if err != nil { + http.Error(w, "Error parsing HTML template", http.StatusInternalServerError) + return + } + + // 直接将HTML内容发送给客户端 + w.Header().Set("Content-Type", "text/html") + err = tmpl.Execute(w, nil) + if err != nil { + http.Error(w, "Error rendering HTML template", http.StatusInternalServerError) + } return } // 设置cookie diff --git a/main.go b/main.go index 65d618f..832cd04 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,9 @@ func main() { } func web() { - http.HandleFunc("/pwd", control.Pwd) + if conf.Pass != "" && conf.Pass != "none" { + http.HandleFunc("/pwd", control.Pwd) + } http.HandleFunc("/d/", control.D) http.HandleFunc("/api", control.Middleware(control.UploadImageAPI)) if index {