支持从远程URL读取文件内容
This commit is contained in:
parent
cb92be844e
commit
de2e49144f
31
file.go
31
file.go
@ -13,6 +13,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@ -304,3 +305,33 @@ func (f *file) ReadFileListRecurve(rootDir string) ([]*define.FileInfo, error) {
|
||||
func (f *file) FileExt(filePath string) string {
|
||||
return strings.TrimLeft(filepath.Ext(filePath), ".")
|
||||
}
|
||||
|
||||
// ReadFromRemote 从远端URL读取文件内容
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:09 2024/12/23
|
||||
func (f *file) ReadFromRemote(remoteUrl string) ([]byte, error) {
|
||||
if len(remoteUrl) == 0 {
|
||||
return nil, errors.New("remoteUrl is empty")
|
||||
}
|
||||
var (
|
||||
err error
|
||||
resp *http.Response
|
||||
responseData []byte
|
||||
)
|
||||
|
||||
if _, err = url.Parse(remoteUrl); nil != err {
|
||||
return nil, errors.New("remoteUrl is invalid : " + err.Error())
|
||||
}
|
||||
if resp, err = http.Get(remoteUrl); nil != err {
|
||||
return nil, errors.New("request fail : " + err.Error())
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("http status fail : " + resp.Status)
|
||||
}
|
||||
if responseData, err = io.ReadAll(resp.Body); nil != err {
|
||||
return nil, errors.New("read response fail : " + err.Error())
|
||||
}
|
||||
return responseData, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user