2022-08-25 17:54:52 +08:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2023-03-13 13:38:29 +08:00
|
|
|
"archive/zip"
|
2023-02-21 19:06:24 +08:00
|
|
|
"bufio"
|
2022-08-30 17:59:59 +08:00
|
|
|
"context"
|
2023-12-05 16:30:10 +08:00
|
|
|
"crypto/tls"
|
2022-09-14 19:09:39 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-04-26 22:18:16 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-06-01 17:05:14 +08:00
|
|
|
http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
|
2023-06-01 13:51:11 +08:00
|
|
|
cZip "github.com/klauspost/compress/zip"
|
|
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
|
|
"golang.org/x/text/transform"
|
2022-08-25 17:54:52 +08:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2022-09-05 16:25:26 +08:00
|
|
|
"net/http"
|
2022-08-25 17:54:52 +08:00
|
|
|
"os"
|
2022-09-06 15:46:46 +08:00
|
|
|
"path"
|
2022-08-30 17:59:59 +08:00
|
|
|
"path/filepath"
|
2022-09-14 19:09:39 +08:00
|
|
|
"strconv"
|
2023-03-13 13:38:29 +08:00
|
|
|
"strings"
|
2022-09-09 18:10:41 +08:00
|
|
|
"sync"
|
2022-09-14 19:09:39 +08:00
|
|
|
"time"
|
2023-01-30 21:05:20 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/mholt/archiver/v4"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/afero"
|
2022-08-25 17:54:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileOp struct {
|
|
|
|
Fs afero.Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileOp() FileOp {
|
|
|
|
return FileOp{
|
|
|
|
Fs: afero.NewOsFs(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 18:16:56 +08:00
|
|
|
func (f FileOp) OpenFile(dst string) (fs.File, error) {
|
|
|
|
return f.Fs.Open(dst)
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:47:47 +08:00
|
|
|
func (f FileOp) GetContent(dst string) ([]byte, error) {
|
|
|
|
afs := &afero.Afero{Fs: f.Fs}
|
|
|
|
cByte, err := afs.ReadFile(dst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cByte, nil
|
|
|
|
}
|
|
|
|
|
2022-08-25 17:54:52 +08:00
|
|
|
func (f FileOp) CreateDir(dst string, mode fs.FileMode) error {
|
|
|
|
return f.Fs.MkdirAll(dst, mode)
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:00:51 +08:00
|
|
|
func (f FileOp) CreateFile(dst string) error {
|
|
|
|
if _, err := f.Fs.Create(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileOp) LinkFile(source string, dst string, isSymlink bool) error {
|
|
|
|
if isSymlink {
|
|
|
|
osFs := afero.OsFs{}
|
|
|
|
return osFs.SymlinkIfPossible(source, dst)
|
|
|
|
} else {
|
|
|
|
return os.Link(source, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 18:48:03 +08:00
|
|
|
func (f FileOp) DeleteDir(dst string) error {
|
|
|
|
return f.Fs.RemoveAll(dst)
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:26:36 +08:00
|
|
|
func (f FileOp) Stat(dst string) bool {
|
|
|
|
info, _ := f.Fs.Stat(dst)
|
2022-09-09 11:20:02 +08:00
|
|
|
return info != nil
|
2022-08-29 15:26:36 +08:00
|
|
|
}
|
|
|
|
|
2022-08-25 18:48:03 +08:00
|
|
|
func (f FileOp) DeleteFile(dst string) error {
|
|
|
|
return f.Fs.Remove(dst)
|
|
|
|
}
|
|
|
|
|
2023-10-18 17:02:24 +08:00
|
|
|
func (f FileOp) Delete(dst string) error {
|
|
|
|
return os.RemoveAll(dst)
|
|
|
|
}
|
|
|
|
|
2023-12-28 17:51:39 +08:00
|
|
|
func (f FileOp) CleanDir(dst string) error {
|
|
|
|
return cmd.ExecCmd(fmt.Sprintf("rm -rf %s/*", dst))
|
|
|
|
}
|
|
|
|
|
2023-10-18 17:02:24 +08:00
|
|
|
func (f FileOp) RmRf(dst string) error {
|
|
|
|
return cmd.ExecCmd(fmt.Sprintf("rm -rf %s", dst))
|
|
|
|
}
|
|
|
|
|
2022-08-25 17:54:52 +08:00
|
|
|
func (f FileOp) WriteFile(dst string, in io.Reader, mode fs.FileMode) error {
|
|
|
|
file, err := f.Fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(file, in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = file.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-29 15:26:36 +08:00
|
|
|
|
2023-02-21 19:06:24 +08:00
|
|
|
func (f FileOp) SaveFile(dst string, content string, mode fs.FileMode) error {
|
|
|
|
if !f.Stat(path.Dir(dst)) {
|
|
|
|
_ = f.CreateDir(path.Dir(dst), mode.Perm())
|
|
|
|
}
|
|
|
|
file, err := f.Fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
2023-04-26 22:18:16 +08:00
|
|
|
_, _ = write.WriteString(content)
|
2023-02-21 19:06:24 +08:00
|
|
|
write.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:26:36 +08:00
|
|
|
func (f FileOp) Chmod(dst string, mode fs.FileMode) error {
|
|
|
|
return f.Fs.Chmod(dst, mode)
|
|
|
|
}
|
2022-08-30 17:59:59 +08:00
|
|
|
|
2023-03-31 12:16:14 +08:00
|
|
|
func (f FileOp) Chown(dst string, uid int, gid int) error {
|
|
|
|
return f.Fs.Chown(dst, uid, gid)
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:18:16 +08:00
|
|
|
func (f FileOp) ChownR(dst string, uid string, gid string, sub bool) error {
|
2023-09-06 11:32:11 +08:00
|
|
|
cmdStr := fmt.Sprintf(`chown %s:%s "%s"`, uid, gid, dst)
|
2023-04-26 22:18:16 +08:00
|
|
|
if sub {
|
2023-09-06 11:32:11 +08:00
|
|
|
cmdStr = fmt.Sprintf(`chown -R %s:%s "%s"`, uid, gid, dst)
|
2023-04-27 13:54:15 +08:00
|
|
|
}
|
|
|
|
if cmd.HasNoPasswordSudo() {
|
|
|
|
cmdStr = fmt.Sprintf("sudo %s", cmdStr)
|
2023-04-26 22:18:16 +08:00
|
|
|
}
|
|
|
|
if msg, err := cmd.ExecWithTimeOut(cmdStr, 2*time.Second); err != nil {
|
|
|
|
if msg != "" {
|
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-02 18:38:50 +08:00
|
|
|
func (f FileOp) ChmodR(dst string, mode int64, sub bool) error {
|
|
|
|
cmdStr := fmt.Sprintf(`chmod %v "%s"`, fmt.Sprintf("%04o", mode), dst)
|
|
|
|
if sub {
|
|
|
|
cmdStr = fmt.Sprintf(`chmod -R %v "%s"`, fmt.Sprintf("%04o", mode), dst)
|
|
|
|
}
|
2023-04-26 22:18:16 +08:00
|
|
|
if cmd.HasNoPasswordSudo() {
|
|
|
|
cmdStr = fmt.Sprintf("sudo %s", cmdStr)
|
|
|
|
}
|
|
|
|
if msg, err := cmd.ExecWithTimeOut(cmdStr, 2*time.Second); err != nil {
|
|
|
|
if msg != "" {
|
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-03 22:22:40 +08:00
|
|
|
func (f FileOp) Rename(oldName string, newName string) error {
|
|
|
|
return f.Fs.Rename(oldName, newName)
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:09:39 +08:00
|
|
|
type WriteCounter struct {
|
|
|
|
Total uint64
|
|
|
|
Written uint64
|
|
|
|
Key string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Process struct {
|
|
|
|
Total uint64 `json:"total"`
|
|
|
|
Written uint64 `json:"written"`
|
|
|
|
Percent float64 `json:"percent"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteCounter) Write(p []byte) (n int, err error) {
|
|
|
|
n = len(p)
|
|
|
|
w.Written += uint64(n)
|
|
|
|
w.SaveProcess()
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteCounter) SaveProcess() {
|
2023-03-10 18:28:44 +08:00
|
|
|
percentValue := 0.0
|
|
|
|
if w.Total > 0 {
|
|
|
|
percent := float64(w.Written) / float64(w.Total) * 100
|
|
|
|
percentValue, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", percent), 64)
|
|
|
|
}
|
2022-09-14 19:09:39 +08:00
|
|
|
process := Process{
|
|
|
|
Total: w.Total,
|
|
|
|
Written: w.Written,
|
|
|
|
Percent: percentValue,
|
|
|
|
Name: w.Name,
|
|
|
|
}
|
|
|
|
by, _ := json.Marshal(process)
|
|
|
|
if percentValue < 100 {
|
|
|
|
if err := global.CACHE.Set(w.Key, string(by)); err != nil {
|
|
|
|
global.LOG.Errorf("save cache error, err %s", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := global.CACHE.SetWithTTL(w.Key, string(by), time.Second*time.Duration(10)); err != nil {
|
|
|
|
global.LOG.Errorf("save cache error, err %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 14:42:11 +08:00
|
|
|
func (f FileOp) DownloadFileWithProcess(url, dst, key string, ignoreCertificate bool) error {
|
|
|
|
client := &http.Client{}
|
|
|
|
if ignoreCertificate {
|
|
|
|
client.Transport = &http.Transport{
|
2023-12-05 16:30:10 +08:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
2023-12-14 14:42:11 +08:00
|
|
|
}
|
2023-12-05 16:30:10 +08:00
|
|
|
}
|
2023-03-10 18:28:44 +08:00
|
|
|
request, err := http.NewRequest("GET", url, nil)
|
2022-09-05 16:25:26 +08:00
|
|
|
if err != nil {
|
2023-03-10 18:28:44 +08:00
|
|
|
return nil
|
2022-09-14 19:09:39 +08:00
|
|
|
}
|
2023-03-10 18:28:44 +08:00
|
|
|
request.Header.Set("Accept-Encoding", "identity")
|
|
|
|
resp, err := client.Do(request)
|
2022-09-14 19:09:39 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get download file [%s] error, err %s", dst, err.Error())
|
2023-02-17 17:19:35 +08:00
|
|
|
return err
|
2022-09-05 16:25:26 +08:00
|
|
|
}
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
2022-09-14 19:09:39 +08:00
|
|
|
global.LOG.Errorf("create download file [%s] error, err %s", dst, err.Error())
|
2023-02-17 17:19:35 +08:00
|
|
|
return err
|
2022-09-05 16:25:26 +08:00
|
|
|
}
|
|
|
|
go func() {
|
2022-09-14 19:09:39 +08:00
|
|
|
counter := &WriteCounter{}
|
|
|
|
counter.Key = key
|
2023-03-10 18:28:44 +08:00
|
|
|
if resp.ContentLength > 0 {
|
|
|
|
counter.Total = uint64(resp.ContentLength)
|
|
|
|
}
|
2022-09-14 19:09:39 +08:00
|
|
|
counter.Name = filepath.Base(dst)
|
|
|
|
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
|
2022-09-05 16:25:26 +08:00
|
|
|
global.LOG.Errorf("save download file [%s] error, err %s", dst, err.Error())
|
|
|
|
}
|
2022-09-14 19:09:39 +08:00
|
|
|
out.Close()
|
|
|
|
resp.Body.Close()
|
2023-03-10 18:28:44 +08:00
|
|
|
|
|
|
|
value, err := global.CACHE.Get(counter.Key)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get cache error,err %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
process := &Process{}
|
|
|
|
_ = json.Unmarshal(value, process)
|
|
|
|
process.Percent = 100
|
|
|
|
process.Name = counter.Name
|
|
|
|
process.Total = process.Written
|
|
|
|
by, _ := json.Marshal(process)
|
|
|
|
if err := global.CACHE.SetWithTTL(counter.Key, string(by), time.Second*time.Duration(10)); err != nil {
|
|
|
|
global.LOG.Errorf("save cache error, err %s", err.Error())
|
|
|
|
}
|
2022-09-05 16:25:26 +08:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:48:55 +08:00
|
|
|
func (f FileOp) DownloadFile(url, dst string) error {
|
2023-06-01 17:05:14 +08:00
|
|
|
resp, err := http2.GetHttpRes(url)
|
2022-10-14 14:48:55 +08:00
|
|
|
if err != nil {
|
2023-06-01 17:05:14 +08:00
|
|
|
return err
|
2022-10-14 14:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
2023-01-30 21:05:20 +08:00
|
|
|
return fmt.Errorf("create download file [%s] error, err %s", dst, err.Error())
|
2022-10-14 14:48:55 +08:00
|
|
|
}
|
2023-01-30 21:05:20 +08:00
|
|
|
defer out.Close()
|
2022-10-14 14:48:55 +08:00
|
|
|
|
|
|
|
if _, err = io.Copy(out, resp.Body); err != nil {
|
2023-01-30 21:05:20 +08:00
|
|
|
return fmt.Errorf("save download file [%s] error, err %s", dst, err.Error())
|
2022-10-14 14:48:55 +08:00
|
|
|
}
|
|
|
|
out.Close()
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:27:19 +08:00
|
|
|
func (f FileOp) Cut(oldPaths []string, dst, name string, cover bool) error {
|
2022-09-06 10:35:35 +08:00
|
|
|
for _, p := range oldPaths {
|
2023-11-01 18:27:19 +08:00
|
|
|
var dstPath string
|
|
|
|
if name != "" {
|
|
|
|
dstPath = filepath.Join(dst, name)
|
2023-11-06 14:14:31 +08:00
|
|
|
if f.Stat(dstPath) {
|
|
|
|
dstPath = dst
|
|
|
|
}
|
2023-11-01 18:27:19 +08:00
|
|
|
} else {
|
|
|
|
base := filepath.Base(p)
|
|
|
|
dstPath = filepath.Join(dst, base)
|
|
|
|
}
|
|
|
|
coverFlag := ""
|
|
|
|
if cover {
|
|
|
|
coverFlag = "-f"
|
|
|
|
}
|
2023-11-10 16:38:07 +08:00
|
|
|
|
|
|
|
cmdStr := fmt.Sprintf(`mv %s "%s" "%s"`, coverFlag, p, dstPath)
|
2023-11-01 18:27:19 +08:00
|
|
|
if err := cmd.ExecCmd(cmdStr); err != nil {
|
2022-09-06 10:35:35 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:43:32 +08:00
|
|
|
func (f FileOp) Mv(oldPath, dstPath string) error {
|
2024-01-20 22:17:37 +08:00
|
|
|
cmdStr := fmt.Sprintf(`mv "%s" "%s"`, oldPath, dstPath)
|
2023-11-05 21:43:32 +08:00
|
|
|
if err := cmd.ExecCmd(cmdStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:46:46 +08:00
|
|
|
func (f FileOp) Copy(src, dst string) error {
|
|
|
|
if src = path.Clean("/" + src); src == "" {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
if dst = path.Clean("/" + dst); dst == "" {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
if src == "/" || dst == "/" {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
if dst == src {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
info, err := f.Fs.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return f.CopyDir(src, dst)
|
|
|
|
}
|
|
|
|
return f.CopyFile(src, dst)
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:27:19 +08:00
|
|
|
func (f FileOp) CopyAndReName(src, dst, name string, cover bool) error {
|
|
|
|
if src = path.Clean("/" + src); src == "" {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
if dst = path.Clean("/" + dst); dst == "" {
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
if src == "/" || dst == "/" {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
if dst == src {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
srcInfo, err := f.Fs.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if srcInfo.IsDir() {
|
|
|
|
dstPath := dst
|
|
|
|
if name != "" && !cover {
|
|
|
|
dstPath = filepath.Join(dst, name)
|
|
|
|
}
|
2023-11-10 16:38:07 +08:00
|
|
|
return cmd.ExecCmd(fmt.Sprintf(`cp -rf "%s" "%s"`, src, dstPath))
|
2023-11-01 18:27:19 +08:00
|
|
|
} else {
|
|
|
|
dstPath := filepath.Join(dst, name)
|
|
|
|
if cover {
|
|
|
|
dstPath = dst
|
|
|
|
}
|
2023-11-10 16:38:07 +08:00
|
|
|
return cmd.ExecCmd(fmt.Sprintf(`cp -f "%s" "%s"`, src, dstPath))
|
2023-11-01 18:27:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:46:46 +08:00
|
|
|
func (f FileOp) CopyDir(src, dst string) error {
|
|
|
|
srcInfo, err := f.Fs.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-04 12:47:19 +08:00
|
|
|
dstDir := filepath.Join(dst, srcInfo.Name())
|
2023-10-16 11:08:15 +08:00
|
|
|
if err = f.Fs.MkdirAll(dstDir, srcInfo.Mode()); err != nil {
|
2022-09-06 15:46:46 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-11-10 16:38:07 +08:00
|
|
|
return cmd.ExecCmd(fmt.Sprintf(`cp -rf "%s" "%s"`, src, dst+"/"))
|
2022-09-06 15:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileOp) CopyFile(src, dst string) error {
|
2023-11-10 16:38:07 +08:00
|
|
|
dst = filepath.Clean(dst) + string(filepath.Separator)
|
|
|
|
return cmd.ExecCmd(fmt.Sprintf(`cp -f "%s" "%s"`, src, dst+"/"))
|
2022-09-06 15:46:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 18:10:41 +08:00
|
|
|
func (f FileOp) GetDirSize(path string) (float64, error) {
|
|
|
|
var m sync.Map
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go ScanDir(f.Fs, path, &m, &wg)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
var dirSize float64
|
|
|
|
m.Range(func(k, v interface{}) bool {
|
|
|
|
dirSize = dirSize + v.(float64)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return dirSize, nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:59:59 +08:00
|
|
|
func getFormat(cType CompressType) archiver.CompressedArchive {
|
|
|
|
format := archiver.CompressedArchive{}
|
|
|
|
switch cType {
|
|
|
|
case Tar:
|
|
|
|
format.Archival = archiver.Tar{}
|
|
|
|
case TarGz, Gz:
|
|
|
|
format.Compression = archiver.Gz{}
|
|
|
|
format.Archival = archiver.Tar{}
|
2023-11-03 18:18:12 +08:00
|
|
|
case SdkTarGz:
|
|
|
|
format.Compression = archiver.Gz{}
|
|
|
|
format.Archival = archiver.Tar{}
|
2023-11-29 20:26:07 +08:00
|
|
|
case SdkZip, Zip:
|
2023-03-13 13:38:29 +08:00
|
|
|
format.Archival = archiver.Zip{
|
|
|
|
Compression: zip.Deflate,
|
|
|
|
}
|
2022-08-30 17:59:59 +08:00
|
|
|
case Bz2:
|
|
|
|
format.Compression = archiver.Bz2{}
|
|
|
|
format.Archival = archiver.Tar{}
|
|
|
|
case Xz:
|
|
|
|
format.Compression = archiver.Xz{}
|
|
|
|
format.Archival = archiver.Tar{}
|
|
|
|
}
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileOp) Compress(srcRiles []string, dst string, name string, cType CompressType) error {
|
|
|
|
format := getFormat(cType)
|
|
|
|
|
|
|
|
fileMaps := make(map[string]string, len(srcRiles))
|
|
|
|
for _, s := range srcRiles {
|
|
|
|
base := filepath.Base(s)
|
|
|
|
fileMaps[s] = base
|
|
|
|
}
|
|
|
|
|
2022-10-12 18:57:22 +08:00
|
|
|
if !f.Stat(dst) {
|
|
|
|
_ = f.CreateDir(dst, 0755)
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:59:59 +08:00
|
|
|
files, err := archiver.FilesFromDisk(nil, fileMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dstFile := filepath.Join(dst, name)
|
|
|
|
out, err := f.Fs.Create(dstFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-13 13:38:29 +08:00
|
|
|
switch cType {
|
|
|
|
case Zip:
|
2023-11-29 20:26:07 +08:00
|
|
|
if err := ZipFile(files, out); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_ = f.DeleteFile(dstFile)
|
2023-10-16 16:34:15 +08:00
|
|
|
return NewZipArchiver().Compress(srcRiles, dstFile)
|
2023-03-13 13:38:29 +08:00
|
|
|
default:
|
|
|
|
err = format.Archive(context.Background(), out, files)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.DeleteFile(dstFile)
|
|
|
|
return err
|
|
|
|
}
|
2022-08-30 17:59:59 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:48:19 +08:00
|
|
|
func isIgnoreFile(name string) bool {
|
|
|
|
return strings.HasPrefix(name, "__MACOSX") || strings.HasSuffix(name, ".DS_Store") || strings.HasPrefix(name, "._")
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:51:11 +08:00
|
|
|
func decodeGBK(input string) (string, error) {
|
|
|
|
decoder := simplifiedchinese.GBK.NewDecoder()
|
|
|
|
decoded, _, err := transform.String(decoder, input)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return decoded, nil
|
|
|
|
}
|
|
|
|
|
2023-11-29 20:26:07 +08:00
|
|
|
func (f FileOp) decompressWithSDK(srcFile string, dst string, cType CompressType) error {
|
|
|
|
format := getFormat(cType)
|
|
|
|
handler := func(ctx context.Context, archFile archiver.File) error {
|
|
|
|
info := archFile.FileInfo
|
|
|
|
if isIgnoreFile(archFile.Name()) {
|
|
|
|
return nil
|
2023-05-05 15:50:52 +08:00
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
fileName := archFile.NameInArchive
|
|
|
|
var err error
|
|
|
|
if header, ok := archFile.Header.(cZip.FileHeader); ok {
|
|
|
|
if header.NonUTF8 && header.Flags == 0 {
|
|
|
|
fileName, err = decodeGBK(fileName)
|
|
|
|
if err != nil {
|
2023-06-01 13:51:11 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-08-28 22:37:48 +08:00
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
}
|
|
|
|
filePath := filepath.Join(dst, fileName)
|
|
|
|
if archFile.FileInfo.IsDir() {
|
|
|
|
if err := f.Fs.MkdirAll(filePath, info.Mode()); err != nil {
|
2023-10-16 15:48:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-11-29 20:26:07 +08:00
|
|
|
} else {
|
|
|
|
parentDir := path.Dir(filePath)
|
|
|
|
if !f.Stat(parentDir) {
|
|
|
|
if err := f.Fs.MkdirAll(parentDir, info.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fr, err := archFile.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-30 17:59:59 +08:00
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
defer fr.Close()
|
|
|
|
fw, err := f.Fs.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, info.Mode())
|
2022-08-30 17:59:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
defer fw.Close()
|
|
|
|
if _, err := io.Copy(fw, fr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-08-30 17:59:59 +08:00
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
input, err := f.Fs.Open(srcFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return format.Extract(context.Background(), input, nil, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileOp) Decompress(srcFile string, dst string, cType CompressType) error {
|
|
|
|
if err := f.decompressWithSDK(srcFile, dst, cType); err != nil {
|
|
|
|
if cType == Tar || cType == Zip {
|
|
|
|
shellArchiver, err := NewShellArchiver(cType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return shellArchiver.Extract(srcFile, dst)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-30 17:59:59 +08:00
|
|
|
}
|
2022-10-14 14:48:55 +08:00
|
|
|
|
|
|
|
func (f FileOp) Backup(srcFile string) (string, error) {
|
|
|
|
backupPath := srcFile + "_bak"
|
|
|
|
info, _ := f.Fs.Stat(backupPath)
|
|
|
|
if info != nil {
|
|
|
|
if info.IsDir() {
|
2022-11-18 18:02:14 +08:00
|
|
|
_ = f.DeleteDir(backupPath)
|
2022-10-14 14:48:55 +08:00
|
|
|
} else {
|
2022-11-18 18:02:14 +08:00
|
|
|
_ = f.DeleteFile(backupPath)
|
2022-10-14 14:48:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := f.Rename(srcFile, backupPath); err != nil {
|
|
|
|
return backupPath, err
|
|
|
|
}
|
2022-12-12 12:01:33 +08:00
|
|
|
|
|
|
|
return backupPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileOp) CopyAndBackup(src string) (string, error) {
|
|
|
|
backupPath := src + "_bak"
|
|
|
|
info, _ := f.Fs.Stat(backupPath)
|
|
|
|
if info != nil {
|
|
|
|
if info.IsDir() {
|
|
|
|
_ = f.DeleteDir(backupPath)
|
|
|
|
} else {
|
|
|
|
_ = f.DeleteFile(backupPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = f.CreateDir(backupPath, 0755)
|
|
|
|
if err := f.Copy(src, backupPath); err != nil {
|
|
|
|
return backupPath, err
|
|
|
|
}
|
2022-10-14 14:48:55 +08:00
|
|
|
return backupPath, nil
|
|
|
|
}
|
2023-11-29 20:26:07 +08:00
|
|
|
|
|
|
|
func ZipFile(files []archiver.File, dst afero.File) error {
|
|
|
|
zw := zip.NewWriter(dst)
|
|
|
|
defer zw.Close()
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
hdr, err := zip.FileInfoHeader(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-15 14:15:26 +08:00
|
|
|
hdr.Method = zip.Deflate
|
2023-11-29 20:26:07 +08:00
|
|
|
hdr.Name = file.NameInArchive
|
|
|
|
if file.IsDir() {
|
|
|
|
if !strings.HasSuffix(hdr.Name, "/") {
|
|
|
|
hdr.Name += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w, err := zw.CreateHeader(hdr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.LinkTarget != "" {
|
|
|
|
_, err = w.Write([]byte(filepath.ToSlash(file.LinkTarget)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileReader, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, fileReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|