Redmage/api/bmessage/bmessage.go

94 lines
1.7 KiB
Go
Raw Normal View History

2024-04-10 22:38:19 +07:00
package bmessage
import (
2024-04-26 22:13:04 +07:00
"encoding/json"
2024-04-10 22:38:19 +07:00
"github.com/alecthomas/units"
)
type ImageMetadata struct {
2024-04-11 16:04:13 +07:00
Kind ImageKind
URL string
2024-04-14 00:32:55 +07:00
Height int64
Width int64
2024-04-10 22:38:19 +07:00
}
2024-04-11 16:04:13 +07:00
type ImageKind int
func (im ImageKind) String() string {
switch im {
case KindThumbnail:
return "Thumbnail"
default:
return "Image"
}
}
func (im ImageKind) MarshalJSON() ([]byte, error) {
return []byte(`"` + im.String() + `"`), nil
}
2024-04-11 16:04:13 +07:00
const (
KindImage ImageKind = iota
KindThumbnail
)
2024-04-14 00:32:55 +07:00
type DownloadEvent int
func (do DownloadEvent) MarshalJSON() ([]byte, error) {
return []byte(`"` + do.String() + `"`), nil
}
func (do DownloadEvent) String() string {
switch do {
case DownloadStart:
return "DownloadStart"
case DownloadProgress:
return "DownloadProgress"
case DownloadEnd:
return "DownloadEnd"
case DownloadError:
return "DownloadError"
default:
return "Unknown"
}
}
const (
DownloadStart DownloadEvent = iota
DownloadProgress
DownloadEnd
DownloadError
)
2024-04-11 16:04:13 +07:00
type ImageDownloadMessage struct {
2024-04-26 22:13:04 +07:00
Event DownloadEvent `json:"event"`
Metadata ImageMetadata `json:"metadata"`
ContentLength units.MetricBytes `json:"content_length"`
Downloaded units.MetricBytes `json:"downloaded"`
Subreddit string `json:"subreddit"`
PostURL string `json:"post_url"`
PostID string `json:"post_id"`
Error error `json:"error"`
}
func (im ImageDownloadMessage) MarshalJSON() ([]byte, error) {
type Alias ImageDownloadMessage
type W struct {
Alias
Error string `json:"error"`
}
errMsg := ""
if im.Error != nil {
errMsg = im.Error.Error()
}
w := W{
Alias: Alias(im),
Error: errMsg,
}
return json.Marshal(w)
2024-04-10 22:38:19 +07:00
}