Greg-House commited on
Commit
8c94bfc
·
verified ·
1 Parent(s): 7164de3

Update main.go

Browse files
Files changed (1) hide show
  1. main.go +21 -27
main.go CHANGED
@@ -160,33 +160,28 @@ func unrarSource(source, dest string) error {
160
  cmd := exec.Command("unrar", "x", "-y", source, dest+string(os.PathSeparator))
161
  return cmd.Run()
162
  }
163
-
164
- // --- Download & Zip Logic ---
165
  func downloadFile(url string, customName string, destFolder string) (string, error) {
166
-
167
- resp, err := http.Get(url)
168
- if err != nil {
169
- return "", err
170
- }
171
-
172
- filename := customName
173
- if filename == "" {
174
- filename = "download_" + uuid.New().String()
175
- }
176
-
177
- filename = filepath.Base(filename)
178
- finalPath := filepath.Join(destFolder, filename)
179
-
180
- out, err := os.Create(finalPath)
181
- if err != nil {
182
- return "", err
183
- }
184
- defer out.Close()
185
-
186
- _, err = io.Copy(out, resp.Body)
187
- return finalPath, err
188
- }
189
-
190
 
191
  filename = filepath.Base(filename)
192
  finalPath := filepath.Join(destFolder, filename)
@@ -198,7 +193,6 @@ func downloadFile(url string, customName string, destFolder string) (string, err
198
  _, err = io.Copy(out, resp.Body)
199
  return finalPath, err
200
  }
201
-
202
  func zipFile(sourcePath string, destFolder string) (string, error) {
203
  filename := filepath.Base(sourcePath)
204
  zipName := fmt.Sprintf("%s_%s.zip", strings.TrimSuffix(filename, filepath.Ext(filename)), uuid.New().String()[:8])
 
160
  cmd := exec.Command("unrar", "x", "-y", source, dest+string(os.PathSeparator))
161
  return cmd.Run()
162
  }
 
 
163
  func downloadFile(url string, customName string, destFolder string) (string, error) {
164
+ resp, err := http.Get(url)
165
+ if err != nil {
166
+ return "", err
167
+ }
168
+ defer resp.Body.Close()
169
+
170
+ filename := customName
171
+ if filename == "" {
172
+ if cd := resp.Header.Get("Content-Disposition"); cd != "" {
173
+ _, params, err := mime.ParseMediaType(cd)
174
+ if err == nil {
175
+ filename = params["filename"]
176
+ }
177
+ }
178
+ if filename == "" {
179
+ filename = filepath.Base(resp.Request.URL.Path)
180
+ }
181
+ if filename == "" || filename == "." || filename == "/" {
182
+ filename = "download_" + uuid.New().String()
183
+ }
184
+ }
 
 
 
185
 
186
  filename = filepath.Base(filename)
187
  finalPath := filepath.Join(destFolder, filename)
 
193
  _, err = io.Copy(out, resp.Body)
194
  return finalPath, err
195
  }
 
196
  func zipFile(sourcePath string, destFolder string) (string, error) {
197
  filename := filepath.Base(sourcePath)
198
  zipName := fmt.Sprintf("%s_%s.zip", strings.TrimSuffix(filename, filepath.Ext(filename)), uuid.New().String()[:8])