|
5 | 5 | "fmt" |
6 | 6 | "hash" |
7 | 7 | "io" |
8 | | - "net/http" |
9 | 8 | "os" |
10 | 9 | "path/filepath" |
11 | 10 | "strconv" |
@@ -115,89 +114,8 @@ func InstallModel(basePath, nameOverride string, config *Config, configOverrides |
115 | 114 | // Create file path |
116 | 115 | filePath := filepath.Join(basePath, file.Filename) |
117 | 116 |
|
118 | | - // Check if the file already exists |
119 | | - _, err := os.Stat(filePath) |
120 | | - if err == nil { |
121 | | - // File exists, check SHA |
122 | | - if file.SHA256 != "" { |
123 | | - // Verify SHA |
124 | | - calculatedSHA, err := calculateSHA(filePath) |
125 | | - if err != nil { |
126 | | - return fmt.Errorf("failed to calculate SHA for file %q: %v", file.Filename, err) |
127 | | - } |
128 | | - if calculatedSHA == file.SHA256 { |
129 | | - // SHA matches, skip downloading |
130 | | - log.Debug().Msgf("File %q already exists and matches the SHA. Skipping download", file.Filename) |
131 | | - continue |
132 | | - } |
133 | | - // SHA doesn't match, delete the file and download again |
134 | | - err = os.Remove(filePath) |
135 | | - if err != nil { |
136 | | - return fmt.Errorf("failed to remove existing file %q: %v", file.Filename, err) |
137 | | - } |
138 | | - log.Debug().Msgf("Removed %q (SHA doesn't match)", filePath) |
139 | | - |
140 | | - } else { |
141 | | - // SHA is missing, skip downloading |
142 | | - log.Debug().Msgf("File %q already exists. Skipping download", file.Filename) |
143 | | - continue |
144 | | - } |
145 | | - } else if !os.IsNotExist(err) { |
146 | | - // Error occurred while checking file existence |
147 | | - return fmt.Errorf("failed to check file %q existence: %v", file.Filename, err) |
148 | | - } |
149 | | - |
150 | | - log.Debug().Msgf("Downloading %q", file.URI) |
151 | | - |
152 | | - // Download file |
153 | | - resp, err := http.Get(file.URI) |
154 | | - if err != nil { |
155 | | - return fmt.Errorf("failed to download file %q: %v", file.Filename, err) |
156 | | - } |
157 | | - defer resp.Body.Close() |
158 | | - |
159 | | - // Create parent directory |
160 | | - err = os.MkdirAll(filepath.Dir(filePath), 0755) |
161 | | - if err != nil { |
162 | | - return fmt.Errorf("failed to create parent directory for file %q: %v", file.Filename, err) |
163 | | - } |
164 | | - |
165 | | - // Create and write file content |
166 | | - outFile, err := os.Create(filePath) |
167 | | - if err != nil { |
168 | | - return fmt.Errorf("failed to create file %q: %v", file.Filename, err) |
169 | | - } |
170 | | - defer outFile.Close() |
171 | | - |
172 | | - progress := &progressWriter{ |
173 | | - fileName: file.Filename, |
174 | | - total: resp.ContentLength, |
175 | | - hash: sha256.New(), |
176 | | - downloadStatus: downloadStatus, |
177 | | - } |
178 | | - _, err = io.Copy(io.MultiWriter(outFile, progress), resp.Body) |
179 | | - if err != nil { |
180 | | - return fmt.Errorf("failed to write file %q: %v", file.Filename, err) |
181 | | - } |
182 | | - |
183 | | - if file.SHA256 != "" { |
184 | | - // Verify SHA |
185 | | - calculatedSHA := fmt.Sprintf("%x", progress.hash.Sum(nil)) |
186 | | - if calculatedSHA != file.SHA256 { |
187 | | - log.Debug().Msgf("SHA mismatch for file %q ( calculated: %s != metadata: %s )", file.Filename, calculatedSHA, file.SHA256) |
188 | | - return fmt.Errorf("SHA mismatch for file %q ( calculated: %s != metadata: %s )", file.Filename, calculatedSHA, file.SHA256) |
189 | | - } |
190 | | - } else { |
191 | | - log.Debug().Msgf("SHA missing for %q. Skipping validation", file.Filename) |
192 | | - } |
193 | | - |
194 | | - log.Debug().Msgf("File %q downloaded and verified", file.Filename) |
195 | | - if utils.IsArchive(filePath) { |
196 | | - log.Debug().Msgf("File %q is an archive, uncompressing to %s", file.Filename, basePath) |
197 | | - if err := utils.ExtractArchive(filePath, basePath); err != nil { |
198 | | - log.Debug().Msgf("Failed decompressing %q: %s", file.Filename, err.Error()) |
199 | | - return err |
200 | | - } |
| 117 | + if err := utils.DownloadFile(file.URI, filePath, file.SHA256, downloadStatus); err != nil { |
| 118 | + return err |
201 | 119 | } |
202 | 120 | } |
203 | 121 |
|
|
0 commit comments