123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package file
- import (
- "fmt"
- "io/ioutil"
- "mime/multipart"
- "os"
- "path"
- )
- func GetSize(f multipart.File) (int, error) {
- content, err := ioutil.ReadAll(f)
- return len(content), err
- }
- func GetExt(fileName string) string {
- return path.Ext(fileName)
- }
- func CheckNotExist(src string) bool {
- _, err := os.Stat(src)
- return os.IsNotExist(err)
- }
- func CheckPermission(src string) bool {
- _, err := os.Stat(src)
- return os.IsPermission(err)
- }
- func IsNotExistMkDir(src string) error {
- if notExist := CheckNotExist(src); notExist == true {
- if err := MkDir(src); err != nil {
- return err
- }
- }
- return nil
- }
- func MkDir(src string) error {
- err := os.MkdirAll(src, os.ModePerm)
- if err != nil {
- return err
- }
- return nil
- }
- func Open(name string, flag int, perm os.FileMode) (*os.File, error) {
- f, err := os.OpenFile(name, flag, perm)
- if err != nil {
- return nil, err
- }
- return f, nil
- }
- func MustOpen(fileName, filePath string) (*os.File, error) {
- dir, err := os.Getwd()
- if err != nil {
- return nil, fmt.Errorf("os.Getwd err: %v", err)
- }
- src := dir + "/" + filePath
- perm := CheckPermission(src)
- if perm == true {
- return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
- }
- err = IsNotExistMkDir(src)
- if err != nil {
- return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
- }
- f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
- if err != nil {
- return nil, fmt.Errorf("Fail to OpenFile :%v", err)
- }
- return f, nil
- }
|