2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
2024-10-15 21:13:46 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"mime"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
)
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
type OutOfLineContent struct {
|
|
|
|
*CommonAttributes
|
|
|
|
Type MediaType `xml:"type,attr,omitempty"`
|
|
|
|
SRC URI `xml:"src,attr"`
|
|
|
|
}
|
|
|
|
|
2024-10-15 21:13:46 +02:00
|
|
|
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
|
|
|
|
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
|
|
|
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.TypeOf(content).Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := url.ParseRequestURI(content.(string)); err != nil {
|
|
|
|
return nil, errors.New("content not a valid uri")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(URI)}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:32:14 +02:00
|
|
|
func (o *OutOfLineContent) isContent() bool { return true }
|
|
|
|
|
|
|
|
func (o *OutOfLineContent) hasSRC() bool { return true }
|
|
|
|
|
|
|
|
func (o *OutOfLineContent) getType() string { return string(o.Type) }
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
func (o *OutOfLineContent) Check() error {
|
2024-10-15 19:53:17 +02:00
|
|
|
if isCompositeMediaType(o.getType()) {
|
|
|
|
return errors.New("type attribute of out of line content must not be a composite type")
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
if o.SRC == "" {
|
|
|
|
return errors.New("src attribute of out of line content empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|