From de2e49144fcd59cb35f9742fbaeb4f90b7c21641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 23 Dec 2024 16:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BB=8E=E8=BF=9C=E7=A8=8BUR?= =?UTF-8?q?L=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/file.go b/file.go index a238ced..703b93e 100644 --- a/file.go +++ b/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 +}