-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big upstream changes from Project Forge
- Loading branch information
Showing
42 changed files
with
527 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package filesystem - Content managed by Project Forge, see [projectforge.md] for details. | ||
package filesystem | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/kyleu/npn/app/util" | ||
) | ||
|
||
func (f *FileSystem) Download(ctx context.Context, url string, path string, overwrite bool, _ util.Logger) (int, error) { | ||
if !strings.HasPrefix(url, "https://") { | ||
return 0, errors.New("only [https] requests are supported") | ||
} | ||
if f.Exists(path) && !overwrite { | ||
return 0, errors.Errorf("file [%s] exists", path) | ||
} | ||
w, err := f.FileWriter(path, true, false) | ||
if err != nil { | ||
return 0, errors.Wrapf(err, "unable to open file at path [%s]", path) | ||
} | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) | ||
if err != nil { | ||
return 0, errors.Wrapf(err, "unable to make request for url [%s]", url) | ||
} | ||
rsp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return 0, errors.Wrapf(err, "unable to load url [%s]", url) | ||
} | ||
defer func() { | ||
_ = rsp.Body.Close() | ||
}() | ||
if rsp.StatusCode != http.StatusOK { | ||
return 0, errors.Errorf("response from url [%s] has status [%d], expected [200]", url, rsp.StatusCode) | ||
} | ||
x, err := io.Copy(w, rsp.Body) | ||
if err != nil { | ||
return 0, errors.Wrapf(err, "unable to save response from url [%s] to file [%s]", url, path) | ||
} | ||
return int(x), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.