实现导入Markdown

This commit is contained in:
Minho
2018-03-24 17:24:02 +08:00
parent 7e9769dfbb
commit 4d1a03998a
81 changed files with 15083 additions and 459 deletions

View File

@@ -1,94 +0,0 @@
package utils
import (
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
)
func AbsolutePath(p string) (string, error) {
if strings.HasPrefix(p, "~") {
home := os.Getenv("HOME")
if home == "" {
panic(fmt.Sprintf("can not found HOME in envs, '%s' AbsPh Failed!", p))
}
p = fmt.Sprint(home, string(p[1:]))
}
s, err := filepath.Abs(p)
if nil != err {
return "", err
}
return s, nil
}
// FileExists reports whether the named file or directory exists.
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}
func FormatBytes(size int64) string {
units := []string{" B", " KB", " MB", " GB", " TB"}
s := float64(size)
i := 0
for ; s >= 1024 && i < 4; i++ {
s /= 1024
}
return fmt.Sprintf("%.2f%s", s, units[i])
}
func Round(val float64, places int) float64 {
var t float64
f := math.Pow10(places)
x := val * f
if math.IsInf(x, 0) || math.IsNaN(x) {
return val
}
if x >= 0.0 {
t = math.Ceil(x)
if (t - x) > 0.50000000001 {
t -= 1.0
}
} else {
t = math.Ceil(-x)
if (t + x) > 0.50000000001 {
t -= 1.0
}
t = -t
}
x = t / f
if !math.IsInf(x, 0) {
return x
}
return t
}

View File

@@ -6,6 +6,7 @@ import (
"strings"
"io"
"fmt"
"math"
)
//==================================
@@ -130,3 +131,71 @@ func RemoveDir(dir string) error {
return os.RemoveAll(dir)
}
func AbsolutePath(p string) (string, error) {
if strings.HasPrefix(p, "~") {
home := os.Getenv("HOME")
if home == "" {
panic(fmt.Sprintf("can not found HOME in envs, '%s' AbsPh Failed!", p))
}
p = fmt.Sprint(home, string(p[1:]))
}
s, err := filepath.Abs(p)
if nil != err {
return "", err
}
return s, nil
}
// FileExists reports whether the named file or directory exists.
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func FormatBytes(size int64) string {
units := []string{" B", " KB", " MB", " GB", " TB"}
s := float64(size)
i := 0
for ; s >= 1024 && i < 4; i++ {
s /= 1024
}
return fmt.Sprintf("%.2f%s", s, units[i])
}
func Round(val float64, places int) float64 {
var t float64
f := math.Pow10(places)
x := val * f
if math.IsInf(x, 0) || math.IsNaN(x) {
return val
}
if x >= 0.0 {
t = math.Ceil(x)
if (t - x) > 0.50000000001 {
t -= 1.0
}
} else {
t = math.Ceil(-x)
if (t + x) > 0.50000000001 {
t -= 1.0
}
t = -t
}
x = t / f
if !math.IsInf(x, 0) {
return x
}
return t
}

View File

@@ -0,0 +1,45 @@
package requests
import (
"io"
"net/http"
"net/url"
"os"
"errors"
"fmt"
)
//下载远程文件并保存到指定位置
func DownloadAndSaveFile(remoteUrl, dstFile string) (error) {
client := &http.Client{}
uri, err := url.Parse(remoteUrl)
if err != nil {
return err
}
// Create the file
out, err := os.Create(dstFile)
if err != nil {
return err
}
defer out.Close()
request, err := http.NewRequest("GET", uri.String(), nil)
request.Header.Add("Connection", "close")
request.Header.Add("Host", uri.Host)
request.Header.Add("Referer", uri.String())
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
_, err = io.Copy(out, resp.Body)
}else{
return errors.New(fmt.Sprintf("bad status: %s", resp.Status))
}
return nil
}