@@ -2,27 +2,15 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
- "io"
6
- "net/http"
7
5
"os"
8
6
"path/filepath"
9
7
"strings"
10
8
"time"
11
9
12
- "gopkg.in/yaml.v3"
13
-
14
- "github.com/schollz/progressbar/v3"
10
+ "github.com/yansigit/civitai-downloader/config"
11
+ "github.com/yansigit/civitai-downloader/downloader"
15
12
)
16
13
17
- type Config struct {
18
- Civitai struct {
19
- Token string `yaml:"token"`
20
- } `yaml:"civitai"`
21
- ComfyUI struct {
22
- BaseModelPath string `yaml:"base_model_path"`
23
- } `yaml:"comfyui"`
24
- }
25
-
26
14
func main () {
27
15
if len (os .Args ) != 3 {
28
16
fmt .Println ("Usage: " , os .Args [0 ], " <model_type> <model_url|AIR>" )
@@ -38,7 +26,7 @@ func main() {
38
26
}
39
27
40
28
configPath := os .Getenv ("HOME" ) + "/.civitai-downloader/config.yaml"
41
- config , err := loadConfig (configPath )
29
+ config , err := config . LoadConfig (configPath )
42
30
if err != nil {
43
31
fmt .Println ("Error loading config:" , err )
44
32
return
@@ -71,75 +59,11 @@ func main() {
71
59
72
60
outputPath := filepath .Join (baseModelPath , modelType , fmt .Sprintf ("temp-%d.safetensors" , time .Now ().UnixNano ()))
73
61
74
- err = downloadFile (outputPath , downloadURL )
62
+ err = downloader . DownloadFile (outputPath , downloadURL )
75
63
if err != nil {
76
64
fmt .Printf ("Error downloading %s: %v\n " , modelType , err )
77
65
return
78
66
}
79
67
80
68
fmt .Printf ("Model downloaded successfully to %s\n " , filepath .Dir (outputPath ))
81
69
}
82
-
83
- func loadConfig (filename string ) (* Config , error ) {
84
- data , err := os .ReadFile (filename )
85
- if err != nil {
86
- return nil , err
87
- }
88
-
89
- config := & Config {}
90
- err = yaml .Unmarshal (data , config )
91
- if err != nil {
92
- return nil , err
93
- }
94
-
95
- return config , nil
96
- }
97
-
98
- func downloadFile (outputPath string , url string ) error {
99
- dir := filepath .Dir (outputPath )
100
- if err := os .MkdirAll (dir , 0755 ); err != nil {
101
- return fmt .Errorf ("failed to create directories: %v" , err )
102
- }
103
-
104
- resp , err := http .Get (url )
105
- if err != nil {
106
- return fmt .Errorf ("failed to fetch %s: %v" , url , err )
107
- }
108
- defer resp .Body .Close ()
109
-
110
- if resp .StatusCode != http .StatusOK {
111
- return fmt .Errorf ("HTTP error for %s: %v" , url , resp .StatusCode )
112
- }
113
-
114
- header := resp .Header .Get ("content-disposition" )
115
- if header != "" {
116
- parts := strings .Split (header , "filename=" )
117
- if len (parts ) > 1 {
118
- outputPath = filepath .Join (filepath .Dir (outputPath ), strings .Trim (parts [1 ], "\" " ))
119
- }
120
- }
121
-
122
- file , err := os .Create (outputPath )
123
- if err != nil {
124
- return fmt .Errorf ("failed to create file %s: %v" , outputPath , err )
125
- }
126
- defer file .Close ()
127
-
128
- bar := progressbar .NewOptions (
129
- int (resp .ContentLength ),
130
- progressbar .OptionSetWidth (15 ),
131
- progressbar .OptionEnableColorCodes (true ),
132
- progressbar .OptionSetDescription ("[Downloading] " ),
133
- progressbar .OptionSetTheme (
134
- progressbar.Theme {
135
- Saucer : "[green]=[reset]" ,
136
- SaucerHead : "[green]>[reset]" ,
137
- SaucerPadding : " " ,
138
- BarStart : "|" ,
139
- BarEnd : "|" ,
140
- }),
141
- )
142
-
143
- _ , err = io .Copy (io .MultiWriter (file , bar ), resp .Body )
144
- return err
145
- }
0 commit comments