diff --git a/internal/dal/model/st_category.gen.go b/internal/dal/model/st_category.gen.go index a57ebab..35a59e5 100644 --- a/internal/dal/model/st_category.gen.go +++ b/internal/dal/model/st_category.gen.go @@ -12,7 +12,7 @@ const TableNameStCategory = "st_category" // StCategory mapped from table type StCategory struct { - ID int `gorm:"column:id;type:INTEGER" json:"id"` + ID int `gorm:"column:id;type:INTEGER;primaryKey" json:"id"` ParentID int `gorm:"column:parent_id;type:int(11);not null" json:"parent_id"` Sort int `gorm:"column:sort;type:int(11);not null" json:"sort"` Title string `gorm:"column:title;type:varchar(50);not null" json:"title"` diff --git a/internal/dal/model/st_site.gen.go b/internal/dal/model/st_site.gen.go index 23d94f7..e811186 100644 --- a/internal/dal/model/st_site.gen.go +++ b/internal/dal/model/st_site.gen.go @@ -15,7 +15,7 @@ type StSite struct { ID int `gorm:"column:id;type:INTEGER" json:"id"` CategoryID int `gorm:"column:category_id;type:int(11)" json:"category_id"` Title string `gorm:"column:title;type:varchar(50)" json:"title"` - Icon string `gorm:"column:icon;type:varchar(255)" json:"icon"` + Icon string `gorm:"column:icon;type:text" json:"icon"` Description string `gorm:"column:description;type:varchar(500)" json:"description"` URL string `gorm:"column:url;type:varchar(255);not null" json:"url"` IsUsed bool `gorm:"column:is_used;type:bool;default:false" json:"is_used"` diff --git a/internal/dal/model/sys_menu.gen.go b/internal/dal/model/sys_menu.gen.go index f2e461e..c19f848 100644 --- a/internal/dal/model/sys_menu.gen.go +++ b/internal/dal/model/sys_menu.gen.go @@ -12,7 +12,7 @@ const TableNameSysMenu = "sys_menu" // SysMenu mapped from table type SysMenu struct { - ID int `gorm:"column:id;type:INTEGER" json:"id"` + ID int `gorm:"column:id;type:INTEGER;primaryKey" json:"id"` Pid int `gorm:"column:pid;type:int(11);not null" json:"pid"` Name string `gorm:"column:name;type:varchar(32);not null" json:"name"` Link string `gorm:"column:link;type:varchar(100);not null" json:"link"` diff --git a/internal/dal/model/sys_user.gen.go b/internal/dal/model/sys_user.gen.go index 6945037..26399a3 100644 --- a/internal/dal/model/sys_user.gen.go +++ b/internal/dal/model/sys_user.gen.go @@ -12,7 +12,7 @@ const TableNameSysUser = "sys_user" // SysUser mapped from table type SysUser struct { - ID int `gorm:"column:id;type:INTEGER" json:"id"` + ID int `gorm:"column:id;type:INTEGER;primaryKey" json:"id"` Username string `gorm:"column:username;type:varchar(32);not null" json:"username"` Password string `gorm:"column:password;type:varchar(100);not null" json:"password"` CreatedAt *time.Time `gorm:"column:created_at;type:datetime;not null;default:CURRENT_TIMESTAMP not null" json:"created_at"` diff --git a/internal/dal/model/sys_user_menu.gen.go b/internal/dal/model/sys_user_menu.gen.go index 48c486d..9e5d623 100644 --- a/internal/dal/model/sys_user_menu.gen.go +++ b/internal/dal/model/sys_user_menu.gen.go @@ -12,7 +12,7 @@ const TableNameSysUserMenu = "sys_user_menu" // SysUserMenu mapped from table type SysUserMenu struct { - ID int `gorm:"column:id;type:INTEGER" json:"id"` + ID int `gorm:"column:id;type:INTEGER;primaryKey" json:"id"` UserID int `gorm:"column:user_id;type:int(11);not null" json:"user_id"` MenuID int `gorm:"column:menu_id;type:int(11);not null" json:"menu_id"` CreatedAt *time.Time `gorm:"column:created_at;type:datetime;not null;default:CURRENT_TIMESTAMP not null" json:"created_at"` diff --git a/internal/server/http.go b/internal/server/http.go index c027ddf..90e82a1 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -43,7 +43,6 @@ func NewHTTPServer( httpx.WithServerPort(conf.GetInt("http.port")), ) - s.Static("/upload", "web/upload") s.StaticFS("/assets", http.FS(assets.Static)) s.SetHTMLTemplate(template.Must(template.New("").ParseFS(assets.Templates, "templates/**/*"))) diff --git a/internal/service/site/batchcreate.go b/internal/service/site/batchcreate.go index eed3ec7..f4dd1f7 100644 --- a/internal/service/site/batchcreate.go +++ b/internal/service/site/batchcreate.go @@ -38,7 +38,7 @@ func (s *service) BatchCreate(ctx context.Context, req *v1.SiteCreateReq) (*v1.S return nil }) g.Go(func() error { - icon = getWebLogoIconByURL(url) + icon = getWebLogoIcon(url) return nil }) g.Go(func() error { diff --git a/internal/service/site/service.go b/internal/service/site/service.go index 0bf60e9..0ce3df6 100644 --- a/internal/service/site/service.go +++ b/internal/service/site/service.go @@ -8,15 +8,10 @@ package site import ( "context" "crypto/tls" - "fmt" + "encoding/base64" "io" - "io/ioutil" "net/http" - "os" - "path" - "path/filepath" - "github.com/duke-git/lancet/v2/random" "github.com/gin-gonic/gin" "github.com/gocolly/colly" "github.com/mat/besticon/besticon" @@ -58,9 +53,10 @@ func (s *service) i() {} // ----------------------------------------------------------------------------------------------------------------------------------------- -const defaultIcon = "/upload/favicon.png" +// default icon base64 +const defaultIcon = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAA+VBMVEUAAAC6sWUzNjZCQkIsLi8sLjAsLi//tSs6Oj8tLzAsLi/9sycsLi/9sycsLi/9tCctLi/8tSj/tSguMTEuMzQzMzMwMzb/ujP9tCcsLi8uLzEtLy8sLi8sLi8tLi8sLi8tLi8sLi8tLjAsLi8sLy8sLi8sLjAsLjAtLy8tLzAtLzAuMDAuMDL/tyn9tCf8tCctLi/8tCf8tCf8tCf9sycsLjD9tCgsLjD9tSf9tCgtLzAtMDAuLzAvLzEvLzL/ty0tLy/+tij9tCj9tCj9syf9syj9tCf9tCj9tCf9tCn8tSj/tCj/tikwMDT/vzP/v0AsLy/8sycsLi+FeN01AAAAUXRSTlMAAg8F/uOqIwv7+Pjp6dLQdWM/KSIcFg3v7Uv18tzZzce5ta+ej4eBb2lUQT0x+8jBvrWvqpqLiollZGBcNjAck1T039fNo5h7cFpHOB8UCKLRW+BFAAACmElEQVRYw82VaVPyMBSFW5aC7FulgMiqICIIIiq4gPvum/7/H/NOe+00xwZL+OTzhZkm5wz35txE+eOo2ZExyqob63NpZpHObSaPTJjDJCIvL57HmUv8vChbfJIhyawqVzwg2YrICRNzEpEoHpBohXoBxXtIXqhSxUu0QlS8ZCuK0zhbj/i0KFu8fyuO0kyO9BHqDSaLAQ5jJs+YNzCYPAZvMGTyDHmDVybPq8KTjUvK41kFuZlI6Sc3gjl4WFv+kPO7hpD1L6fidOArH8AgeIiM/eITWSUtdIP279v9L/L7N3tPsFv4KY/ux8xwj1rxYqzK3gsV3wubsf0o6AN10+L4WrH4fBqIin/6tFevj+299QBv0DaJ0Ok2TefIox/R9G2fhkyizRuETYfSdyuWdyC/W34XXzIdwrzBrumyW7Y/fT3fMofb5y/7Wxn28QZnJk8zT+l+ZMQj5TbfhF1nvEFQg7WtPerQ+9Aa23fq894W7NGCCjBPwHJiplKwlhQcdfZjfa6IksBT63OL/RqsuSkACi0T0D4U4kPDhRaXQ+QwBRsrnYBVfKcCX1OHv72tByW0aC1aKC8dqCuniZq9wGYjW4uAuxfp10JtinG+sVLfyFOc2yGnxZiknQydba8qlFd7lJnMDuTIJuPsKbt7ANe97LhneIOU4F+CXFBfijdIeGN8Vef09StvnBO8gSY6qcuwM7iXojPWeAO9KspKtBOzctuJilJW1RVwEKdV7/7r6uKc6z4xhnmBSYM4Y4ydVkLl0A0iIYgzNBl7T+eBxyRCdGuFmrPyrBny3ndi4N4E4Mb1AWIsirMv9HYA+Or4Q68XQu+eBL0wqunllSEKN2GF4iyHrkFuN6FfwxdCHnXeKDXmqvKn+Q+oeE3vIQF62QAAAABJRU5ErkJggg==" -func getWebLogoIconByURL(url string) string { +func getWebLogoIcon(url string) string { client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, @@ -68,7 +64,7 @@ func getWebLogoIconByURL(url string) string { } b := besticon.New( - besticon.WithLogger(besticon.NewDefaultLogger(ioutil.Discard)), // disable verbose logging + besticon.WithLogger(besticon.NewDefaultLogger(io.Discard)), // disable verbose logging besticon.WithHTTPClient(client), ) @@ -77,31 +73,18 @@ func getWebLogoIconByURL(url string) string { return defaultIcon } - // get picture format - var format string - if ext := filepath.Ext(icons[0].URL); ext != "" { - format = ext[1:] - } - - // image save static resource directory - dst := path.Join("upload", fmt.Sprintf("%s.%s", random.RandNumeralOrLetter(6), format)) - file, err := os.Create(filepath.Join("web", dst)) + resp, err := client.Get(icons[0].URL) if err != nil { return defaultIcon } - defer file.Close() + defer resp.Body.Close() - response, err := client.Get(icons[0].URL) + imgData, err := io.ReadAll(resp.Body) if err != nil { return defaultIcon } - defer response.Body.Close() - - if _, err := io.Copy(file, response.Body); err != nil { - return defaultIcon - } - return filepath.Join("/", dst) + return base64.StdEncoding.EncodeToString(imgData) } func getWebTitle(url string) (title string) { diff --git a/internal/service/site/sync.go b/internal/service/site/sync.go index 23cabb0..aeb5b0f 100644 --- a/internal/service/site/sync.go +++ b/internal/service/site/sync.go @@ -20,7 +20,7 @@ func (s *service) Sync(ctx *gin.Context, req *v1.SiteSyncReq) (resp *v1.SiteSync _, err = s.siteRepository.WithContext(ctx).Update(&model.StSite{ Title: getWebTitle(site.URL), - Icon: getWebLogoIconByURL(site.URL), + Icon: getWebLogoIcon(site.URL), Description: getWebDescription(site.URL), IsUsed: false, }, diff --git a/web/templates/index/index.html b/web/templates/index/index.html index 8164525..4ad94f8 100755 --- a/web/templates/index/index.html +++ b/web/templates/index/index.html @@ -181,7 +181,7 @@

- +