mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-11 16:22:26 +08:00
feat: add IsLink, FileMode, MiMeType funcs for file
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,5 +4,6 @@
|
|||||||
cryptor/*.txt
|
cryptor/*.txt
|
||||||
fileutil/*.txt
|
fileutil/*.txt
|
||||||
fileutil/*.zip
|
fileutil/*.zip
|
||||||
|
fileutil/*.link
|
||||||
fileutil/unzip/*
|
fileutil/unzip/*
|
||||||
cryptor/*.pem
|
cryptor/*.pem
|
||||||
@@ -9,7 +9,9 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -240,3 +242,57 @@ func UnZip(zipFile string, destPath string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsLink checks if a file is symbol link or not
|
||||||
|
func IsLink(path string) bool {
|
||||||
|
fi, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return fi.Mode()&os.ModeSymlink != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileMode return file's mode and permission
|
||||||
|
func FileMode(path string) (fs.FileMode, error) {
|
||||||
|
fi, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return fi.Mode(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MiMeType return file mime type
|
||||||
|
// file should be string or *os.File
|
||||||
|
func MiMeType(file interface{}) string {
|
||||||
|
var mediatype string
|
||||||
|
|
||||||
|
readBuffer := func(f *os.File) ([]byte, error) {
|
||||||
|
buffer := make([]byte, 512)
|
||||||
|
_, err := f.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buffer, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if filePath, ok := file.(string); ok {
|
||||||
|
f, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return mediatype
|
||||||
|
}
|
||||||
|
buffer, err := readBuffer(f)
|
||||||
|
if err != nil {
|
||||||
|
return mediatype
|
||||||
|
}
|
||||||
|
return http.DetectContentType(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f, ok := file.(*os.File); ok {
|
||||||
|
buffer, err := readBuffer(f)
|
||||||
|
if err != nil {
|
||||||
|
return mediatype
|
||||||
|
}
|
||||||
|
return http.DetectContentType(buffer)
|
||||||
|
}
|
||||||
|
return mediatype
|
||||||
|
}
|
||||||
|
|||||||
@@ -181,3 +181,53 @@ func TestZipAndUnZip(t *testing.T) {
|
|||||||
os.Remove(zipFile)
|
os.Remove(zipFile)
|
||||||
os.RemoveAll(unZipPath)
|
os.RemoveAll(unZipPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileMode(t *testing.T) {
|
||||||
|
srcFile := "./text.txt"
|
||||||
|
CreateFile(srcFile)
|
||||||
|
|
||||||
|
mode, err := FileMode(srcFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
t.Log(mode)
|
||||||
|
|
||||||
|
os.Remove(srcFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsLink(t *testing.T) {
|
||||||
|
srcFile := "./text.txt"
|
||||||
|
CreateFile(srcFile)
|
||||||
|
|
||||||
|
linkFile := "./text.link"
|
||||||
|
if !IsExist(linkFile) {
|
||||||
|
_ = os.Symlink(srcFile, linkFile)
|
||||||
|
}
|
||||||
|
if !IsLink(linkFile) {
|
||||||
|
internal.LogFailedTestInfo(t, "IsLink", linkFile, "true", "false")
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsLink("./file.go") {
|
||||||
|
internal.LogFailedTestInfo(t, "IsLink", "./file.go", "false", "true")
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
os.Remove(srcFile)
|
||||||
|
os.Remove(linkFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMiMeType(t *testing.T) {
|
||||||
|
mt1 := MiMeType("./file.go")
|
||||||
|
expected := "text/plain; charset=utf-8"
|
||||||
|
|
||||||
|
if mt1 != expected {
|
||||||
|
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt1)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
f, _ := os.Open("./file.go")
|
||||||
|
mt2 := MiMeType(f)
|
||||||
|
if mt2 != expected {
|
||||||
|
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt2)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user