57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func GetImg(url string) string {
|
|
path, _ := os.Getwd()
|
|
imgPath := fmt.Sprintf("%s/%s", path, "img")
|
|
dir := filepath.Dir(imgPath)
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
os.MkdirAll(dir, 0755)
|
|
}
|
|
urlSlice := strings.Split(url, "/")
|
|
fileName := fmt.Sprintf("%s/%s", imgPath, urlSlice[len(urlSlice)-1])
|
|
_, err := os.Stat(fileName)
|
|
if os.IsNotExist(err) {
|
|
DownloadFile(url, fileName)
|
|
}
|
|
return fileName
|
|
|
|
}
|
|
|
|
func DownloadFile(url string, fileName string) {
|
|
resp, _ := http.Get(url)
|
|
|
|
defer resp.Body.Close()
|
|
out, _ := os.Create(fileName)
|
|
io.Copy(out, resp.Body)
|
|
defer out.Close()
|
|
}
|
|
|
|
func DownloadPpt(url string, fileName string) (file string) {
|
|
path, _ := os.Getwd()
|
|
pptPath := fmt.Sprintf("%s/%s", path, "ppt")
|
|
dir := filepath.Dir(pptPath)
|
|
fmt.Println(pptPath)
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
e := os.MkdirAll(dir, 0755)
|
|
fmt.Println(e)
|
|
}
|
|
fileName = fmt.Sprintf("%s-%d", fileName, time.Now().Unix())
|
|
file = fmt.Sprintf("%s/%s.%s", pptPath, fileName, "pptx")
|
|
resp, _ := http.Get(url)
|
|
defer resp.Body.Close()
|
|
out, _ := os.Create(file)
|
|
io.Copy(out, resp.Body)
|
|
defer out.Close()
|
|
return
|
|
}
|