40 Star 146 Fork 3

Gitee 极速下载/grafana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grafana/grafana
克隆/下载
webdavuploader.go 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
berghauz 提交于 2017-05-17 03:58 . Indenting fix
package imguploader
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"time"
"github.com/grafana/grafana/pkg/util"
)
type WebdavUploader struct {
url string
username string
password string
public_url string
}
var netTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 60 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
var netClient = &http.Client{
Timeout: time.Second * 60,
Transport: netTransport,
}
func (u *WebdavUploader) Upload(pa string) (string, error) {
url, _ := url.Parse(u.url)
filename := util.GetRandomString(20) + ".png"
url.Path = path.Join(url.Path, filename)
imgData, err := ioutil.ReadFile(pa)
req, err := http.NewRequest("PUT", url.String(), bytes.NewReader(imgData))
if u.username != "" {
req.SetBasicAuth(u.username, u.password)
}
res, err := netClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusCreated {
body, _ := ioutil.ReadAll(res.Body)
return "", fmt.Errorf("Failed to upload image. Returned statuscode %v body %s", res.StatusCode, body)
}
if u.public_url != "" {
publicURL, _ := url.Parse(u.public_url)
publicURL.Path = path.Join(publicURL.Path, filename)
return publicURL.String(), nil
}
return url.String(), nil
}
func NewWebdavImageUploader(url, username, password, public_url string) (*WebdavUploader, error) {
return &WebdavUploader{
url: url,
username: username,
password: password,
public_url: public_url,
}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/grafana.git
git@gitee.com:mirrors/grafana.git
mirrors
grafana
grafana
v4.4.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385