Add custom types for type safety throughout codebase
- Add FileCount, FileSize, RelFilePath, AbsFilePath, ModTime, Multihash types - Add UnixSeconds and UnixNanos types for timestamp handling - Add URL types (ManifestURL, FileURL, BaseURL) with safe path joining - Consolidate scanner package into mfer package - Update checker to use custom types in Result and CheckStatus - Add ModTime.Timestamp() method for protobuf conversion - Update all tests to use proper custom types
This commit is contained in:
53
mfer/url.go
Normal file
53
mfer/url.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package mfer
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ManifestURL represents a URL pointing to a manifest file.
|
||||
type ManifestURL string
|
||||
|
||||
// FileURL represents a URL pointing to a file to be fetched.
|
||||
type FileURL string
|
||||
|
||||
// BaseURL represents a base URL for constructing file URLs.
|
||||
type BaseURL string
|
||||
|
||||
// JoinPath safely joins a relative file path to a base URL.
|
||||
// The path is properly URL-encoded to prevent path traversal.
|
||||
func (b BaseURL) JoinPath(path RelFilePath) (FileURL, error) {
|
||||
base, err := url.Parse(string(b))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Ensure base path ends with /
|
||||
if !strings.HasSuffix(base.Path, "/") {
|
||||
base.Path += "/"
|
||||
}
|
||||
|
||||
// Parse and encode the relative path
|
||||
ref, err := url.Parse(url.PathEscape(string(path)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resolved := base.ResolveReference(ref)
|
||||
return FileURL(resolved.String()), nil
|
||||
}
|
||||
|
||||
// String returns the URL as a string.
|
||||
func (b BaseURL) String() string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// String returns the URL as a string.
|
||||
func (f FileURL) String() string {
|
||||
return string(f)
|
||||
}
|
||||
|
||||
// String returns the URL as a string.
|
||||
func (m ManifestURL) String() string {
|
||||
return string(m)
|
||||
}
|
||||
Reference in New Issue
Block a user