Ariver
2026-06-23 7eb0b4196ce15c8bfbb0514c54b51cf019c78d24
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package model
 
import (
    "errors"
    "fmt"
    "io"
    "net/http"
    "os"
    "os/exec"
    "path/filepath"
    "strconv"
    "strings"
    "time"
 
    "voicesnap/internal/logger"
)
 
// ProgressCallback is called with download progress (percent, downloaded bytes, total bytes).
type ProgressCallback func(percent float64, downloaded, total int64)
 
// Download downloads and extracts the ASR model.
// It tries the primary URL first, then falls back to the fallback URL.
func Download(primaryURL, fallbackURL, modelsDir string, progress ProgressCallback) error {
    profile := DefaultModelProfile()
    urls := []string{primaryURL, fallbackURL}
    return DownloadProfile(profile, urls, modelsDir, progress)
}
 
func DownloadProfile(profile ModelProfile, urls []string, modelsDir string, progress ProgressCallback) error {
    if err := os.MkdirAll(modelsDir, 0755); err != nil {
        return fmt.Errorf("failed to create models dir: %w", err)
    }
    cleanupStaleDownloadDirs(profile, modelsDir)
 
    var lastErr error
    for i, url := range urls {
        if url == "" {
            continue
        }
        if err := downloadAndInstallFromURL(profile, url, modelsDir, i+1, progress); err != nil {
            lastErr = err
            logger.Info("Model install from URL %d failed: %v", i+1, err)
            continue
        }
 
        if err := UpdateInstalledModelState(modelsDir, profile, profile.InstallDirName, "new"); err != nil {
            logger.Error("Failed to update model install state: %v", err)
        }
        logger.Info("Model download and extraction complete")
        return nil
    }
 
    if lastErr == nil {
        lastErr = fmt.Errorf("no download URL configured")
    }
    return fmt.Errorf("download failed: %w", lastErr)
}
 
func cleanupStaleDownloadDirs(profile ModelProfile, modelsDir string) {
    downloadRoot := filepath.Join(modelsDir, ".downloads", profile.ID)
    if err := os.RemoveAll(downloadRoot); err != nil {
        logger.Info("Failed to remove stale download dir %s: %v", downloadRoot, err)
    }
}
 
func downloadAndInstallFromURL(profile ModelProfile, url, modelsDir string, attempt int, progress ProgressCallback) error {
    runID := fmt.Sprintf("%d-%d", time.Now().UnixNano(), attempt)
    downloadDir := filepath.Join(modelsDir, ".downloads", profile.ID, runID)
    extractDir := filepath.Join(downloadDir, "extract")
    stagingDir := filepath.Join(modelsDir, ".staging", profile.ID+"-"+runID)
    archivePath := filepath.Join(downloadDir, "model_package")
 
    if err := os.MkdirAll(extractDir, 0755); err != nil {
        return fmt.Errorf("failed to create download dir: %w", err)
    }
    defer os.RemoveAll(downloadDir)
    defer os.RemoveAll(stagingDir)
 
    logger.Info("Downloading model from URL %d: %s", attempt, url)
    if err := downloadFile(url, archivePath, progress); err != nil {
        return err
    }
 
    logger.Info("Extracting model archive...")
    if err := extractArchive(archivePath, extractDir); err != nil {
        return fmt.Errorf("extraction failed: %w", err)
    }
 
    sourceDir, err := findInstallSource(profile, extractDir)
    if err != nil {
        return err
    }
 
    if err := moveOrCopyDir(sourceDir, stagingDir); err != nil {
        return fmt.Errorf("failed to stage model: %w", err)
    }
 
    validation := ValidateModelDir(profile, stagingDir)
    if !validation.Valid {
        return fmt.Errorf("downloaded model is incomplete: missing=%v problems=%v", validation.Missing, validation.Problems)
    }
 
    finalDir := filepath.Join(modelsDir, profile.InstallDirName)
    return installStagedModel(profile, stagingDir, finalDir, runID)
}
 
func downloadFile(url, destPath string, progress ProgressCallback) error {
    const maxAttempts = 6
    var lastErr error
    var lastPercent = -1.0
 
    for attempt := 1; attempt <= maxAttempts; attempt++ {
        downloaded := existingFileSize(destPath)
        err := downloadFileAttempt(url, destPath, downloaded, &lastPercent, progress)
        if err == nil {
            return nil
        }
        if isNonRetryableDownloadError(err) {
            return err
        }
        lastErr = err
        logger.Info("Download attempt %d/%d failed: %v", attempt, maxAttempts, err)
        if attempt < maxAttempts {
            time.Sleep(time.Duration(attempt) * time.Second)
        }
    }
 
    return fmt.Errorf("download failed after %d attempts: %w", maxAttempts, lastErr)
}
 
