SkunkyArt/app/cache.go

139 lines
2.7 KiB
Go
Raw Normal View History

// TODO: реализовать кеширование JSON и почистить код
package app
import (
"crypto/sha1"
"encoding/hex"
"io"
"os"
"strings"
"sync"
"syscall"
"time"
)
type file struct {
Score int
Content []byte
}
var tempFS = make(map[[20]byte]*file)
var mx = &sync.RWMutex{}
func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
var url strings.Builder
url.WriteString("https://images-wixmp-")
url.WriteString(subdomain)
url.WriteString(".wixmp.com/")
url.WriteString(path)
2024-09-02 08:16:54 +00:00
if t := s.Args.Get("token"); t != "" {
2024-09-23 06:38:32 +00:00
url.WriteString("?token=")
url.WriteString(t)
2024-09-02 08:16:54 +00:00
}
2024-09-23 06:38:32 +00:00
var response []byte
switch {
case CFG.Cache.Enabled:
fileName := sha1.Sum([]byte(subdomain + path))
filePath := CFG.Cache.Path + "/" + hex.EncodeToString(fileName[:])
mx.Lock()
if tempFS[fileName] == nil {
tempFS[fileName] = &file{}
}
mx.Unlock()
2024-09-02 08:16:54 +00:00
if tempFS[fileName].Content != nil {
response = tempFS[fileName].Content
tempFS[fileName].Score += 2
break
} else {
file, err := os.Open(filePath)
if err != nil {
if dwnld := Download(url.String()); dwnld.Status == 200 && dwnld.Headers["Content-Type"][0][:5] == "image" {
2024-09-02 08:16:54 +00:00
response = dwnld.Body
try(os.WriteFile(filePath, response, 0700))
} else {
s.ReturnHTTPError(dwnld.Status)
return
}
} else {
file, e := io.ReadAll(file)
try(e)
2024-09-02 08:16:54 +00:00
response = file
}
go func() {
defer restore()
2024-09-02 08:16:54 +00:00
mx.RLock()
tempFS[fileName].Content = response
mx.RUnlock()
for {
time.Sleep(1 * time.Minute)
mx.Lock()
if tempFS[fileName].Score <= 0 {
delete(tempFS, fileName)
mx.Unlock()
return
}
tempFS[fileName].Score--
mx.Unlock()
}
}()
}
case CFG.Proxy:
dwnld := Download(url.String())
if dwnld.Status != 200 {
s.ReturnHTTPError(dwnld.Status)
return
}
response = dwnld.Body
default:
s.Writer.WriteHeader(403)
response = []byte("Sorry, butt proxy on this instance are disabled.")
}
s.Writer.Write(response)
}
func InitCacheSystem() {
c := &CFG.Cache
for {
2024-08-14 16:18:06 +00:00
dir, err := os.ReadDir(c.Path)
if err != nil {
if os.IsNotExist(err) {
os.Mkdir(c.Path, 0700)
continue
}
println(err.Error())
}
for _, file := range dir {
fileName := c.Path + "/" + file.Name()
fileInfo, err := file.Info()
try(err)
if c.Lifetime != "" {
now := time.Now().UnixMilli()
2024-08-14 16:18:06 +00:00
stat := fileInfo.Sys().(*syscall.Stat_t)
time := statTime(stat)
if time+lifetimeParsed <= now {
2024-08-14 16:18:06 +00:00
try(os.RemoveAll(fileName))
}
}
2024-09-23 06:38:32 +00:00
2024-08-14 16:18:06 +00:00
if c.MaxSize != 0 && fileInfo.Size() > c.MaxSize {
try(os.RemoveAll(fileName))
}
}
time.Sleep(time.Second * time.Duration(c.UpdateInterval))
}
}