This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullservice.go
118 lines (100 loc) · 2.46 KB
/
pullservice.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
package main
import (
"archive/zip"
"fmt"
"github.com/kjk/lzmadec"
"github.com/pkg/errors"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
type PullService struct{
BoneTomeMetadata *BoneTomeMetadata
TempDirectory string
}
func (ps *PullService) download(btmd *BoneTomeMetadata) {
resp, err := http.Get(btmd.Asset.DownloadUrl)
if err != nil {
log.Fatal("Unable to establish connection to download file: "+btmd.Asset.DownloadUrl, err)
}
defer resp.Body.Close()
file, _ := os.Create(btmd.Asset.Name)
defer file.Close()
io.Copy(file, resp.Body)
}
func (ps *PullService) Do(btmd *BoneTomeMetadata) {
_, lstatErr := os.Lstat(btmd.Asset.Name + ".temp_dir")
if !(os.IsNotExist(lstatErr)) {
err := os.RemoveAll(btmd.Asset.Name + ".temp_dir")
if err != nil {
log.Fatal(err)
}
}
ps.download(btmd)
nameLower := strings.ToLower(btmd.Asset.Name)
if strings.HasSuffix(nameLower, ".zip") {
err := ps.unzip(btmd)
if err != nil {
log.Fatal(err)
}
} else if strings.HasSuffix(nameLower, ".7z") {
ps.unzip7z(btmd)
}
ps.BoneTomeMetadata = btmd
ps.TempDirectory = btmd.Asset.Name + ".temp_dir"
err := os.Remove(btmd.Asset.Name)
if err != nil {
log.Fatal(err)
}
}
func (ps *PullService) unzip(btmd *BoneTomeMetadata) error {
baseDir := btmd.Asset.Name + ".temp_dir"
r, err := zip.OpenReader(btmd.Asset.Name)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(baseDir, f.Name)
if !strings.HasPrefix(fpath, filepath.Clean(baseDir)+string(os.PathSeparator)) {
return errors.New(fmt.Sprintf("%s is an invalid filepath", fpath))
}
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
continue
}
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
rc, err := f.Open()
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return err
}
}
return nil
}
func (ps *PullService) unzip7z(btmd *BoneTomeMetadata) {
archive, archErr := lzmadec.NewArchive(path.Join(".", btmd.Asset.Name))
if archErr != nil {
log.Fatal(archErr)
}
baseDir := path.Join(".", btmd.Asset.Name+".temp_dir")
_ = os.Mkdir(baseDir, 0775)
for entry := range archive.Entries {
_ = archive.ExtractToFile(path.Join(baseDir, archive.Entries[entry].Path), archive.Entries[entry].Path)
}
}