1 Star 3 Fork 0

我和我最后的倔强/go-alv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
message.go 4.59 KB
一键复制 编辑 原始数据 按行查看 历史
我和我最后的倔强 提交于 2021-12-27 17:34 . 初始化仓库
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
)
var (
client = &http.Client{
Timeout: 680 * time.Second,
}
LENGTH = 1024 * 1000
)
// handleMessages handles messages
func handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "explore":
// Unmarshal payload
var path FileRnage
if len(m.Payload) > 0 {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &path); err != nil {
payload = err.Error()
return
}
}
// Explore
if payload, err = explore(path); err != nil {
payload = err.Error()
return
}
}
return
}
type FileRnage struct {
Path string `json:"path"`
RangeFirst int `json:"rangeFirst"`
RangeLast int `json:"rangeLast"`
}
// Exploration represents the results of an exploration
type Exploration struct {
Dirs []Dir `json:"dirs"`
RangeFirst int `json:"rangeFirst"`
RangeLast int `json:"rangeLast"`
Logs string `json:"logs"`
Path string `json:"path"`
}
// PayloadDir represents a dir payload
type Dir struct {
Name string `json:"name"`
Path string `json:"path"`
}
// explore explores a path.
// If path is empty, it explores the user's home directory
func explore(path FileRnage) (e Exploration, err error) {
// If no path is provided, use the user's home dir
if len(path.Path) == 0 {
return
}
resph, err := client.Head(path.Path)
if err != nil {
return Exploration{}, err
}
if resph.Header.Get("Content-Type") == "application/octet-stream" {
length, err := strconv.Atoi(resph.Header.Get("Content-Length"))
if err != nil {
return Exploration{}, err
}
_ = resph.Body.Close()
log.Printf("first : %v ,last : %v \n", path.RangeFirst, path.RangeLast)
if length > LENGTH {
log.Println("流加载开始")
req, err := http.NewRequest(http.MethodGet, path.Path, nil)
if err != nil {
return Exploration{}, err
}
req.Header.Add("Accept-Ranges", "bytes")
if path.RangeFirst == 0 {
req.Header.Add("Range", fmt.Sprintf("bytes=0-%v", LENGTH))
e.RangeFirst = LENGTH
} else {
first := LENGTH + path.RangeFirst
if length > first {
log.Println("--继续加载--")
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", (path.RangeFirst-1), first))
e.RangeFirst = first
} else {
log.Println("--加载结束--")
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", (path.RangeFirst-1), length))
e.RangeFirst = 0
}
}
resp, err := client.Do(req)
if err != nil {
return Exploration{}, err
}
e.Logs = strings.ReplaceAll(readBodyFile(resp), "\u0000", "")
} else {
resp, err := client.Get(path.Path)
defer resp.Body.Close()
if err != nil {
return Exploration{}, err
}
e.Logs = strings.ReplaceAll(readBodyFile(resp), "\u0000", "")
}
e.Path = path.Path
return e, nil
} else if resph.Header.Get("Content-Type") == "text/html" {
resp, err := client.Get(path.Path)
if err != nil {
return Exploration{}, nil
}
dom, _ := GetDom(resp.Body)
for _, f := range dom {
e.Dirs = append(e.Dirs, Dir{
Name: f,
Path: path.Path + f,
})
}
e.Path = path.Path
return e, nil
} else {
if err := bootstrap.SendMessage(w, "msg", "不支持查看的文件"); err != nil {
log.Println(fmt.Errorf("sending check.out.menu event failed: %w", err))
}
return Exploration{}, nil
}
//resp, err := client.Get(path.Path)
//if err != nil {
// return
//}
//var dom []string
//if resp.Header.Get("Content-Type") != "application/octet-stream" {
// dom, _ = GetDom(resp.Body)
// for _, f := range dom {
// e.Dirs = append(e.Dirs, Dir{
// Name: f,
// Path: path.Path + f,
// })
// }
//} else {
// e.Logs = strings.ReplaceAll(readBodyFile(resp), "\u0000", "")
//}
//e.Path = path.Path
// Loop through files
//return
}
func GetDom(pd io.ReadCloser) ([]string, error) {
doc, err := goquery.NewDocumentFromReader(pd)
if err != nil {
return nil, err
}
//var res = make([]string, 0)
var m = make([]string, 0)
doc.Find("pre").Find("a").EachWithBreak(func(i int, selection *goquery.Selection) bool {
m = append(m, selection.Text())
return true
})
_ = pd.Close()
return m, nil
}
func readBodyFile(res *http.Response) string {
reader := bufio.NewReaderSize(res.Body, 32*1024)
buf := make([]byte, 1024)
var result string
for {
n, err := reader.Read(buf)
if err != nil || n == 0 {
log.Println(err.Error())
break
}
result += string(buf[:n])
}
return result
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/XueHuiFeiXingDeZhu/go-alv.git
git@gitee.com:XueHuiFeiXingDeZhu/go-alv.git
XueHuiFeiXingDeZhu
go-alv
go-alv
master

搜索帮助