mirror of
https://github.com/esimov/caire.git
synced 2025-10-13 04:13:49 +08:00
test: extended test cases with the utility methods
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user