fmt all go file
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"os"
|
||||
// "path/filepath"
|
||||
"strings"
|
||||
// "path/filepath"
|
||||
. "github.com/leanote/leanote/app/lea"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
. "github.com/leanote/leanote/app/lea"
|
||||
)
|
||||
|
||||
// net的util
|
||||
@ -16,33 +17,33 @@ import (
|
||||
// 返回文件的完整目录
|
||||
func WriteUrl(url string, toPath string) (length int64, newFilename, path string, ok bool) {
|
||||
if url == "" {
|
||||
return;
|
||||
return
|
||||
}
|
||||
content, err := GetContent(url)
|
||||
if err != nil {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
length = int64(len(content))
|
||||
|
||||
|
||||
// a.html?a=a11&xxx
|
||||
url = trimQueryParams(url)
|
||||
_, ext := SplitFilename(url)
|
||||
if toPath == "" {
|
||||
toPath = "/tmp"
|
||||
}
|
||||
// dir := filepath.Dir(toPath)
|
||||
// dir := filepath.Dir(toPath)
|
||||
newFilename = NewGuid() + ext
|
||||
fullPath := toPath + "/" + newFilename
|
||||
|
||||
|
||||
// 写到文件中
|
||||
file, err := os.Create(fullPath)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
file.Write(content)
|
||||
|
||||
|
||||
path = fullPath
|
||||
ok = true
|
||||
return
|
||||
@ -53,43 +54,43 @@ func GetContent(url string) (content []byte, err error) {
|
||||
var resp *http.Response
|
||||
resp, err = http.Get(url)
|
||||
Log(err)
|
||||
if(resp != nil && resp.Body != nil) {
|
||||
if resp != nil && resp.Body != nil {
|
||||
defer resp.Body.Close()
|
||||
} else {
|
||||
}
|
||||
if resp == nil || resp.Body == nil || err != nil || resp.StatusCode != http.StatusOK {
|
||||
return
|
||||
}
|
||||
|
||||
var buf []byte
|
||||
buf, err = ioutil.ReadAll(resp.Body)
|
||||
if(err != nil) {
|
||||
Log(err)
|
||||
if resp == nil || resp.Body == nil || err != nil || resp.StatusCode != http.StatusOK {
|
||||
return
|
||||
}
|
||||
|
||||
content = buf;
|
||||
err = nil
|
||||
return
|
||||
|
||||
var buf []byte
|
||||
buf, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
Log(err)
|
||||
return
|
||||
}
|
||||
|
||||
content = buf
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
||||
// 将url ?, #后面的字符串去掉
|
||||
func trimQueryParams(url string) string {
|
||||
pos := strings.Index(url, "?");
|
||||
pos := strings.Index(url, "?")
|
||||
if pos != -1 {
|
||||
url = Substr(url, 0, pos);
|
||||
url = Substr(url, 0, pos)
|
||||
}
|
||||
|
||||
pos = strings.Index(url, "#");
|
||||
|
||||
pos = strings.Index(url, "#")
|
||||
if pos != -1 {
|
||||
url = Substr(url, 0, pos);
|
||||
url = Substr(url, 0, pos)
|
||||
}
|
||||
|
||||
pos = strings.Index(url, "!");
|
||||
|
||||
pos = strings.Index(url, "!")
|
||||
if pos != -1 {
|
||||
url = Substr(url, 0, pos);
|
||||
url = Substr(url, 0, pos)
|
||||
}
|
||||
return url;
|
||||
return url
|
||||
}
|
||||
|
||||
// 通过domain得到ip
|
||||
@ -99,4 +100,4 @@ func GetIpFromDomain(domain string) string {
|
||||
return ip[0].String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user