group
This commit is contained in:
63
bin/main.go
63
bin/main.go
@@ -39,6 +39,38 @@ func main() {
|
||||
var mysqlConnection models.MysqlConnection
|
||||
mysqlConnection.Init()
|
||||
|
||||
http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.Method, r.URL.Path)
|
||||
// URL 格式: /img/{type}/{id}.{format}?width=320&height=320&fit=cover
|
||||
reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)/([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`)
|
||||
matches := reg.FindStringSubmatch(r.URL.Path)
|
||||
if len(matches) != 5 {
|
||||
log.Println("URL 格式错误", matches)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
group, id, format, width, height, fit := matches[1], matches[2], matches[3], stringToInt(r.URL.Query().Get("width"), 0), stringToInt(r.URL.Query().Get("height"), 0), r.URL.Query().Get("fit")
|
||||
content, err := mysqlConnection.GetImageContent(group, id)
|
||||
if err != nil {
|
||||
log.Println("获取图片失败", format, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var img models.Image
|
||||
img.Init(content)
|
||||
|
||||
data, err := img.ToWebP(width, height, fit)
|
||||
if err != nil {
|
||||
log.Println("转换图片失败", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/webp")
|
||||
w.Header().Set("Cache-Control", "max-age=31536000")
|
||||
w.Write(data)
|
||||
})
|
||||
|
||||
// 直接走 CDN 的请求不缓存本地, 因此直接使用参数
|
||||
http.HandleFunc("/webp/", func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println(r.Method, r.URL.Path)
|
||||
@@ -72,6 +104,37 @@ func main() {
|
||||
w.Write(data)
|
||||
})
|
||||
|
||||
http.HandleFunc("/article/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// URL 格式: /article/{id}.{格式}?width=320&height=320&fit=cover
|
||||
reg := regexp.MustCompile(`^/article/([0-9a-zA-Z]+).(jpg|jpeg|png|webp)$`)
|
||||
matches := reg.FindStringSubmatch(r.URL.Path)
|
||||
if len(matches) != 3 {
|
||||
log.Println("URL 格式错误", matches)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
id, format, width, height, fit := matches[1], matches[2], stringToInt(r.URL.Query().Get("width"), 0), stringToInt(r.URL.Query().Get("height"), 0), r.URL.Query().Get("fit")
|
||||
content, err := mysqlConnection.GetArticleImageContent(id)
|
||||
if err != nil {
|
||||
log.Println("获取图片失败", format, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var img models.Image
|
||||
img.Init(content)
|
||||
|
||||
data, err := img.ToWebP(width, height, fit)
|
||||
if err != nil {
|
||||
log.Println("转换图片失败", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/webp")
|
||||
w.Header().Set("Cache-Control", "max-age=31536000")
|
||||
w.Write(data)
|
||||
})
|
||||
|
||||
http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// URL 格式: /img/{id}-{version}@{倍图}{宽度}.{格式}
|
||||
reg := regexp.MustCompile(`^/img/([0-9a-zA-Z]+)-([0-9a-zA-Z]+)@([0-9]{1,4})x([0-9]{1,4}).(jpg|jpeg|png|webp)$`)
|
||||
|
@@ -29,9 +29,19 @@ func (m *MysqlConnection) Init() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// 根据 id 获取图片的 content
|
||||
func (m *MysqlConnection) GetImageContent(id string) (key string, err error) {
|
||||
var content string
|
||||
err = m.db.QueryRow("SELECT content FROM web_images WHERE id=" + id).Scan(&content)
|
||||
// 根据图片 id 获取图片的 content
|
||||
func (m *MysqlConnection) GetImageContent(group string, id string) (content string, err error) {
|
||||
switch group {
|
||||
case "article":
|
||||
err = m.db.QueryRow("SELECT image FROM web_article WHERE id=" + id).Scan(&content)
|
||||
case "article_attribute":
|
||||
err = m.db.QueryRow("SELECT image FROM web_article_attribute WHERE id=" + id).Scan(&content)
|
||||
case "ad":
|
||||
err = m.db.QueryRow("SELECT image FROM web_ad WHERE id=" + id).Scan(&content)
|
||||
case "user":
|
||||
err = m.db.QueryRow("SELECT avatar FROM web_member WHERE id=" + id).Scan(&content)
|
||||
case "image":
|
||||
err = m.db.QueryRow("SELECT content FROM web_images WHERE id=" + id).Scan(&content)
|
||||
}
|
||||
return content, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user