2023-11-09 00:44:05 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
2023-11-10 03:06:16 +00:00
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
"io"
|
2023-11-09 00:44:05 +00:00
|
|
|
"mime"
|
2023-11-10 03:06:16 +00:00
|
|
|
"path"
|
2023-11-09 00:44:05 +00:00
|
|
|
"time"
|
2023-11-10 03:06:16 +00:00
|
|
|
|
|
|
|
_ "golang.org/x/image/bmp"
|
|
|
|
_ "golang.org/x/image/webp"
|
2023-11-09 00:44:05 +00:00
|
|
|
)
|
|
|
|
|
2023-11-09 22:34:37 +00:00
|
|
|
type Resource struct {
|
2023-11-10 03:06:16 +00:00
|
|
|
modTime time.Time
|
2024-02-03 14:36:26 +00:00
|
|
|
params map[string]any
|
|
|
|
File Path
|
2023-11-10 03:06:16 +00:00
|
|
|
mediaType MediaType
|
|
|
|
key string
|
2024-02-03 14:36:26 +00:00
|
|
|
name string
|
2023-11-10 03:06:16 +00:00
|
|
|
resourceType ResourceType
|
2024-02-03 14:36:26 +00:00
|
|
|
title string
|
2023-11-10 03:06:16 +00:00
|
|
|
image image.Config
|
|
|
|
}
|
|
|
|
|
2023-11-19 07:41:53 +00:00
|
|
|
func NewResource(modTime time.Time, r io.Reader, key string) *Resource {
|
2023-11-10 03:06:16 +00:00
|
|
|
mediaType, _, _ := mime.ParseMediaType(mime.TypeByExtension(path.Ext(key)))
|
|
|
|
out := &Resource{
|
2024-01-22 06:44:14 +00:00
|
|
|
File: NewPath(key),
|
2023-11-10 03:06:16 +00:00
|
|
|
modTime: modTime,
|
|
|
|
key: key,
|
|
|
|
name: key, // TODO(toby3d): set from Page configuration
|
|
|
|
title: "", // TODO(toby3d): set from Page configuration
|
|
|
|
params: make(map[string]any), // TODO(toby3d): set from Page configuration
|
|
|
|
mediaType: NewMediaType(mediaType),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch path.Ext(key) {
|
|
|
|
default:
|
2023-11-19 07:41:53 +00:00
|
|
|
out.resourceType = ResourceType(out.mediaType.mainType)
|
2023-11-10 03:06:16 +00:00
|
|
|
case ".md":
|
|
|
|
out.resourceType = ResourceTypePage
|
|
|
|
case ".webmanifest":
|
|
|
|
out.resourceType = ResourceTypeText
|
|
|
|
}
|
|
|
|
|
|
|
|
switch out.resourceType {
|
|
|
|
case ResourceTypeImage:
|
2023-11-19 07:41:53 +00:00
|
|
|
out.image, _, _ = image.DecodeConfig(r)
|
2023-11-10 03:06:16 +00:00
|
|
|
}
|
2023-11-09 21:52:56 +00:00
|
|
|
|
2023-11-10 03:06:16 +00:00
|
|
|
return out
|
2023-11-09 00:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 03:06:16 +00:00
|
|
|
func (r Resource) Key() string {
|
|
|
|
return r.key
|
2023-11-09 00:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 03:06:16 +00:00
|
|
|
func (r Resource) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
2023-11-09 00:44:05 +00:00
|
|
|
|
2023-11-10 03:06:16 +00:00
|
|
|
func (r Resource) MediaType() MediaType {
|
|
|
|
return r.mediaType
|
2023-11-09 00:44:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 03:06:16 +00:00
|
|
|
// Width returns width if current r is an image.
|
2024-02-03 14:36:26 +00:00
|
|
|
func (r Resource) Width() uint {
|
|
|
|
return uint(r.image.Width)
|
2023-11-10 03:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Height returns height if current r is an image.
|
2024-02-03 14:36:26 +00:00
|
|
|
func (r Resource) Height() uint {
|
|
|
|
return uint(r.image.Height)
|
2023-11-10 03:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Resource) ResourceType() ResourceType {
|
|
|
|
return r.resourceType
|
|
|
|
}
|
2023-11-09 00:44:05 +00:00
|
|
|
|
2023-11-09 22:34:37 +00:00
|
|
|
func (f Resource) GoString() string {
|
2023-11-10 03:06:16 +00:00
|
|
|
return "domain.Resource(" + f.key + ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceModTime used in http.ServeContent to get modtime of this resource.
|
|
|
|
func ResourceModTime(r *Resource) time.Time {
|
|
|
|
if r == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.modTime
|
2023-11-09 01:53:27 +00:00
|
|
|
}
|