Skip to content

Commit 26812f3

Browse files
committed
feat(site): update site icon handling - Remove file upload and storage for site icons
- Use base64 encoded icons instead of file paths - Update related models, services, and templates
1 parent 8bedede commit 26812f3

File tree

11 files changed

+18
-36
lines changed

11 files changed

+18
-36
lines changed

internal/dal/model/st_category.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/dal/model/st_site.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/dal/model/sys_menu.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/dal/model/sys_user.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/dal/model/sys_user_menu.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/http.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func NewHTTPServer(
4343
httpx.WithServerPort(conf.GetInt("http.port")),
4444
)
4545

46-
s.Static("/upload", "web/upload")
4746
s.StaticFS("/assets", http.FS(assets.Static))
4847
s.SetHTMLTemplate(template.Must(template.New("").ParseFS(assets.Templates, "templates/**/*")))
4948

internal/service/site/batchcreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (s *service) BatchCreate(ctx context.Context, req *v1.SiteCreateReq) (*v1.S
3838
return nil
3939
})
4040
g.Go(func() error {
41-
icon = getWebLogoIconByURL(url)
41+
icon = getWebLogoIcon(url)
4242
return nil
4343
})
4444
g.Go(func() error {

internal/service/site/service.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ package site
88
import (
99
"context"
1010
"crypto/tls"
11-
"fmt"
11+
"encoding/base64"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
15-
"os"
16-
"path"
17-
"path/filepath"
1814

19-
"github.com/duke-git/lancet/v2/random"
2015
"github.com/gin-gonic/gin"
2116
"github.com/gocolly/colly"
2217
"github.com/mat/besticon/besticon"
@@ -58,17 +53,18 @@ func (s *service) i() {}
5853

5954
// -----------------------------------------------------------------------------------------------------------------------------------------
6055

61-
const defaultIcon = "/upload/favicon.png"
56+
// default icon base64
57+
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=="
6258

63-
func getWebLogoIconByURL(url string) string {
59+
func getWebLogoIcon(url string) string {
6460
client := &http.Client{
6561
Transport: &http.Transport{
6662
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
6763
},
6864
}
6965

7066
b := besticon.New(
71-
besticon.WithLogger(besticon.NewDefaultLogger(ioutil.Discard)), // disable verbose logging
67+
besticon.WithLogger(besticon.NewDefaultLogger(io.Discard)), // disable verbose logging
7268
besticon.WithHTTPClient(client),
7369
)
7470

@@ -77,31 +73,18 @@ func getWebLogoIconByURL(url string) string {
7773
return defaultIcon
7874
}
7975

80-
// get picture format
81-
var format string
82-
if ext := filepath.Ext(icons[0].URL); ext != "" {
83-
format = ext[1:]
84-
}
85-
86-
// image save static resource directory
87-
dst := path.Join("upload", fmt.Sprintf("%s.%s", random.RandNumeralOrLetter(6), format))
88-
file, err := os.Create(filepath.Join("web", dst))
76+
resp, err := client.Get(icons[0].URL)
8977
if err != nil {
9078
return defaultIcon
9179
}
92-
defer file.Close()
80+
defer resp.Body.Close()
9381

94-
response, err := client.Get(icons[0].URL)
82+
imgData, err := io.ReadAll(resp.Body)
9583
if err != nil {
9684
return defaultIcon
9785
}
98-
defer response.Body.Close()
99-
100-
if _, err := io.Copy(file, response.Body); err != nil {
101-
return defaultIcon
102-
}
10386

104-
return filepath.Join("/", dst)
87+
return base64.StdEncoding.EncodeToString(imgData)
10588
}
10689

10790
func getWebTitle(url string) (title string) {

internal/service/site/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (s *service) Sync(ctx *gin.Context, req *v1.SiteSyncReq) (resp *v1.SiteSync
2020

2121
_, err = s.siteRepository.WithContext(ctx).Update(&model.StSite{
2222
Title: getWebTitle(site.URL),
23-
Icon: getWebLogoIconByURL(site.URL),
23+
Icon: getWebLogoIcon(site.URL),
2424
Description: getWebDescription(site.URL),
2525
IsUsed: false,
2626
},

web/templates/index/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ <h4 class="text-gray"><i class="linecons-tag" style="margin-right: 7px;" id="{{
181181
data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{ .Description }}">
182182
<div class="xe-comment-entry">
183183
<a class="xe-user-img">
184-
<img data-src="{{ .Icon }}" class="lozad img-circle" width="40">
184+
<img data-src="data:image/png;base64,{{ .Icon }}" class="lozad img-circle" width="40">
185185
</a>
186186
<div class="xe-comment">
187187
<a href="#" class="xe-user-name overflowClip_1">

0 commit comments

Comments
 (0)