v2 #43
|
@ -14,11 +14,18 @@ type EntryRepository 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)
|
||||
FindById(id string) (*model.BinaryFile, error)
|
||||
}
|
||||
|
||||
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)
|
||||
// FindByName finds an author by name
|
||||
// It returns an error if the author is not found
|
||||
FindByName(name string) (*model.Author, error)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,24 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BinaryFile struct {
|
||||
Id string
|
||||
Name string
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
|
@ -3,6 +3,7 @@ package infra
|
|||
import (
|
||||
"owl-blogs/app/repository"
|
||||
"owl-blogs/domain/model"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
@ -18,6 +19,8 @@ type DefaultBinaryFileRepo struct {
|
|||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// NewBinaryFileRepo creates a new binary file repository
|
||||
// It creates the table if not exists
|
||||
func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
||||
sqlxdb := db.Get()
|
||||
|
||||
|
@ -33,8 +36,15 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
|||
return &DefaultBinaryFileRepo{db: sqlxdb}
|
||||
}
|
||||
|
||||
// Create implements repository.BinaryRepository
|
||||
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.BinaryFile, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
// FindById implements repository.BinaryRepository
|
||||
func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error) {
|
||||
var sqlFile sqlBinaryFile
|
||||
err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE id = ?", id)
|
||||
|
|
|
@ -47,12 +47,11 @@ func NewWebApp(
|
|||
app.Get("/", indexHandler.Handle)
|
||||
app.Get("/lists/:list/", listHandler.Handle)
|
||||
// Media
|
||||
app.Get("/media/:id", mediaHandler.Handle)
|
||||
app.Get("/media/+", mediaHandler.Handle)
|
||||
// RSS
|
||||
app.Get("/index.xml", rssHandler.Handle)
|
||||
// Posts
|
||||
app.Get("/posts/:post/", entryHandler.Handle)
|
||||
app.Get("/posts/:post/media/*filepath", mediaHandler.Handle)
|
||||
// Webmention
|
||||
// app.Post("/webmention/", userWebmentionHandler(repo))
|
||||
// Micropub
|
||||
|
|
|
@ -15,7 +15,7 @@ func NewMediaHandler(binaryService *app.BinaryService) *MediaHandler {
|
|||
}
|
||||
|
||||
func (h *MediaHandler) Handle(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
id := c.Params("+")
|
||||
binary, err := h.binaryService.FindById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue