-
Notifications
You must be signed in to change notification settings - Fork 7
/
zipCompressor.go
130 lines (108 loc) · 2.92 KB
/
zipCompressor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package compressor
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/denizgursoy/gotouch/internal/auth"
"github.com/denizgursoy/gotouch/internal/logger"
"github.com/go-playground/validator/v10"
"github.com/denizgursoy/gotouch/internal/manager"
"github.com/denizgursoy/gotouch/internal/store"
)
type (
compressor struct {
Client *http.Client
Manager manager.Manager `validate:"required"`
Store store.Store `validate:"required"`
Strategy ZipStrategy `validate:"required"`
Logger logger.Logger `validate:"required"`
}
)
func newCompressor() Compressor {
return &compressor{
Client: auth.NewAuthenticatedHTTPClient(),
Manager: manager.GetInstance(),
Store: store.GetInstance(),
Strategy: newTarStrategy(),
Logger: logger.NewLogger(),
}
}
func (z *compressor) UncompressFromUrl(url string) error {
if err := validator.New().Struct(z); err != nil {
return err
}
z.Logger.LogInfo("Extracting files...")
response, httpErr := z.Client.Get(url)
if httpErr != nil {
return httpErr
}
pattern := fmt.Sprintf("*%s", z.Strategy.GetExtension())
temp, tempFileErr := os.CreateTemp("", pattern)
if tempFileErr != nil {
return tempFileErr
}
defer func() {
os.Remove(temp.Name())
}()
if _, copyErr := io.Copy(temp, response.Body); copyErr != nil {
return copyErr
}
projectName := z.Store.GetValue(store.ProjectName)
target := fmt.Sprintf("%s/%s", z.Manager.GetExtractLocation(), projectName)
if unCompressError := z.Strategy.UnCompressDirectory(temp.Name(), target); unCompressError != nil {
return unCompressError
}
z.Logger.LogInfo("Zip is extracted successfully")
return nil
}
func (z *compressor) CompressDirectory(source, target string) error {
if !filepath.IsAbs(source) {
absoluteSource, err := filepath.Abs(source)
if err != nil {
return err
}
source = absoluteSource
}
if !filepath.IsAbs(target) {
absoluteTarget, err := filepath.Abs(target)
if err != nil {
return err
}
target = absoluteTarget
}
if !checkIsDirectory(source) {
return errors.New("source is not a directory")
} else if !checkIsDirectory(target) {
return errors.New("target is not a directory")
}
if !strings.HasSuffix(source, string(os.PathSeparator)) {
source = fmt.Sprintf("%s%s", source, string(os.PathSeparator))
}
filename := fmt.Sprintf("%s%s", filepath.Base(source), z.Strategy.GetExtension())
target = filepath.Join(target, string(os.PathSeparator), filename)
return z.Strategy.CompressDirectory(source, target)
}
func checkIsDirectory(path string) bool {
open, err := os.Open(path)
if err != nil {
return false
}
stat, err := open.Stat()
if err != nil {
return false
}
return stat.IsDir()
}
func shouldSkip(fileName string) bool {
filesNotToZip := []string{"__MACOS", ".DS_Store", ".idea", ".vscode", ".git"}
for _, file := range filesNotToZip {
if strings.TrimSpace(fileName) == file {
return true
}
}
return false
}