jx/internal/folio/uri.go

99 lines
1.9 KiB
Go
Raw Normal View History

2024-08-15 15:12:42 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
"net/url"
"decl/internal/transport"
"decl/internal/data"
"decl/internal/identifier"
"errors"
2024-09-19 07:57:26 +00:00
"strings"
2024-08-15 15:12:42 +00:00
)
var (
ErrInvalidURI error = errors.New("Invalid URI")
)
type URI identifier.ID
2024-09-19 07:57:26 +00:00
func NewURI(u *url.URL) URI {
return URI(u.String())
}
2024-08-15 15:12:42 +00:00
func (u URI) NewResource(document data.Document) (newResource data.Resource, err error) {
if document == nil {
declaration := NewDeclaration()
if err = declaration.NewResource((*string)(&u)); err == nil {
2024-08-18 01:19:56 +00:00
return declaration.Attributes, err
2024-08-15 15:12:42 +00:00
}
} else {
newResource, err = document.NewResource(string(u))
return
}
return
}
func (u URI) ConstructResource(res data.Resource) (err error) {
parsedURI := u.Parse()
if ri, ok := res.(data.ResourceInitializer); ok {
err = ri.Init(parsedURI)
} else {
err = res.SetParsedURI(parsedURI)
}
return
}
func (u URI) Converter() (converter data.Converter, err error) {
return DocumentRegistry.ConverterTypes.New(string(u))
}
func (u URI) Parse() data.URIParser {
2024-08-15 15:12:42 +00:00
url, e := url.Parse(string(u))
if e == nil {
return (*ParsedURI)(url)
2024-08-15 15:12:42 +00:00
}
return nil
}
func (u URI) Exists() bool {
return transport.ExistsURI(string(u))
}
func (u URI) ContentReaderStream() (*transport.Reader, error) {
return transport.NewReaderURI(string(u))
}
func (u URI) ContentWriterStream() (*transport.Writer, error) {
return transport.NewWriterURI(string(u))
}
func (u URI) String() string {
return string(u)
}
func (u *URI) SetURL(url *url.URL) {
(*identifier.ID)(u).SetURL(url)
}
func (u URI) Extension() (string, string) {
return (identifier.ID)(u).Extension()
}
2024-09-19 07:57:26 +00:00
func (u URI) ContentType() string {
var ext strings.Builder
exttype, fileext := u.Extension()
if fileext == "" {
return exttype
}
ext.WriteString(exttype)
ext.WriteRune('.')
ext.WriteString(fileext)
return ext.String()
}
func (u URI) IsEmpty() bool {
return (identifier.ID)(u).IsEmpty()
}