-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilestore-local.go
213 lines (179 loc) · 5.29 KB
/
filestore-local.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"archive/zip"
"bytes"
"crypto/sha512"
"encoding/hex"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
nuspec "github.com/soloworks/go-nuspec"
)
type fileStoreLocal struct {
rootDir string
packages []*NugetPackageEntry
}
func (fs *fileStoreLocal) Init(s *Server) error {
// Set the Repo Path
fs.rootDir = s.config.FileStore.RepoDIR
// Create the package folder if requried
if _, err := os.Stat(fs.rootDir); os.IsNotExist(err) {
// Path already exists
log.Println("Creating Directory: ", fs.rootDir)
err := os.MkdirAll(fs.rootDir, os.ModePerm)
if err != nil {
return err
}
}
// Refresh Packages
err := fs.RefeshPackages()
if err != nil {
return err
}
// Return repo
return nil
}
func (fs *fileStoreLocal) RefeshPackages() error {
// Read in all files in directory root
IDs, err := ioutil.ReadDir(fs.rootDir)
if err != nil {
return err
}
// Loop through all directories (first level is lowercase IDs)
for _, ID := range IDs {
// Check if this is a directory
if ID.IsDir() {
// Search files in directory (second level is versions)
Vers, err := ioutil.ReadDir(filepath.Join(fs.rootDir, ID.Name()))
if err != nil {
return err
}
for _, Ver := range Vers {
// Check if this is a directory
if Ver.IsDir() {
// Create full filepath
fp := filepath.Join(fs.rootDir, ID.Name(), Ver.Name(), ID.Name()+"."+Ver.Name()+".nupkg")
if _, err := os.Stat(fp); os.IsNotExist(err) {
log.Println("Not a nupkg directory")
break
}
err = fs.LoadPackage(fp)
if err != nil {
log.Println("Error: Cannot load package")
log.Println(err)
break
}
}
}
}
}
log.Printf("fs Loaded with %d Packages Found", len(fs.packages))
return nil
}
func (fs *fileStoreLocal) LoadPackage(fp string) error {
// Open and read in the file (Is a Zip file under the hood)
content, err := ioutil.ReadFile(fp)
if err != nil {
return err
}
f, err := os.Stat(fp)
if err != nil {
return err
}
// Set up a zipReader
zipReader, err := zip.NewReader(bytes.NewReader(content), int64(len(content)))
if err != nil {
return err
}
// NugetPackage Object
var p *NugetPackageEntry
// Find and Process the .nuspec file
for _, zipFile := range zipReader.File {
// If this is the root .nuspec file read it into a NewspecFile structure
if filepath.Dir(zipFile.Name) == "." && filepath.Ext(zipFile.Name) == ".nuspec" {
// Marshall XML into Structure
rc, err := zipFile.Open()
if err != nil {
return err
}
b, err := ioutil.ReadAll(rc)
if err != nil {
return err
}
// Read into NuspecFile structure
nsf, err := nuspec.FromBytes(b)
// Read Entry into memory
p = NewNugetPackageEntry(nsf)
// Set Updated to match file
p.Properties.Created.Value = f.ModTime().Format(zuluTimeLayout)
p.Properties.LastEdited.Value = f.ModTime().Format(zuluTimeLayout)
p.Properties.Published.Value = f.ModTime().Format(zuluTimeLayout)
p.Updated = f.ModTime().Format(zuluTimeLayout)
// Get and Set file hash
h := sha512.Sum512(content)
p.Properties.PackageHash = hex.EncodeToString(h[:])
p.Properties.PackageHashAlgorithm = `SHA512`
p.Properties.PackageSize.Value = len(content)
p.Properties.PackageSize.Type = "Edm.Int64"
// Insert this into the array in order
index := sort.Search(len(fs.packages), func(i int) bool { return fs.packages[i].Filename() > p.Filename() })
x := NugetPackageEntry{}
fs.packages = append(fs.packages, &x)
copy(fs.packages[index+1:], fs.packages[index:])
fs.packages[index] = p
}
}
return nil
}
func (fs *fileStoreLocal) RemovePackage(fn string) {
// Remove the Package from the Map
for i, p := range fs.packages {
if p.Filename() == fn {
fs.packages = append(fs.packages[:i], fs.packages[i+1:]...)
}
}
// Delete the contents directory
os.RemoveAll(filepath.Join(fs.rootDir, `content`, fn))
}
func (fs *fileStoreLocal) StorePackage(pkg []byte) (bool, error) {
/*
// Test for folder, if present bail, if not make it
// Fixme: Broke this to get to compile
packagePath := filepath.Join(c.FileStore.RepoDIR, strings.ToLower(nsf.Meta.ID), nsf.Meta.Version)
if _, err := os.Stat(packagePath); !os.IsNotExist(err) {
// Path already exists
w.WriteHeader(http.StatusConflict)
return
}
err = os.MkdirAll(packagePath, os.ModePerm)
if err != nil {
println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Println("Creating Directory: ", packagePath)
// Dump the .nupkg file in the same directory
err = ioutil.WriteFile(filepath.Join(packagePath, strings.ToLower(nsf.Meta.ID)+"."+nsf.Meta.Version+".nupkg"), body, os.ModePerm)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}*/
return false, nil
}
func (fs *fileStoreLocal) GetPackageEntry(id string, ver string) (*NugetPackageEntry, error) {
return nil, nil
}
func (fs *fileStoreLocal) GetPackageFeedEntries(id string, startAfter string, max int) ([]*NugetPackageEntry, bool, error) {
return nil, false, nil
}
func (fs *fileStoreLocal) GetPackageFile(id string, ver string) ([]byte, string, error) {
return nil, "", nil
}
func (fs *fileStoreLocal) GetFile(f string) ([]byte, string, error) {
return nil, "", nil
}
func (fs *fileStoreLocal) GetAccessLevel(key string) (access, error) {
return accessDenied, nil
}