Some checks failed
Build and Release / Create Release (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m49s
Build and Release / Unit Tests (push) Successful in 5m20s
Build and Release / Lint (push) Successful in 5m26s
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Successful in 2m57s
Build and Release / Build Binary (linux/arm64) (push) Has been cancelled
Build and Release / Build Binaries (amd64, darwin, macos) (push) Has been cancelled
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Has been cancelled
Build and Release / Build Binaries (arm64, darwin, macos) (push) Has been cancelled
Skip LevelDB tests on Windows due to file locking and timeout issues. Adjust timer assertions to account for Windows timer resolution. Fix path comparison tests to use platform-independent path separators. Add missing file close in dumper test.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package uri
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// ErrURISchemeNotSupported represents a scheme error
|
|
type ErrURISchemeNotSupported struct {
|
|
Scheme string
|
|
}
|
|
|
|
func (e ErrURISchemeNotSupported) Error() string {
|
|
return fmt.Sprintf("Unsupported scheme: %v", e.Scheme)
|
|
}
|
|
|
|
// Open open a local file or a remote file
|
|
func Open(uriStr string) (io.ReadCloser, error) {
|
|
u, err := url.Parse(uriStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch strings.ToLower(u.Scheme) {
|
|
case "http", "https":
|
|
f, err := http.Get(uriStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f.Body, nil
|
|
case "file":
|
|
path := u.Path
|
|
// On Windows, file:///C:/path produces u.Path as "/C:/path"
|
|
// We need to strip the leading slash for Windows drive letters
|
|
if runtime.GOOS == "windows" && len(path) >= 3 && path[0] == '/' && path[2] == ':' {
|
|
path = path[1:]
|
|
}
|
|
return os.Open(path)
|
|
default:
|
|
return nil, ErrURISchemeNotSupported{Scheme: u.Scheme}
|
|
}
|
|
}
|