Refactor AttachImage

This commit is contained in:
Trevor Slocum 2024-01-15 12:49:51 -08:00
parent f52606c828
commit e9e3651ec7
3 changed files with 18 additions and 16 deletions

View File

@ -16,7 +16,7 @@ type AppConfig struct {
// See https://pkg.go.dev/github.com/lib/pq for a full list of options.
//
// When using the "mysql" driver, DataSource may be set as follows:
// username:password@localhost)/dbname?allowFallbackToPlaintext=true
// username:password@localhost/dbname?allowFallbackToPlaintext=true
// See https://github.com/go-sql-driver/mysql/#dsn-data-source-name for a full list of options.
//
// When using the "sqlite" driver, Datasource may be set as follows:

View File

@ -7,7 +7,7 @@ import (
"code.rocket9labs.com/tslocum/sriracha"
"code.rocket9labs.com/tslocum/sriracha/extension"
// Load database drivers. SQLite and PostgreSQL are supported by default.
// Load database drivers. PostgreSQL, MySQL and SQLite are supported by default.
_ "github.com/glebarez/go-sqlite"
_ "github.com/glebarez/go-sqlite/compat"
_ "github.com/go-sql-driver/mysql"
@ -20,11 +20,12 @@ func main() {
os.Setenv("SRIRACHA_DRIVER", "")
os.Setenv("SRIRACHA_DATASOURCE", "")
// Register extensions.
// Extensions are processed in the order they are added.
// Register extensions. Extensions are processed in the order they are added.
// Register file attachment extensions.
sriracha.AddExtension(extension.NewAttachImage())
sriracha.AddExtension(extension.NewAttachImage("image/jpeg", "jpg"))
sriracha.AddExtension(extension.NewAttachImage("image/gif", "gif"))
sriracha.AddExtension(extension.NewAttachImage("image/png", "png"))
// Register post handling extensions.
sriracha.AddExtension(extension.NewHelloWorld())

View File

@ -1,36 +1,37 @@
package extension
import (
"fmt"
"io"
"log"
"strings"
"code.rocket9labs.com/tslocum/sriracha"
)
type attachImage struct {
mime string
fileExtension string
}
var _ sriracha.ExtensionAttach = &attachImage{}
func NewAttachImage() *attachImage {
return &attachImage{}
func NewAttachImage(mime string, fileExtension string) *attachImage {
return &attachImage{
mime: mime,
fileExtension: fileExtension,
}
}
func (a *attachImage) Init(settings *sriracha.ExtensionSettings) string {
return "Handles attaching images."
return fmt.Sprintf("Handles attaching %s images.", strings.ToUpper(a.fileExtension))
}
func (a *attachImage) Types() []*sriracha.AttachmentType {
return []*sriracha.AttachmentType{
{
MIME: "image/jpeg",
Extension: "jpg",
}, {
MIME: "image/gif",
Extension: "gif",
}, {
MIME: "image/x-png",
Extension: "png",
MIME: a.mime,
Extension: a.fileExtension,
},
}
}