236 lines
5.4 KiB
Go
236 lines
5.4 KiB
Go
// Package kunstzeal selects projections and executes bounded rendering providers.
|
|
// Authentication, source provenance and canonical storage remain caller-owned.
|
|
package kunstzeal
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type Delivery string
|
|
|
|
const (
|
|
DeliveryHTML Delivery = "html"
|
|
DeliveryText Delivery = "text"
|
|
DeliveryRawMedia Delivery = "raw-media"
|
|
DeliveryRawFrame Delivery = "raw-frame"
|
|
DeliveryDownload Delivery = "download"
|
|
)
|
|
|
|
type Descriptor struct {
|
|
MediaType string
|
|
Filename string
|
|
SizeBytes int64
|
|
}
|
|
|
|
type Adapter struct {
|
|
ID string
|
|
Kind string
|
|
Delivery Delivery
|
|
NeedsText bool
|
|
WindowableText bool
|
|
Match func(Descriptor) bool
|
|
}
|
|
|
|
const InlineTextWindowThreshold = int64(2 << 20)
|
|
|
|
var projectionAdapters = []Adapter{
|
|
{
|
|
ID: "html",
|
|
Kind: "html",
|
|
Delivery: DeliveryHTML,
|
|
Match: mediaTypes("text/html"),
|
|
},
|
|
{
|
|
ID: "excalidraw",
|
|
Kind: "excalidraw",
|
|
Delivery: DeliveryText,
|
|
NeedsText: true,
|
|
WindowableText: true,
|
|
Match: extensions(".excalidraw"),
|
|
},
|
|
{
|
|
ID: "csv",
|
|
Kind: "csv",
|
|
Delivery: DeliveryText,
|
|
NeedsText: true,
|
|
WindowableText: true,
|
|
Match: matchAny(
|
|
mediaTypes("text/csv", "application/csv"),
|
|
extensions(".csv"),
|
|
),
|
|
},
|
|
{
|
|
ID: "archive",
|
|
Kind: "archive",
|
|
Delivery: DeliveryDownload,
|
|
Match: matchAny(
|
|
mediaTypes(
|
|
"application/zip", "application/x-zip-compressed",
|
|
"application/x-rar-compressed", "application/vnd.rar",
|
|
"application/x-7z-compressed", "application/x-tar",
|
|
"application/gzip", "application/x-xz",
|
|
),
|
|
extensions(".zip", ".rar", ".7z", ".tar", ".gz", ".tgz", ".xz", ".txz"),
|
|
),
|
|
},
|
|
{
|
|
ID: "debian-package",
|
|
Kind: "metadata",
|
|
Delivery: DeliveryDownload,
|
|
Match: matchAny(
|
|
mediaTypes(
|
|
"application/vnd.debian.binary-package",
|
|
"application/x-debian-package",
|
|
),
|
|
extensions(".deb"),
|
|
),
|
|
},
|
|
{
|
|
ID: "eml",
|
|
Kind: "email",
|
|
Delivery: DeliveryText,
|
|
Match: matchAny(
|
|
mediaTypes("message/rfc822"),
|
|
extensions(".eml"),
|
|
),
|
|
},
|
|
{
|
|
ID: "pe",
|
|
Kind: "metadata",
|
|
Delivery: DeliveryDownload,
|
|
Match: extensions(".exe", ".dll", ".sys"),
|
|
},
|
|
{
|
|
ID: "json",
|
|
Kind: "json",
|
|
Delivery: DeliveryText,
|
|
WindowableText: true,
|
|
Match: matchAny(mediaTypes("application/json", "application/ld+json"), extensions(".json", ".jsonl")),
|
|
},
|
|
{
|
|
ID: "text",
|
|
Kind: "code",
|
|
Delivery: DeliveryText,
|
|
NeedsText: true,
|
|
WindowableText: true,
|
|
Match: textMediaType,
|
|
},
|
|
{
|
|
ID: "image",
|
|
Kind: "image",
|
|
Delivery: DeliveryRawMedia,
|
|
Match: matchAny(
|
|
mediaTypePrefix("image/"),
|
|
extensions(".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif", ".svg"),
|
|
),
|
|
},
|
|
{
|
|
ID: "audio",
|
|
Kind: "audio",
|
|
Delivery: DeliveryRawMedia,
|
|
Match: matchAny(
|
|
mediaTypePrefix("audio/"),
|
|
extensions(".mp3", ".flac", ".wav", ".ogg", ".m4a", ".aac", ".opus"),
|
|
),
|
|
},
|
|
{
|
|
ID: "video",
|
|
Kind: "video",
|
|
Delivery: DeliveryRawMedia,
|
|
Match: matchAny(
|
|
mediaTypePrefix("video/"),
|
|
extensions(".mp4", ".webm", ".mkv", ".mov", ".m4v", ".ogv"),
|
|
),
|
|
},
|
|
{
|
|
ID: "pdf",
|
|
Kind: "pdf",
|
|
Delivery: DeliveryRawFrame,
|
|
Match: matchAny(
|
|
mediaTypes("application/pdf"),
|
|
extensions(".pdf"),
|
|
),
|
|
},
|
|
{
|
|
ID: "download",
|
|
Kind: "download",
|
|
Delivery: DeliveryDownload,
|
|
Match: func(Descriptor) bool { return true },
|
|
},
|
|
}
|
|
|
|
func SelectAdapter(descriptor Descriptor) Adapter {
|
|
for _, adapter := range projectionAdapters {
|
|
if adapter.Match(descriptor) {
|
|
return adapter
|
|
}
|
|
}
|
|
return projectionAdapters[len(projectionAdapters)-1]
|
|
}
|
|
|
|
func mediaTypes(types ...string) func(Descriptor) bool {
|
|
return func(descriptor Descriptor) bool {
|
|
for _, candidate := range types {
|
|
if descriptor.MediaType == candidate {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
func mediaTypePrefix(prefix string) func(Descriptor) bool {
|
|
return func(descriptor Descriptor) bool {
|
|
return strings.HasPrefix(descriptor.MediaType, prefix)
|
|
}
|
|
}
|
|
|
|
func extensions(values ...string) func(Descriptor) bool {
|
|
return func(descriptor Descriptor) bool {
|
|
extension := strings.ToLower(filepath.Ext(descriptor.Filename))
|
|
for _, candidate := range values {
|
|
if extension == candidate {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
func matchAny(matchers ...func(Descriptor) bool) func(Descriptor) bool {
|
|
return func(descriptor Descriptor) bool {
|
|
for _, matcher := range matchers {
|
|
if matcher(descriptor) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
func textMediaType(descriptor Descriptor) bool {
|
|
switch descriptor.MediaType {
|
|
case "application/json",
|
|
"application/xml",
|
|
"application/javascript",
|
|
"application/x-javascript",
|
|
"application/x-sh",
|
|
"application/x-shellscript",
|
|
"application/x-yaml",
|
|
"application/yaml",
|
|
"application/toml":
|
|
return true
|
|
default:
|
|
return strings.HasPrefix(descriptor.MediaType, "text/")
|
|
}
|
|
}
|
|
|
|
// Catalog returns a copy; callers cannot reorder or disable the shared registry.
|
|
func Catalog() []Adapter {
|
|
items := append([]Adapter(nil), projectionAdapters...)
|
|
// Explicitly selected projection. It must not replace normal HTML/text
|
|
// viewing in the automatic MIME classifier.
|
|
return append(items, Adapter{ID: "reveal", Kind: "slides", Delivery: DeliveryRawFrame, Match: func(Descriptor) bool { return false }})
|
|
}
|