|
|
package operaSystem
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
"encoding/base64"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func (fl *OperaSystem) ByteToBase64(b []byte) string {
|
|
|
return base64.StdEncoding.EncodeToString(b)
|
|
|
}
|
|
|
|
|
|
// Base64ToByte base64 string to byte
|
|
|
// baseStr parameter is base64 string
|
|
|
func (fl *OperaSystem) Base64ToByte(baseStr string) ([]byte, error) {
|
|
|
return base64.StdEncoding.DecodeString(string(baseStr))
|
|
|
}
|
|
|
|
|
|
// ByteToFile The byte array is converted into a file and landed
|
|
|
// The parameter b is the bate array filePath file path
|
|
|
func (fl *OperaSystem) ByteToFile(b []byte, filePath string) error {
|
|
|
//Open file Create a file if there is no file
|
|
|
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
_, err = file.Write(b)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
defer func(file *os.File) {
|
|
|
err := file.Close()
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
}(file)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (fl *OperaSystem) FileName(filePath string) string {
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
if err == nil {
|
|
|
return fileInfo.Name()
|
|
|
}
|
|
|
return ""
|
|
|
} // FileName
|
|
|
|
|
|
func (fl *OperaSystem) FileSize(filePath string) int64 {
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
if err == nil {
|
|
|
return fileInfo.Size()
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
func (fl *OperaSystem) FileModTime(filePath string) time.Time {
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
if err == nil {
|
|
|
return fileInfo.ModTime()
|
|
|
}
|
|
|
return time.Time{}
|
|
|
}
|
|
|
|
|
|
func (fl *OperaSystem) FileMode(filePath string) os.FileMode {
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
if err == nil {
|
|
|
return fileInfo.Mode()
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
func (fl *OperaSystem) IsDir(filePath string) bool {
|
|
|
fileInfo, err := os.Stat(filePath)
|
|
|
if err == nil {
|
|
|
return fileInfo.IsDir()
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
// Exist 判断文件是否存在
|
|
|
func (tf *OperaSystem) FileExist(path string) bool {
|
|
|
_, err := os.Lstat(path)
|
|
|
return !os.IsNotExist(err)
|
|
|
}
|
|
|
|
|
|
// RemoveSuffix 删除文件后缀
|
|
|
func (tf *OperaSystem) RemoveSuffix(path string) string {
|
|
|
suffix := filepath.Ext(path)
|
|
|
if suffix != "" {
|
|
|
return strings.Replace(path, suffix, "", -1)
|
|
|
}
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
// RemovePrefix 删除文件前缀
|
|
|
func (tf *OperaSystem) RemovePrefix(path string) string {
|
|
|
prefix := filepath.Ext(path)
|
|
|
if prefix != "" {
|
|
|
return prefix[1:]
|
|
|
}
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
// FileAppendString 将String写入文件,追加模式
|
|
|
func (tf *OperaSystem) FileAppendString(content string, path string) (*os.File, error) {
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer file.Close()
|
|
|
write := bufio.NewWriter(file)
|
|
|
write.WriteString(content)
|
|
|
write.Flush()
|
|
|
return file, nil
|
|
|
}
|