2023-09-11 15:43:09 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
2023-09-11 16:25:39 +00:00
|
|
|
package formats
|
2023-09-11 15:43:09 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-11 17:09:08 +00:00
|
|
|
"errors"
|
2023-09-11 15:43:09 +00:00
|
|
|
"fmt"
|
2023-09-11 17:09:08 +00:00
|
|
|
"image"
|
2023-09-11 15:43:09 +00:00
|
|
|
_ "image/gif"
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
2023-09-11 17:09:08 +00:00
|
|
|
"os"
|
2023-09-11 15:43:09 +00:00
|
|
|
|
|
|
|
_ "golang.org/x/image/bmp"
|
|
|
|
_ "golang.org/x/image/webp"
|
|
|
|
)
|
|
|
|
|
2023-09-11 17:09:08 +00:00
|
|
|
type Dimensions struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2023-09-11 16:25:39 +00:00
|
|
|
func RegisterImageFormats() *SupportedFormat {
|
|
|
|
return &SupportedFormat{
|
2023-09-12 18:06:45 +00:00
|
|
|
Css: ``,
|
2023-09-11 17:09:08 +00:00
|
|
|
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
|
|
|
dimensions, err := ImageDimensions(filePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:43:09 +00:00
|
|
|
return fmt.Sprintf(`<title>%s (%dx%d)</title>`,
|
|
|
|
fileName,
|
2023-09-11 17:09:08 +00:00
|
|
|
dimensions.Width,
|
|
|
|
dimensions.Height)
|
2023-09-11 15:43:09 +00:00
|
|
|
},
|
2023-09-11 17:09:08 +00:00
|
|
|
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
|
|
|
dimensions, err := ImageDimensions(filePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:43:09 +00:00
|
|
|
return fmt.Sprintf(`<a href="/%s"><img src="%s" width="%d" height="%d" type="%s" alt="Roulette selected: %s"></a>`,
|
|
|
|
queryParams,
|
2023-09-11 17:09:08 +00:00
|
|
|
fileUri,
|
|
|
|
dimensions.Width,
|
|
|
|
dimensions.Height,
|
2023-09-11 15:43:09 +00:00
|
|
|
mime,
|
|
|
|
fileName)
|
|
|
|
},
|
2023-09-11 16:25:39 +00:00
|
|
|
Extensions: []string{
|
2023-09-12 02:22:26 +00:00
|
|
|
`.apng`,
|
|
|
|
`.avif`,
|
2023-09-11 15:43:09 +00:00
|
|
|
`.bmp`,
|
|
|
|
`.gif`,
|
|
|
|
`.jpg`,
|
2023-09-11 21:05:38 +00:00
|
|
|
`.jpeg`,
|
2023-09-12 02:22:26 +00:00
|
|
|
`.jfif`,
|
|
|
|
`.pjp`,
|
|
|
|
`.pjpeg`,
|
2023-09-11 15:43:09 +00:00
|
|
|
`.png`,
|
2023-09-12 02:22:26 +00:00
|
|
|
`.svg`,
|
2023-09-11 15:43:09 +00:00
|
|
|
`.webp`,
|
|
|
|
},
|
2023-09-11 21:05:38 +00:00
|
|
|
MimeTypes: []string{
|
2023-09-12 02:22:26 +00:00
|
|
|
`image/apng`,
|
|
|
|
`image/avif`,
|
2023-09-11 21:05:38 +00:00
|
|
|
`image/bmp`,
|
|
|
|
`image/gif`,
|
|
|
|
`image/jpeg`,
|
|
|
|
`image/png`,
|
2023-09-12 02:22:26 +00:00
|
|
|
`image/svg+xml`,
|
2023-09-11 21:05:38 +00:00
|
|
|
`image/webp`,
|
2023-09-11 15:43:09 +00:00
|
|
|
},
|
2023-09-12 00:38:38 +00:00
|
|
|
Validate: func(filePath string) bool {
|
|
|
|
return true
|
|
|
|
},
|
2023-09-11 15:43:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-11 17:09:08 +00:00
|
|
|
|
|
|
|
func ImageDimensions(path string) (*Dimensions, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
fmt.Printf("File %s does not exist\n", path)
|
|
|
|
return &Dimensions{}, nil
|
|
|
|
case err != nil:
|
|
|
|
fmt.Printf("File %s open returned error: %s\n", path, err)
|
|
|
|
return &Dimensions{}, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
decodedConfig, _, err := image.DecodeConfig(file)
|
2023-09-11 17:09:08 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, image.ErrFormat):
|
|
|
|
fmt.Printf("File %s has invalid image format\n", path)
|
|
|
|
return &Dimensions{Width: 0, Height: 0}, nil
|
|
|
|
case err != nil:
|
|
|
|
fmt.Printf("File %s decode returned error: %s\n", path, err)
|
|
|
|
return &Dimensions{}, err
|
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
return &Dimensions{Width: decodedConfig.Width, Height: decodedConfig.Height}, nil
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|