func downloadFileAttempt(url, destPath string, resumeFrom int64, lastPercent *float64, progress ProgressCallback) error {
    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        return nonRetryableDownloadError{err: err}
    }
    if resumeFrom > 0 {
        req.Header.Set("Range", fmt.Sprintf("bytes=%d-", resumeFrom))
    }
 
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
 
    appendMode := resumeFrom > 0 && resp.StatusCode == http.StatusPartialContent
    if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
        err := fmt.Errorf("HTTP %d: %s", resp.StatusCode, resp.Status)
        if resp.StatusCode < http.StatusInternalServerError {
            return nonRetryableDownloadError{err: err}
        }
        return err
    }
    if resumeFrom > 0 && resp.StatusCode == http.StatusOK {
        logger.Info("Download server ignored Range request; restarting download")
        resumeFrom = 0
        appendMode = false
    }
 
    totalBytes := resp.ContentLength
    if appendMode {
        if total := parseContentRangeTotal(resp.Header.Get("Content-Range")); total > 0 {
            totalBytes = total
        } else if resp.ContentLength > 0 {
            totalBytes = resumeFrom + resp.ContentLength
        }
    }
 
    flag := os.O_CREATE | os.O_WRONLY
    if appendMode {
        flag |= os.O_APPEND
    } else {
        flag |= os.O_TRUNC
    }
    out, err := os.OpenFile(destPath, flag, 0644)
    if err != nil {
        return err
    }
    defer out.Close()
 
    downloaded := resumeFrom
    if !appendMode {
        downloaded = 0
    }
    buf := make([]byte, 32*1024)
    for {
        n, readErr := resp.Body.Read(buf)
        if n > 0 {
            if _, writeErr := out.Write(buf[:n]); writeErr != nil {
                return writeErr
            }
            downloaded += int64(n)
 
            if totalBytes > 0 && progress != nil {
                pct := float64(downloaded) / float64(totalBytes) * 100
                if pct-*lastPercent >= 0.5 {
                    *lastPercent = pct
                    progress(pct, downloaded, totalBytes)
                }
            }
        }
        if readErr != nil {
            if readErr == io.EOF {
                if totalBytes > 0 && downloaded < totalBytes {
                    return fmt.Errorf("short download: got %d of %d bytes", downloaded, totalBytes)
                }
                return nil
            }
            return readErr
        }
    }
}
 
func existingFileSize(path string) int64 {
    info, err := os.Stat(path)
    if err != nil || info.IsDir() {
        return 0
    }
    return info.Size()
}
 
func parseContentRangeTotal(value string) int64 {
    if value == "" {
        return 0
    }
    slash := strings.LastIndex(value, "/")
    if slash < 0 || slash == len(value)-1 {
        return 0
    }
    total, err := strconv.ParseInt(value[slash+1:], 10, 64)
    if err != nil {
        return 0
    }
    return total
}
 
type nonRetryableDownloadError struct {
    err error
}
 
func (e nonRetryableDownloadError) Error() string {
    return e.err.Error()
}
 
func (e nonRetryableDownloadError) Unwrap() error {
    return e.err
}
 
func isNonRetryableDownloadError(err error) bool {
    var target nonRetryableDownloadError
    return errors.As(err, &target)
}
 
func extractArchive(archivePath, destDir string) error {
    cmd := exec.Command("tar", "-xf", archivePath, "-C", destDir)
    output, err := cmd.CombinedOutput()
    if err != nil {
        return fmt.Errorf("tar extraction failed: %v, output: %s", err, string(output))
    }
    return nil
}
 
func findInstallSource(profile ModelProfile, extractDir string) (string, error) {
    if validation := ValidateModelDir(profile, extractDir); validation.Valid {
        return extractDir, nil
    }
 
    var found string
    err := filepath.WalkDir(extractDir, func(path string, entry os.DirEntry, walkErr error) error {
        if walkErr != nil {
            return walkErr
        }
        if found != "" || !entry.IsDir() {
            return nil
        }
        if path == extractDir {
            return nil
        }
        if validation := ValidateModelDir(profile, path); validation.Valid {
            found = path
            return filepath.SkipDir
        }
        return nil
    })
    if err != nil {
        return "", err
    }
    if found == "" {
        return "", fmt.Errorf("no complete %s model found in archive", profile.ID)
    }
    return found, nil
}
 
func installStagedModel(profile ModelProfile, stagingDir, finalDir, runID string) error {
    var backupDir string
    if _, err := os.Stat(finalDir); err == nil {
        backupDir = finalDir + ".backup-" + runID
        if err := os.Rename(finalDir, backupDir); err != nil {
            return fmt.Errorf("failed to move existing model dir: %w", err)
        }
    }
 
    if err := os.Rename(stagingDir, finalDir); err != nil {
        if copyErr := copyDir(stagingDir, finalDir); copyErr != nil {
            restoreBackup(finalDir, backupDir)
            return fmt.Errorf("failed to install staged model: rename=%v copy=%w", err, copyErr)
        }
    }
 
    if validation := ValidateModelDir(profile, finalDir); !validation.Valid {
        restoreBackup(finalDir, backupDir)
        return fmt.Errorf("installed model is incomplete: missing=%v problems=%v", validation.Missing, validation.Problems)
    }
 
    if backupDir != "" {
        os.RemoveAll(backupDir)
    }
    return nil
}
 
func restoreBackup(finalDir, backupDir string) {
    if backupDir == "" {
        return
    }
    os.RemoveAll(finalDir)
    os.Rename(backupDir, finalDir)
}
 
func moveOrCopyDir(src, dst string) error {
    os.RemoveAll(dst)
    if err := os.Rename(src, dst); err == nil {
        return nil
    }
    return copyDir(src, dst)
}
 
func copyDir(src, dst string) error {
    return filepath.WalkDir(src, func(path string, entry os.DirEntry, walkErr error) error {
        if walkErr != nil {
            return walkErr
        }
        rel, err := filepath.Rel(src, path)
        if err != nil {
            return err
        }
        target := filepath.Join(dst, rel)
        info, err := entry.Info()
        if err != nil {
            return err
        }
        if entry.IsDir() {
            return os.MkdirAll(target, info.Mode())
        }
        in, err := os.Open(path)
        if err != nil {
            return err
        }
        defer in.Close()
 
        out, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
        if err != nil {
            return err
        }
        if _, err := io.Copy(out, in); err != nil {
            out.Close()
            return err
        }
        if err := out.Close(); err != nil {
            return err
        }
        return nil
    })
}