Skip to content

Commit

Permalink
chore: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Jul 15, 2024
1 parent ffa94c9 commit 53a0c7d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
3 changes: 3 additions & 0 deletions db/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ const (
func (r ResolutionType) String() string {
return string(r)
}


const JwtSerectKey = "jwt_secrect_key"
16 changes: 14 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ func Open() (*Client, error) {
if err := client.Schema.Create(context.Background()); err != nil {
return nil, errors.Wrap(err, "failed creating schema resources")
}
return &Client{
c := &Client{
ent: client,
}, nil
}

c.generateJwtSerectIfNotExist()
return c, nil
}

func (c *Client) generateJwtSerectIfNotExist() {
v := c.GetSetting(JwtSerectKey)
if v == "" {
log.Infof("generate jwt serect")
key := utils.RandString(32)
c.SetSetting(JwtSerectKey, key)
}
}

func (c *Client) GetSetting(key string) string {
Expand Down
43 changes: 27 additions & 16 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,46 @@ import (
"unicode"

"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/rand"
)

func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}

// HashPassword generates a bcrypt hash for the given password.
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}

// VerifyPassword verifies if the given password matches the stored hash.
func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}

func IsChineseChar(str string) bool {

for _, r := range str {
if unicode.Is(unicode.Han, r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
return true
}
}
return false
for _, r := range str {
if unicode.Is(unicode.Han, r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
return true
}
}
return false
}

var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func RandString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
5 changes: 2 additions & 3 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *Server) authModdleware(c *gin.Context) {
auth = strings.TrimPrefix(auth, "Bearer ")
log.Infof("current token: %v", auth)
token, err := jwt.ParseWithClaims(auth, &jwt.RegisteredClaims{}, func(t *jwt.Token) (interface{}, error) {
return []byte(secretKey), nil
return []byte(s.jwtSerect), nil
})
if err != nil {
log.Errorf("parse token error: %v", err)
Expand Down Expand Up @@ -60,7 +60,6 @@ type LoginIn struct {
Password string `json:"password"`
}

const secretKey = "r1OF7nhpNjnYiGKtTLuKEVq7YznzT"

func (s *Server) Login(c *gin.Context) (interface{}, error) {
var in LoginIn
Expand Down Expand Up @@ -89,7 +88,7 @@ func (s *Server) Login(c *gin.Context) (interface{}, error) {
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
})
sig, err := token.SignedString([]byte(secretKey))
sig, err := token.SignedString([]byte(s.jwtSerect))
if err != nil {
return nil, errors.Wrap(err, "sign")
}
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ type Server struct {
cron *cron.Cron
language string
tasks map[int]*Task
jwtSerect string
}

func (s *Server) Serve() error {
s.scheduler()
s.reloadTasks()
s.jwtSerect = s.db.GetSetting(db.JwtSerectKey)
//st, _ := fs.Sub(ui.Web, "build/web")
s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web")))

Expand Down
1 change: 1 addition & 0 deletions server/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,6 @@ func (s *Server) DeleteFromWatchlist(c *gin.Context) (interface{}, error) {
if err := s.db.DeleteSeries(id); err != nil {
return nil, errors.Wrap(err, "delete db")
}
os.RemoveAll(filepath.Join(db.ImgPath, ids)) //delete image related
return "success", nil
}
4 changes: 2 additions & 2 deletions ui/lib/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class _SearchPageState extends ConsumerState<SearchPage> {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (context, setState) {
return Consumer(
builder: (context, ref, _) {
String _resSelected = "1080p";
int _storageSelected = 0;
var storage = ref.watch(storageSettingProvider);
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/tv_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class _TvDetailsPageState extends ConsumerState<TvDetailsPage> {
error: (err, trace) {
return Text("$err");
},
loading: () => MyProgressIndicator());
loading: () => const MyProgressIndicator());
});
}
}

0 comments on commit 53a0c7d

Please sign in to comment.