mirror of
				https://github.com/duke-git/lancet.git
				synced 2025-10-31 02:36:36 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			325 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			325 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2021 dudaodong@gmail.com. All rights reserved.
 | |
| // Use of this source code is governed by MIT license.
 | |
| 
 | |
| // Package fileutil implements some basic functions for file operations
 | |
| package fileutil
 | |
| 
 | |
| import (
 | |
| 	"archive/zip"
 | |
| 	"bufio"
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 	"io"
 | |
| 	"io/fs"
 | |
| 	"io/ioutil"
 | |
| 	"net/http"
 | |
| 	"os"
 | |
| 	"path"
 | |
| 	"path/filepath"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| // IsExist checks if a file or directory exists
 | |
| func IsExist(path string) bool {
 | |
| 	_, err := os.Stat(path)
 | |
| 	if err == nil {
 | |
| 		return true
 | |
| 	}
 | |
| 	if errors.Is(err, os.ErrNotExist) {
 | |
| 		return false
 | |
| 	}
 | |
| 	return false
 | |
| }
 | |
| 
 | |
| // CreateFile create a file in path
 | |
| func CreateFile(path string) bool {
 | |
| 	file, err := os.Create(path)
 | |
| 	if err != nil {
 | |
| 		return false
 | |
| 	}
 | |
| 
 | |
| 	defer file.Close()
 | |
| 	return true
 | |
| }
 | |
| 
 | |
| // CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/
 | |
| func CreateDir(absPath string) error {
 | |
| 	return os.MkdirAll(path.Dir(absPath), os.ModePerm)
 | |
| }
 | |
| 
 | |
| // IsDir checks if the path is directory or not
 | |
| func IsDir(path string) bool {
 | |
| 	file, err := os.Stat(path)
 | |
| 	if err != nil {
 | |
| 		return false
 | |
| 	}
 | |
| 	return file.IsDir()
 | |
| }
 | |
| 
 | |
| // RemoveFile remove the path file
 | |
| func RemoveFile(path string) error {
 | |
| 	return os.Remove(path)
 | |
| }
 | |
| 
 | |
| // CopyFile copy src file to dest file
 | |
| func CopyFile(srcFilePath string, dstFilePath string) error {
 | |
| 	srcFile, err := os.Open(srcFilePath)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer srcFile.Close()
 | |
| 
 | |
| 	distFile, err := os.Create(dstFilePath)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer distFile.Close()
 | |
| 
 | |
| 	var tmp = make([]byte, 1024*4)
 | |
| 	for {
 | |
| 		n, err := srcFile.Read(tmp)
 | |
| 		distFile.Write(tmp[:n])
 | |
| 		if err != nil {
 | |
| 			if err == io.EOF {
 | |
| 				return nil
 | |
| 			}
 | |
| 			return err
 | |
| 		}
 | |
| 	}
 | |
| }
 | |
| 
 | |
| //ClearFile write empty string to path file
 | |
| func ClearFile(path string) error {
 | |
| 	f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer f.Close()
 | |
| 
 | |
| 	_, err = f.WriteString("")
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| //ReadFileToString return string of file content
 | |
| func ReadFileToString(path string) (string, error) {
 | |
| 	bytes, err := ioutil.ReadFile(path)
 | |
| 	if err != nil {
 | |
| 		return "", err
 | |
| 	}
 | |
| 	return string(bytes), nil
 | |
| }
 | |
| 
 | |
| // ReadFileByLine read file line by line
 | |
| func ReadFileByLine(path string) ([]string, error) {
 | |
| 	f, err := os.Open(path)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer f.Close()
 | |
| 
 | |
| 	result := make([]string, 0)
 | |
| 	buf := bufio.NewReader(f)
 | |
| 
 | |
| 	for {
 | |
| 		line, _, err := buf.ReadLine()
 | |
| 		l := string(line)
 | |
| 		if err == io.EOF {
 | |
| 			break
 | |
| 		}
 | |
| 		if err != nil {
 | |
| 			continue
 | |
| 		}
 | |
| 		result = append(result, l)
 | |
| 	}
 | |
| 
 | |
| 	return result, nil
 | |
| }
 | |
| 
 | |
| // ListFileNames return all file names in the path
 | |
| func ListFileNames(path string) ([]string, error) {
 | |
| 	if !IsExist(path) {
 | |
| 		return []string{}, nil
 | |
| 	}
 | |
| 
 | |
| 	fs, err := ioutil.ReadDir(path)
 | |
| 	if err != nil {
 | |
| 		return []string{}, err
 | |
| 	}
 | |
| 
 | |
| 	sz := len(fs)
 | |
| 	if sz == 0 {
 | |
| 		return []string{}, nil
 | |
| 	}
 | |
| 
 | |
| 	result := []string{}
 | |
| 	for i := 0; i < sz; i++ {
 | |
| 		if !fs[i].IsDir() {
 | |
| 			result = append(result, fs[i].Name())
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return result, nil
 | |
| }
 | |
| 
 | |
| // Zip create zip file, fpath could be a single file or a directory
 | |
| func Zip(fpath string, destPath string) error {
 | |
| 	zipFile, err := os.Create(destPath)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer zipFile.Close()
 | |
| 
 | |
| 	archive := zip.NewWriter(zipFile)
 | |
| 	defer archive.Close()
 | |
| 
 | |
| 	filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 
 | |
| 		header, err := zip.FileInfoHeader(info)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 
 | |
| 		header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/")
 | |
| 
 | |
| 		if info.IsDir() {
 | |
| 			header.Name += "/"
 | |
| 		} else {
 | |
| 			header.Method = zip.Deflate
 | |
| 		}
 | |
| 
 | |
| 		writer, err := archive.CreateHeader(header)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 
 | |
| 		if !info.IsDir() {
 | |
| 			file, err := os.Open(path)
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 			defer file.Close()
 | |
| 			_, err = io.Copy(writer, file)
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 		}
 | |
| 		return nil
 | |
| 	})
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // UnZip unzip the file and save it to destPath
 | |
| func UnZip(zipFile string, destPath string) error {
 | |
| 
 | |
| 	zipReader, err := zip.OpenReader(zipFile)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	defer zipReader.Close()
 | |
| 
 | |
| 	for _, f := range zipReader.File {
 | |
| 		path := filepath.Join(destPath, f.Name)
 | |
| 
 | |
| 		//issue#62: fix ZipSlip bug
 | |
| 		path, err := safeFilepathJoin(destPath, f.Name)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 
 | |
| 		if f.FileInfo().IsDir() {
 | |
| 			os.MkdirAll(path, os.ModePerm)
 | |
| 		} else {
 | |
| 			if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 
 | |
| 			inFile, err := f.Open()
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 			defer inFile.Close()
 | |
| 
 | |
| 			outFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 			defer outFile.Close()
 | |
| 
 | |
| 			_, err = io.Copy(outFile, inFile)
 | |
| 			if err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func safeFilepathJoin(path1, path2 string) (string, error) {
 | |
| 	relPath, err := filepath.Rel(".", path2)
 | |
| 	if err != nil || strings.HasPrefix(relPath, "..") {
 | |
| 		return "", fmt.Errorf("(zipslip) filepath is unsafe %q: %v", path2, err)
 | |
| 	}
 | |
| 	if path1 == "" {
 | |
| 		path1 = "."
 | |
| 	}
 | |
| 	return filepath.Join(path1, filepath.Join("/", relPath)), 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
 | |
| // param `file` should be string(file path) or *os.File
 | |
| func MiMeType(file any) 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
 | |
| }
 | 
