Skip to content

Commit

Permalink
Rework imgcache module
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelGoerentz committed Jul 8, 2024
1 parent 21f1eee commit 29cdd93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
14 changes: 0 additions & 14 deletions src/internal/imgcache/imagecache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package imgcache

import (
"bytes"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
)
Expand Down Expand Up @@ -80,18 +78,6 @@ func (ic *ImageCache) GetImageURL(url string) (string) {
}
}

// Faster creation of file names
func createFileNameFromURL(url string, key string) (string) {
url_stripped := strings.Split(url, "?")[0]
ext := filepath.Ext(url_stripped)

var buf bytes.Buffer
buf.WriteString(key)
buf.WriteString(ext)

return buf.String()
}

// Download the Image
func (ic *ImageCache) DownloadImage(url string, filename string) (error) {

Expand Down
43 changes: 15 additions & 28 deletions src/internal/imgcache/tools.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
package imgcache

import (
"bytes"
"crypto/md5"
"encoding/hex"
"io"
"path/filepath"
"strings"
)

func strToMD5(str string) string {
md5Hasher := md5.New()
md5Hasher.Write([]byte(str))
return hex.EncodeToString(md5Hasher.Sum(nil))
}

func indexOfString(str string, slice []string) int {

for i, v := range slice {
if str == v {
return i
}
}

return -1
}

func removeStringFromSlice(str string, slice []string) []string {

var i = indexOfString(str, slice)

if i != -1 {
slice = append(slice[:i], slice[i+1:]...)
}

return slice
}

// createIndexFromUrl will calculate the URLs md5 and will pick then only the digits within the string too create a key
func createKeyFromUrl(url string) (string) {
// Berechne den MD5-Hash der URL
hasher := md5.New()
io.WriteString(hasher, url)
return hex.EncodeToString(hasher.Sum(nil))
}

// Faster creation of file names
func createFileNameFromURL(url string, key string) (string) {
url_stripped := strings.Split(url, "?")[0]
ext := filepath.Ext(url_stripped)

var buf bytes.Buffer
buf.WriteString(key)
buf.WriteString(ext)

return buf.String()
}

0 comments on commit 29cdd93

Please sign in to comment.