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 811B

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