commit
978a609684
11
content.go
11
content.go
@ -231,17 +231,10 @@ func handleContent(cc *contentConn) {
|
|||||||
case *mt.ToCltAnnounceMedia:
|
case *mt.ToCltAnnounceMedia:
|
||||||
var filenames []string
|
var filenames []string
|
||||||
|
|
||||||
RequestLoop:
|
|
||||||
for _, f := range cmd.Files {
|
for _, f := range cmd.Files {
|
||||||
switch len(f.Base64SHA1) % 4 {
|
|
||||||
case 2:
|
|
||||||
f.Base64SHA1 += "=="
|
|
||||||
case 3:
|
|
||||||
f.Base64SHA1 += "="
|
|
||||||
}
|
|
||||||
|
|
||||||
if cc.fromCache(f.Name, f.Base64SHA1) {
|
if cc.fromCache(f.Name, f.Base64SHA1) {
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
filenames = append(filenames, f.Name)
|
filenames = append(filenames, f.Name)
|
||||||
@ -249,7 +242,7 @@ func handleContent(cc *contentConn) {
|
|||||||
for i, mf := range cc.media {
|
for i, mf := range cc.media {
|
||||||
if mf.name == f.Name {
|
if mf.name == f.Name {
|
||||||
cc.media[i].base64SHA1 = f.Base64SHA1
|
cc.media[i].base64SHA1 = f.Base64SHA1
|
||||||
continue RequestLoop
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,23 +2,24 @@ package proxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/base64"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cc *contentConn) fromCache(filename, base64SHA1 string) bool {
|
func (cc *contentConn) fromCache(filename, base64SHA1 string) bool {
|
||||||
os.Mkdir(Path("cache"), 0777)
|
os.Mkdir(Path("cache"), 0777)
|
||||||
|
|
||||||
hash, err := b64.DecodeString(base64SHA1)
|
// convert to filename safe b64
|
||||||
if err != nil {
|
base64SHA1Filesafe := strings.Replace(base64SHA1, "/", "_", -1)
|
||||||
cc.log("<-", base64SHA1, ": ", err)
|
base64SHA1Filesafe = strings.Replace(base64SHA1Filesafe, "+", "-", -1)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
hexSHA1 := hex.EncodeToString(hash)
|
data, err := os.ReadFile(Path("cache/", base64SHA1Filesafe))
|
||||||
|
|
||||||
data, err := os.ReadFile(Path("cache/", hexSHA1))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
cc.log("->", "cache", err)
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,20 +36,17 @@ func (cc *contentConn) updateCache() {
|
|||||||
os.Mkdir(Path("cache"), 0777)
|
os.Mkdir(Path("cache"), 0777)
|
||||||
|
|
||||||
for _, f := range cc.media {
|
for _, f := range cc.media {
|
||||||
hash, err := b64.DecodeString(f.base64SHA1)
|
// convert to filename safe b64
|
||||||
if err != nil {
|
base64SHA1Filesafe := strings.Replace(f.base64SHA1, "/", "_", -1)
|
||||||
cc.log("<-", f.base64SHA1, ": ", err)
|
base64SHA1Filesafe = strings.Replace(base64SHA1Filesafe, "+", "-", -1)
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
hexSHA1 := hex.EncodeToString(hash)
|
os.WriteFile(Path("cache/", base64SHA1Filesafe), f.data, 0666)
|
||||||
os.WriteFile(Path("cache/", hexSHA1), f.data, 0666)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cacheMedia(f mediaFile) {
|
func cacheMedia(f mediaFile) {
|
||||||
hash := sha1.Sum(f.data)
|
hash := sha1.Sum(f.data)
|
||||||
sum := hex.EncodeToString(hash[:])
|
sum := base64.RawStdEncoding.EncodeToString(hash[:])
|
||||||
|
|
||||||
os.WriteFile(Path("cache/", sum), f.data, 0666)
|
os.WriteFile(Path("cache/", sum), f.data, 0666)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user