From a53f0ac44a12b1787ff0d59b57e12f9d4b5fcfe1 Mon Sep 17 00:00:00 2001 From: Benny Date: Tue, 7 Feb 2023 06:39:48 +0100 Subject: [PATCH] check image size (#170) * check image size could be useful for #169 * Remove more deprecated ioutil lib, and fix CI problem * Fix deps * Write a bigger file for testing --------- Co-authored-by: n0vad3v --- .gitignore | 1 + encoder_test.go | 1 + helper.go | 7 ++++++- helper_test.go | 6 ++++-- update.go | 2 ++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 33c0635..30d5d82 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # Dependency directories (remove the comment below to include it) exhaust/ +exhaust_test/ builds/ /.idea/.gitignore /.idea/misc.xml diff --git a/encoder_test.go b/encoder_test.go index e7c7f0b..8143215 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -2,6 +2,7 @@ package main import ( "github.com/stretchr/testify/assert" + "os" "path" "path/filepath" diff --git a/helper.go b/helper.go index 6839f70..32f23ff 100644 --- a/helper.go +++ b/helper.go @@ -3,7 +3,6 @@ package main import ( "bytes" "fmt" - "github.com/h2non/filetype" "hash/crc32" "io" "net/http" @@ -12,6 +11,8 @@ import ( "path/filepath" "strconv" + "github.com/h2non/filetype" + "github.com/valyala/fasthttp" "strings" @@ -59,6 +60,10 @@ func imageExists(filename string) bool { if os.IsNotExist(err) { return false } + if info.Size() < 100 { + // means something wrong in exhaust file system + return false + } log.Debugf("file %s exists!", filename) return !info.IsDir() } diff --git a/helper_test.go b/helper_test.go index e410b55..8aced5b 100644 --- a/helper_test.go +++ b/helper_test.go @@ -3,6 +3,7 @@ package main import ( log "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" + "net/http" "os" "path/filepath" @@ -32,7 +33,7 @@ func TestFileCount(t *testing.T) { func TestImageExists(t *testing.T) { var data = "./pics/empty.jpg" - var result = !imageExists(data) + var result = imageExists(data) if result { t.Errorf("Result: [%v], Expected: [%v]", result, false) @@ -168,7 +169,8 @@ func TestFetchRemoteImage(t *testing.T) { func TestCleanProxyCache(t *testing.T) { // test normal situation fp := filepath.Join("./exhaust", "sample.png.12345.webp") - _ = os.WriteFile(fp, []byte("1234"), 0755) + buf := make([]byte, 0x1000) + _ = os.WriteFile(fp, buf, 0755) assert.True(t, imageExists(fp)) cleanProxyCache(fp) assert.False(t, imageExists(fp)) diff --git a/update.go b/update.go index 55a0140..6327f4d 100644 --- a/update.go +++ b/update.go @@ -3,8 +3,10 @@ package main import ( "encoding/json" "fmt" + log "github.com/sirupsen/logrus" "github.com/staktrace/go-update" + "io" "net/http" "runtime"