renaming and cleanup

This commit is contained in:
Niko Abeler 2023-07-09 19:09:54 +02:00
parent 723c94c576
commit 408bb88cb8
6 changed files with 50 additions and 3 deletions

View File

@ -14,11 +14,18 @@ type EntryRepository interface {
} }
type BinaryRepository interface { type BinaryRepository interface {
// Create creates a new binary file
// The name is the original file name, and is not unique
// BinaryFile.Id is a unique identifier
Create(name string, data []byte) (*model.BinaryFile, error) Create(name string, data []byte) (*model.BinaryFile, error)
FindById(id string) (*model.BinaryFile, error) FindById(id string) (*model.BinaryFile, error)
} }
type AuthorRepository interface { type AuthorRepository interface {
// Create creates a new author
// It returns an error if the name is already taken
Create(name string, passwordHash string) (*model.Author, error) Create(name string, passwordHash string) (*model.Author, error)
// FindByName finds an author by name
// It returns an error if the author is not found
FindByName(name string) (*model.Author, error) FindByName(name string) (*model.Author, error)
} }

View File

@ -1,7 +1,24 @@
package model package model
import (
"mime"
"strings"
)
type BinaryFile struct { type BinaryFile struct {
Id string Id string
Name string Name string
Data []byte Data []byte
} }
func (b *BinaryFile) Mime() string {
parts := strings.Split(b.Name, ".")
if len(parts) < 2 {
return "application/octet-stream"
}
t := mime.TypeByExtension("." + parts[len(parts)-1])
if t == "" {
return "application/octet-stream"
}
return t
}

View File

@ -0,0 +1,13 @@
package model_test
import (
"owl-blogs/domain/model"
"testing"
"github.com/stretchr/testify/require"
)
func TestMimeType(t *testing.T) {
bin := model.BinaryFile{Name: "test.jpg"}
require.Equal(t, "image/jpeg", bin.Mime())
}

View File

@ -3,6 +3,7 @@ package infra
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -18,6 +19,8 @@ type DefaultBinaryFileRepo struct {
db *sqlx.DB db *sqlx.DB
} }
// NewBinaryFileRepo creates a new binary file repository
// It creates the table if not exists
func NewBinaryFileRepo(db Database) repository.BinaryRepository { func NewBinaryFileRepo(db Database) repository.BinaryRepository {
sqlxdb := db.Get() sqlxdb := db.Get()
@ -33,8 +36,15 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
return &DefaultBinaryFileRepo{db: sqlxdb} return &DefaultBinaryFileRepo{db: sqlxdb}
} }
// Create implements repository.BinaryRepository
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.BinaryFile, error) { func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.BinaryFile, error) {
id := uuid.New().String() id := uuid.New().String()
parts := strings.Split(name, ".")
if len(parts) > 1 {
ext := parts[len(parts)-1]
id = id + "." + ext
}
_, err := repo.db.Exec("INSERT INTO binary_files (id, name, data) VALUES (?, ?, ?)", id, name, data) _, err := repo.db.Exec("INSERT INTO binary_files (id, name, data) VALUES (?, ?, ?)", id, name, data)
if err != nil { if err != nil {
return nil, err return nil, err
@ -42,6 +52,7 @@ func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.Bina
return &model.BinaryFile{Id: id, Name: name, Data: data}, nil return &model.BinaryFile{Id: id, Name: name, Data: data}, nil
} }
// FindById implements repository.BinaryRepository
func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error) { func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error) {
var sqlFile sqlBinaryFile var sqlFile sqlBinaryFile
err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE id = ?", id) err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE id = ?", id)

View File

@ -47,12 +47,11 @@ func NewWebApp(
app.Get("/", indexHandler.Handle) app.Get("/", indexHandler.Handle)
app.Get("/lists/:list/", listHandler.Handle) app.Get("/lists/:list/", listHandler.Handle)
// Media // Media
app.Get("/media/:id", mediaHandler.Handle) app.Get("/media/+", mediaHandler.Handle)
// RSS // RSS
app.Get("/index.xml", rssHandler.Handle) app.Get("/index.xml", rssHandler.Handle)
// Posts // Posts
app.Get("/posts/:post/", entryHandler.Handle) app.Get("/posts/:post/", entryHandler.Handle)
app.Get("/posts/:post/media/*filepath", mediaHandler.Handle)
// Webmention // Webmention
// app.Post("/webmention/", userWebmentionHandler(repo)) // app.Post("/webmention/", userWebmentionHandler(repo))
// Micropub // Micropub

View File

@ -15,7 +15,7 @@ func NewMediaHandler(binaryService *app.BinaryService) *MediaHandler {
} }
func (h *MediaHandler) Handle(c *fiber.Ctx) error { func (h *MediaHandler) Handle(c *fiber.Ctx) error {
id := c.Params("id") id := c.Params("+")
binary, err := h.binaryService.FindById(id) binary, err := h.binaryService.FindById(id)
if err != nil { if err != nil {
return err return err