-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: fsutil - add UpdateContents() and add more unit test cases
- Loading branch information
Showing
9 changed files
with
211 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package fsutil | ||
|
||
import ( | ||
"io" | ||
"mime" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// refer https://www.freeformatter.com/mime-types-list.html | ||
var builtinMimeTypes = map[string]string{ | ||
".xml": "application/xml", | ||
} | ||
|
||
func init() { | ||
// register builtin mime types | ||
for ext, mimeTyp := range builtinMimeTypes { | ||
_ = mime.AddExtensionType(ext, mimeTyp) | ||
} | ||
} | ||
|
||
// ExtsByMimeType returns the extensions known to be associated with the MIME type typ. | ||
// | ||
// returns like: [".html"] [".jpg", ".jpeg] | ||
func ExtsByMimeType(mimeTyp string) ([]string, error) { | ||
return mime.ExtensionsByType(mimeTyp) | ||
} | ||
|
||
// ExtByMimeType returns an extension known to be associated with the MIME type typ. | ||
// | ||
// allow with a default ext on not found. | ||
func ExtByMimeType(mimeTyp, defExt string) (string, error) { | ||
ss, err := mime.ExtensionsByType(mimeTyp) | ||
if err != nil { | ||
if defExt != "" { | ||
return defExt, nil | ||
} | ||
return "", err | ||
} | ||
|
||
if len(ss) == 0 { | ||
return defExt, nil | ||
} | ||
|
||
// always return the best match | ||
for _, ext := range ss { | ||
if strings.Index(mimeTyp, ext[1:]) > 0 { | ||
return ext, nil | ||
} | ||
} | ||
return ss[0], nil | ||
} | ||
|
||
// DetectMime detect file mime type. alias of MimeType() | ||
func DetectMime(path string) string { | ||
return MimeType(path) | ||
} | ||
|
||
// MimeType get File Mime Type name. eg "image/png" | ||
func MimeType(path string) (mime string) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return | ||
} | ||
return ReaderMimeType(file) | ||
} | ||
|
||
// ReaderMimeType get the io.Reader mimeType | ||
// | ||
// Usage: | ||
// | ||
// file, err := os.Open(filepath) | ||
// if err != nil { | ||
// return | ||
// } | ||
// mime := ReaderMimeType(file) | ||
func ReaderMimeType(r io.Reader) (mime string) { | ||
var buf [MimeSniffLen]byte | ||
n, _ := io.ReadFull(r, buf[:]) | ||
if n == 0 { | ||
return "" | ||
} | ||
|
||
return http.DetectContentType(buf[:n]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package fsutil_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/fsutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestExtByMimeType(t *testing.T) { | ||
tests := []struct { | ||
mime string | ||
ok bool | ||
exts []string | ||
ext string | ||
}{ | ||
{"application/json", true, []string{".json"}, ".json"}, | ||
{"application/xml", true, []string{".xml"}, ".xml"}, | ||
{"application/x-yaml", true, []string{".yaml", ".yml"}, ".yaml"}, | ||
{"text/plain; charset=utf-8", true, []string{".asc", ".txt"}, ".txt"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
es, err := fsutil.ExtsByMimeType(tt.mime) | ||
if tt.ok { | ||
assert.NoErr(t, err) | ||
assert.Eq(t, tt.exts, es) | ||
} else { | ||
assert.Err(t, err) | ||
assert.Empty(t, es) | ||
} | ||
|
||
ext, err := fsutil.ExtByMimeType(tt.mime, "") | ||
if tt.ok { | ||
assert.NoErr(t, err) | ||
assert.Eq(t, tt.ext, ext) | ||
} else { | ||
assert.Err(t, err) | ||
assert.Empty(t, ext) | ||
} | ||
} | ||
|
||
ext, err := fsutil.ExtByMimeType("application/not-exists", "default") | ||
assert.NoErr(t, err) | ||
assert.Eq(t, "default", ext) | ||
} | ||
|
||
func TestMimeType(t *testing.T) { | ||
assert.Eq(t, "", fsutil.DetectMime("")) | ||
assert.Eq(t, "", fsutil.MimeType("not-exist")) | ||
assert.Eq(t, "image/jpeg", fsutil.MimeType("testdata/test.jpg")) | ||
|
||
buf := new(bytes.Buffer) | ||
buf.Write([]byte("\xFF\xD8\xFF")) | ||
assert.Eq(t, "image/jpeg", fsutil.ReaderMimeType(buf)) | ||
buf.Reset() | ||
|
||
buf.Write([]byte("text")) | ||
assert.Eq(t, "text/plain; charset=utf-8", fsutil.ReaderMimeType(buf)) | ||
buf.Reset() | ||
|
||
buf.Write([]byte("")) | ||
assert.Eq(t, "", fsutil.ReaderMimeType(buf)) | ||
buf.Reset() | ||
|
||
assert.True(t, fsutil.IsImageFile("testdata/test.jpg")) | ||
assert.False(t, fsutil.IsImageFile("testdata/not-exists")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters