diff --git a/utils/download.go b/utils/download.go index bdd6a4f..36a8685 100644 --- a/utils/download.go +++ b/utils/download.go @@ -52,3 +52,27 @@ func IsValidUrl(uri string) bool { return true } + +// DetectFileContentType detects the file type by reading MIME type information of the file content. +func DetectFileContentType(fname string) (interface{}, error) { + file, err := os.Open(fname) + if err != nil { + return nil, err + } + defer file.Close() + + // Only the first 512 bytes are used to sniff the content type. + buffer := make([]byte, 512) + _, err = file.Read(buffer) + if err != nil { + return nil, err + } + + // Reset the read pointer if necessary. + file.Seek(0, 0) + + // Always returns a valid content-type and "application/octet-stream" if no others seemed to match. + contentType := http.DetectContentType(buffer) + + return string(contentType), nil +} diff --git a/utils/download_test.go b/utils/download_test.go new file mode 100644 index 0000000..96b79da --- /dev/null +++ b/utils/download_test.go @@ -0,0 +1,38 @@ +package utils + +import ( + "path/filepath" + "strings" + "testing" +) + +func TestUtils_ShouldDownloadImage(t *testing.T) { + f, err := DownloadImage("https://raw.githubusercontent.com/esimov/caire/master/testdata/sample.jpg") + if err != nil { + t.Fatalf("could't download test file: %v", err) + } + + if !strings.Contains(f.Name(), "tmp") { + t.Errorf("The downloaded image should have been saved in a temporary folder") + } +} + +func TestUtils_ShouldBeValidUrl(t *testing.T) { + ok := IsValidUrl("https://github.com/esimov/caire/") + if !ok { + t.Errorf("A valid URL should have been provided") + } +} + +func TestUtils_ShouldDetectValidFileType(t *testing.T) { + sampleImg := filepath.Join("../testdata", "sample.jpg") + + ftype, err := DetectFileContentType(sampleImg) + if err != nil { + t.Fatalf("could not detect content type: %v", err) + } + + if !strings.Contains(ftype.(string), "image") { + t.Errorf("Content type expected to be of type image, got: %v", ftype) + } +} diff --git a/utils/utils.go b/utils/utils.go index 111cd74..0ea54e8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,8 +3,6 @@ package utils import ( "fmt" "math" - "net/http" - "os" "time" ) @@ -66,27 +64,3 @@ func FormatTime(d time.Duration) string { int64(d.Hours()/24), int64(remainingHours), int64(remainingMinutes), remainingSeconds) } - -// DetectFileContentType detects the file type by reading MIME type information of the file content. -func DetectFileContentType(fname string) (interface{}, error) { - file, err := os.Open(fname) - if err != nil { - return nil, err - } - defer file.Close() - - // Only the first 512 bytes are used to sniff the content type. - buffer := make([]byte, 512) - _, err = file.Read(buffer) - if err != nil { - return nil, err - } - - // Reset the read pointer if necessary. - file.Seek(0, 0) - - // Always returns a valid content-type and "application/octet-stream" if no others seemed to match. - contentType := http.DetectContentType(buffer) - - return string(contentType), nil -}