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 <n0vad3v@riseup.net>
This commit is contained in:
Benny 2023-02-07 06:39:48 +01:00 committed by GitHub
parent 16ff497ade
commit a53f0ac44a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 3 deletions

1
.gitignore vendored
View File

@ -13,6 +13,7 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
exhaust/ exhaust/
exhaust_test/
builds/ builds/
/.idea/.gitignore /.idea/.gitignore
/.idea/misc.xml /.idea/misc.xml

View File

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/h2non/filetype"
"hash/crc32" "hash/crc32"
"io" "io"
"net/http" "net/http"
@ -12,6 +11,8 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/h2non/filetype"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"strings" "strings"
@ -59,6 +60,10 @@ func imageExists(filename string) bool {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false return false
} }
if info.Size() < 100 {
// means something wrong in exhaust file system
return false
}
log.Debugf("file %s exists!", filename) log.Debugf("file %s exists!", filename)
return !info.IsDir() return !info.IsDir()
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -32,7 +33,7 @@ func TestFileCount(t *testing.T) {
func TestImageExists(t *testing.T) { func TestImageExists(t *testing.T) {
var data = "./pics/empty.jpg" var data = "./pics/empty.jpg"
var result = !imageExists(data) var result = imageExists(data)
if result { if result {
t.Errorf("Result: [%v], Expected: [%v]", result, false) t.Errorf("Result: [%v], Expected: [%v]", result, false)
@ -168,7 +169,8 @@ func TestFetchRemoteImage(t *testing.T) {
func TestCleanProxyCache(t *testing.T) { func TestCleanProxyCache(t *testing.T) {
// test normal situation // test normal situation
fp := filepath.Join("./exhaust", "sample.png.12345.webp") 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)) assert.True(t, imageExists(fp))
cleanProxyCache(fp) cleanProxyCache(fp)
assert.False(t, imageExists(fp)) assert.False(t, imageExists(fp))

View File

@ -3,8 +3,10 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/staktrace/go-update" "github.com/staktrace/go-update"
"io" "io"
"net/http" "net/http"
"runtime" "runtime"