-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathzip.go
More file actions
133 lines (113 loc) · 2.8 KB
/
zip.go
File metadata and controls
133 lines (113 loc) · 2.8 KB
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
131
132
133
package main
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
)
var modifiedBuffer []byte
func extractZipFile(zipFile *zip.File) ([]byte, error) {
file, err := zipFile.Open()
if err != nil {
return nil, err
}
defer file.Close()
return io.ReadAll(file)
}
func retreiveZipFile() ([]byte, error) {
// only retreive data once
if modifiedBuffer != nil {
log.Println("reusing already prepared net-sideloader")
return modifiedBuffer, nil
}
var zipData []byte
var download bool
if _, err := os.Stat(officialAppFile); err == nil { // check if the plex-app has already been downloaded
log.Println("plex-app exists in local directory, not downloading it again")
zipData, err = os.ReadFile(officialAppFile)
if err != nil {
return nil, err
}
} else {
// download the original zip
log.Println("downloading application from " + officialAppURL + " ... ")
resp, err := http.Get(officialAppURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
log.Println("done!")
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
} else {
zipData = body
}
download = true
}
// checksum
chksum := sha256.Sum256(zipData)
if hex.EncodeToString(chksum[:]) == officialAppChksum {
log.Println("checksum match. going on ...")
} else {
var message string
message = "checksum did not match, aborting"
if !download {
message = message + "\nPlease delete existing file " + officialAppFile + " to force redownload"
}
err := errors.New(message)
log.Println(message)
return nil, err
}
if download {
// write zipData to local file for caching
err := os.WriteFile(officialAppFile, zipData, 0664)
if err != nil {
log.Println("could not save downloaded file, going on anyway")
}
}
// attach the zipWriter to the buffer
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
// create zipReader
zipReader, err := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
if err != nil {
return nil, err
}
// regexp pattern to remove first directory
regex := regexp.MustCompile(`^Plex/`)
// process all files, read each file and write it to new zip
for _, zipFile := range zipReader.File {
fmt.Println("Processing file:", zipFile.Name)
newFileName := regex.ReplaceAllString(zipFile.Name, "")
fmt.Println("Replacement:", newFileName)
bytes, err := extractZipFile(zipFile)
if err != nil {
return nil, err
}
// write file to new zip
file, err := w.Create(newFileName)
if err != nil {
return nil, err
}
_, err = file.Write(bytes)
if err != nil {
return nil, err
}
}
err = w.Close()
if err != nil {
return nil, err
}
// write data to global var
modifiedBuffer = buf.Bytes()
log.Println("app ready for net-sideloading!")
return modifiedBuffer, nil
}