57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func contentType2Ext(contentType string) string {
|
||
|
if strings.Contains(contentType, "image/jpeg") {
|
||
|
return "jpg"
|
||
|
} else if strings.Contains(contentType, "audio/mpeg") {
|
||
|
return "mp3"
|
||
|
} else if strings.Contains(contentType, "video/mp4") {
|
||
|
return "mp4"
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
func ext2ContentType(ext string) string {
|
||
|
ext = strings.ToLower(ext)
|
||
|
if ext == "jpg" || ext == "jpeg" {
|
||
|
return "image/jpeg"
|
||
|
} else if ext == "png" {
|
||
|
return "image/png"
|
||
|
} else if ext == "mp3" {
|
||
|
return "audio/mpeg"
|
||
|
} else if ext == "mp4" {
|
||
|
return "video/mp4"
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
func Download(url, path string) error {
|
||
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||
|
stat, err := f.Stat()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
req, _ := http.NewRequest("GET", url, nil)
|
||
|
req.Header.Set("Range", "bytes="+strconv.FormatInt(stat.Size(), 10)+"-")
|
||
|
resp, err := http.DefaultClient.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
_, err = io.Copy(f, resp.Body)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|