Skip to content

Commit

Permalink
feat: 使用新的压缩解压库
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Sep 14, 2024
1 parent ec5d8a0 commit 0efb1e4
Show file tree
Hide file tree
Showing 8 changed files with 617 additions and 157 deletions.
27 changes: 17 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/go-rat/chix v1.1.3
github.com/go-rat/gormstore v1.0.5
github.com/go-rat/sessions v1.0.8
github.com/go-rat/utils v1.0.3
github.com/go-resty/resty/v2 v2.14.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang-module/carbon/v2 v2.3.12
Expand All @@ -28,7 +29,7 @@ require (
github.com/libdns/libdns v0.2.2
github.com/libdns/tencentcloud v1.0.0
github.com/mholt/acmez/v2 v2.0.2
github.com/mholt/archiver/v3 v3.5.1
github.com/mholt/archiver/v4 v4.0.0-alpha.8
github.com/shirou/gopsutil v2.21.11+incompatible
github.com/spf13/cast v1.7.0
github.com/stretchr/testify v1.9.0
Expand All @@ -43,7 +44,11 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/andybalholm/brotli v1.0.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bodgit/plumbing v1.2.0 // indirect
github.com/bodgit/sevenzip v1.3.0 // indirect
github.com/bodgit/windows v1.0.0 // indirect
github.com/connesc/cipherio v0.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -56,16 +61,17 @@ require (
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-rat/securecookie v1.0.1 // indirect
github.com/go-rat/utils v1.0.2 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jaevor/go-nanoid v1.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.11.4 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
Expand All @@ -74,19 +80,20 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/nrdcg/dnspod-go v0.4.0 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.2 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.597 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/ulikunitz/xz v0.5.9 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.23.0 // indirect
Expand Down
256 changes: 236 additions & 20 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions internal/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package data
import (
"errors"

"github.com/go-rat/utils/hash"

"github.com/TheTNB/panel/internal/app"
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/pkg/argon2id"
)

type userRepo struct {
hasher *argon2id.Argon2id
hasher hash.Hasher
}

func NewUserRepo() biz.UserRepo {
return &userRepo{
hasher: argon2id.NewArgon2id(4, 65536, 1),
hasher: hash.NewArgon2id(),
}
}

Expand Down
116 changes: 0 additions & 116 deletions pkg/argon2id/argon2id.go

This file was deleted.

149 changes: 142 additions & 7 deletions pkg/io/file.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package io

import (
"archive/zip"
"context"
"errors"
stdio "io"
"os"
"path"
"path/filepath"
"strings"

"github.com/mholt/archiver/v3"
"github.com/mholt/archiver/v4"
)

type FormatArchive string

const (
Zip FormatArchive = "zip"
Gz FormatArchive = "gz"
Bz2 FormatArchive = "bz2"
Tar FormatArchive = "tar"
TarGz FormatArchive = "tar.gz"
Xz FormatArchive = "xz"
SevenZip FormatArchive = "7z"
)

var (
ErrFormatNotSupport = errors.New("不支持此格式")
ErrNotDirectory = errors.New("目标不是目录")
)

// Write 写入文件
Expand Down Expand Up @@ -49,14 +71,102 @@ func FileInfo(path string) (os.FileInfo, error) {
return os.Stat(path)
}

// UnArchive 智能解压文件
func UnArchive(file string, dst string) error {
return archiver.Unarchive(file, dst)
// Compress 压缩文件
func Compress(src []string, dst string, format FormatArchive) error {
// 不支持7z
if format == SevenZip {
return ErrFormatNotSupport
}
arch := getFormat(format)

srcMap := make(map[string]string, len(src))
for _, s := range src {
base := filepath.Base(s)
srcMap[s] = base
}

dir := filepath.Dir(dst)
if !Exists(dir) {
if err := Mkdir(dir, 0755); err != nil {
return err
}
}

files, err := archiver.FilesFromDisk(nil, srcMap)
if err != nil {
return err
}

out, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer out.Close()

err = arch.Archive(context.Background(), out, files)
if err != nil {
_ = Remove(dst)
}

return nil
}

// Archive 智能压缩文件
func Archive(src []string, dst string) error {
return archiver.Archive(src, dst)
// UnCompress 解压文件
func UnCompress(src string, dst string, format FormatArchive) error {
handler := func(ctx context.Context, f archiver.File) error {
info := f.FileInfo
fileName := f.NameInArchive
filePath := filepath.Join(dst, fileName)

if f.FileInfo.IsDir() {
if err := Mkdir(filePath, info.Mode()); err != nil {
return err
}
return nil
}

parentDir := path.Dir(filePath)
if !Exists(parentDir) {
if err := Mkdir(parentDir, info.Mode()); err != nil {
return err
}
}

r, err := f.Open()
if err != nil {
return err
}
w, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
defer r.Close()
defer w.Close()

if _, err = stdio.Copy(w, r); err != nil {
return err
}

return nil
}

arch := getFormat(format)
file, err := os.Open(src)
if err != nil {
return err
}
defer file.Close()

if !Exists(dst) {
if err = Mkdir(dst, 0755); err != nil {
return err
}
}
if !IsDir(dst) {
return ErrNotDirectory
}

return arch.Extract(context.Background(), file, nil, handler)
}

// TempFile 创建临时文件
Expand All @@ -83,3 +193,28 @@ func GetSymlink(path string) string {
}
return linkPath
}

func getFormat(f FormatArchive) archiver.CompressedArchive {
format := archiver.CompressedArchive{}
switch f {
case Tar:
format.Archival = archiver.Tar{}
case TarGz, Gz:
format.Compression = archiver.Gz{}
format.Archival = archiver.Tar{}
case Zip:
format.Archival = archiver.Zip{
Compression: zip.Deflate,
}
case Bz2:
format.Compression = archiver.Bz2{}
format.Archival = archiver.Tar{}
case Xz:
format.Compression = archiver.Xz{}
format.Archival = archiver.Tar{}
case SevenZip:
format.Archival = archiver.SevenZip{}

}
return format
}
Loading

0 comments on commit 0efb1e4

Please sign in to comment.