You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri.go 883B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package uri
  5. import (
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strings"
  12. )
  13. // ErrURISchemeNotSupported represents a scheme error
  14. type ErrURISchemeNotSupported struct {
  15. Scheme string
  16. }
  17. func (e ErrURISchemeNotSupported) Error() string {
  18. return fmt.Sprintf("Unsupported scheme: %v", e.Scheme)
  19. }
  20. // Open open a local file or a remote file
  21. func Open(uriStr string) (io.ReadCloser, error) {
  22. u, err := url.Parse(uriStr)
  23. if err != nil {
  24. return nil, err
  25. }
  26. switch strings.ToLower(u.Scheme) {
  27. case "http", "https":
  28. f, err := http.Get(uriStr)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return f.Body, nil
  33. case "file":
  34. return os.Open(u.Path)
  35. default:
  36. return nil, ErrURISchemeNotSupported{Scheme: u.Scheme}
  37. }
  38. }