test: extended test cases with the utility methods

This commit is contained in:
esimov
2021-12-02 16:25:36 +02:00
parent 6e3ecc2fc0
commit b7332ae2bb
3 changed files with 62 additions and 26 deletions

View File

@@ -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
}