Skip to content

Commit

Permalink
add support for papermc and arclight
Browse files Browse the repository at this point in the history
  • Loading branch information
Fellsoul authored and zyxkad committed Mar 7, 2024
1 parent d8c50dc commit f7e297f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
50 changes: 25 additions & 25 deletions httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *HTTPClient) NewRequest(method string, url string, body io.Reader) (req
}

func (c *HTTPClient) Do(req *http.Request) (res *http.Response, err error) {
if _, ok := req.Header["User-Agent"]; !ok {
if ua := req.Header.Get("User-Agent"); ua == "" {
req.Header.Set("User-Agent", c.UserAgent)
}
if res, err = c.Client.Do(req); err != nil {
Expand Down Expand Up @@ -165,12 +165,35 @@ func (c *HTTPClient) DownloadTmp(url string, pattern string, mode os.FileMode, h
func (c *HTTPClient) Download(url string, path string, mode os.FileMode, hashes StringMap, size int64, cb DlCallback) (err error) {
var tmppath string
tmppath, err = c.DownloadTmp(url, path+".*.downloading", mode, hashes, size, cb)
if err = renameIfNotExist(tmppath, path); err != nil {
if err = renameIfNotExist(tmppath, path, 0644); err != nil {
return
}
return
}

func (c *HTTPClient) Head(url string) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("HEAD", url, nil); err != nil {
return
}
return c.Do(req)
}

func (c *HTTPClient) Post(url string, contentType string, body io.Reader) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("POST", url, body); err != nil {
return
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}

func (c *HTTPClient) PostForm(url string, form url.Values) (res *http.Response, err error) {
formStr := form.Encode()
return c.Post(url, "application/x-www-form-urlencoded",
strings.NewReader(formStr))
}

func (c *HTTPClient) DownloadDirect(url string, ExactDownloadeName string, cb DlCallback) (installed string, err error) {
resp, err := http.Head(url)
if err != nil {
Expand Down Expand Up @@ -207,26 +230,3 @@ func (c *HTTPClient) DownloadDirect(url string, ExactDownloadeName string, cb Dl
installed = filepath.Join(cpath, ExactDownloadeName)
return
}

func (c *HTTPClient) Head(url string) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("HEAD", url, nil); err != nil {
return
}
return c.Do(req)
}

func (c *HTTPClient) Post(url string, contentType string, body io.Reader) (res *http.Response, err error) {
var req *http.Request
if req, err = c.NewRequest("POST", url, body); err != nil {
return
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}

func (c *HTTPClient) PostForm(url string, form url.Values) (res *http.Response, err error) {
formStr := form.Encode()
return c.Post(url, "application/x-www-form-urlencoded",
strings.NewReader(formStr))
}
37 changes: 33 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ var EmptyLinkArrayErr = errors.New("Link array is empty")

type StringMap = map[string]string

func renameIfNotExist(src, dst string) (err error) {
func osCopy(src, dst string, mode os.FileMode) (err error) {
srcFd, err := os.Open(src)
if err != nil {
return
}
defer srcFd.Close()
dstFd, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return
}
_, err = io.Copy(dstFd, srcFd)
if er := dstFd.Close(); err == nil && er != nil {
err = er
}
if err != nil {
os.Remove(dst)
return
}
return
}

func renameIfNotExist(src, dst string, mode os.FileMode) (err error) {
if _, e := os.Stat(dst); os.IsNotExist(e) {
if err = os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return
Expand All @@ -35,8 +56,16 @@ func renameIfNotExist(src, dst string) (err error) {
}
}
if err = os.Rename(src, dst); err != nil {
if crossDevice(err) {
if err = osCopy(src, dst, mode); err != nil {
return
}
os.Remove(src)
return
}
return
}
os.Chmod(dst, mode)
return
}

Expand All @@ -51,7 +80,7 @@ func safeDownload(reader io.Reader, path string) (err error) {
if err != nil {
return
}
if err = renameIfNotExist(fd.Name(), path); err != nil {
if err = renameIfNotExist(fd.Name(), path, 0644); err != nil {
return
}
return nil
Expand Down Expand Up @@ -131,12 +160,12 @@ func downloadAnyAndCheckHashes(links []string, path string, hashes StringMap, si
}
for _, l := range links {
var tmp string
if tmp, err = DefaultHTTPClient.DownloadTmp(l, "downloading_", 0644, hashes, size,
if tmp, err = DefaultHTTPClient.DownloadTmp(l, "*.downloading", 0644, hashes, size,
downloadingCallback(l)); err != nil {
continue
}
defer os.Remove(tmp)
if err = renameIfNotExist(tmp, path); err != nil {
if err = renameIfNotExist(tmp, path, 0644); err != nil {
return
}
break
Expand Down

0 comments on commit f7e297f

Please sign in to comment.