func DowOSSImg(imageurl string) (int, error) {
var(
fileName string
filePath string
filePathName string
dir string
urlparse *url.URL
resp *http.Response
fileBody []byte
outfile *os.File
err error
)
fileName = string([]byte(path.Base(imageurl))[0:strings.LastIndex(path.Base(imageurl), "?")])
urlparse,err = url.Parse(imageurl)
if (err != nil) {
return 0, errors.New("Error:url.Parse"+imageurl)
}
if dir, err = os.Getwd() ;err !=nil{
return 0, errors.New("Error:oss.Getwd"+err.Error())
}
filePathName = dir+urlparse.Path
filePath = strings.Split(filePathName, fileName)[0]
_, e := os.Stat(filePath)
if !os.IsExist(e) {
if err := os.MkdirAll(filePath, os.ModePerm) ;err !=nil{
return 0, errors.New("Error:chmod 777" +filePath)
}
}
resp, err = http.Get(imageurl)
if (err != nil) {
return 0, errors.New("Error:Current status code:" + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
fileBody, err = ioutil.ReadAll(resp.Body)
if (err != nil) {
return 0, errors.New("Error:ioutil.ReadAll")
}
ossXml := ossXml{Code: "none", Message: "none", RequestId: "none", HostId: "none", Key: "none"}
if err = xml.Unmarshal([]byte(fileBody), &ossXml) ;err ==nil{
return 0, errors.New("Error:ioutil.ReadAll Becaues NOT FIND"+imageurl )
}
outfile, err = os.Create(filePath+fileName)
if (err != nil) {
return 0, errors.New("Error: os.Create:"+err.Error())
}
defer outfile.Close()
return outfile.Write(fileBody)
}