Skip to content

Commit

Permalink
Remove unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Feb 21, 2025
1 parent 1082e03 commit 4f43cfc
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 18 deletions.
6 changes: 3 additions & 3 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (i *InstaData) parseTimeSliceImpl(embedBody []byte) error {
if tt == js.StringToken && bytes.Contains(text, []byte("shortcode_media")) {
// Strip quotes from start and end
text = text[1 : len(text)-1]
unescapeData := utils.UnescapeJSONString(utils.B2S(text))
unescapeData := utils.UnescapeJSONString(string(text))
if !gjson.Valid(unescapeData) {
return errors.New("failed to parse data from TimeSliceImpl")
}
Expand Down Expand Up @@ -387,14 +387,14 @@ func GetData(postID string) (*InstaData, error) {
if dataBucket == nil {
return nil
}
dataBucket.Put(utils.S2B(item.PostID), bb)
dataBucket.Put([]byte(item.PostID), bb)

ttlBucket := tx.Bucket([]byte("ttl"))
if ttlBucket == nil {
return nil
}
expTime := strconv.FormatInt(time.Now().Add(24*time.Hour).UnixNano(), 10)
ttlBucket.Put(utils.S2B(expTime), utils.S2B(item.PostID))
ttlBucket.Put([]byte(expTime), []byte(item.PostID))
return nil
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"instafix/handlers"
scraper "instafix/handlers/scraper"
"instafix/utils"
"instafix/views"
"log/slog"
"net/http"
Expand Down Expand Up @@ -120,7 +119,7 @@ func evictCache() {
}
c := ttlBucket.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if n, err := strconv.ParseInt(utils.B2S(k), 10, 64); err == nil {
if n, err := strconv.ParseInt(string(k), 10, 64); err == nil {
if n < curTime {
ttlBucket.Delete(k)
dataBucket.Delete(v)
Expand Down
4 changes: 2 additions & 2 deletions utils/jsonesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func UnescapeJSONString(s string) string {
}

// Slow path - unescape string.
b := S2B(s) // It is safe to do, since s points to a byte slice in Parser.b.
b := []byte(s) // It is safe to do, since s points to a byte slice in Parser.b.
b = b[:n]
s = s[n+1:]
for len(s) > 0 {
Expand Down Expand Up @@ -190,7 +190,7 @@ func UnescapeJSONString(s string) string {
b = append(b, s[:n]...)
s = s[n+1:]
}
return B2S(b)
return string(b)
}

func EscapeJSONString(src string) string {
Expand Down
11 changes: 0 additions & 11 deletions utils/strconv.go

This file was deleted.

0 comments on commit 4f43cfc

Please sign in to comment.