v2 #43
|
@ -14,7 +14,7 @@ func NewBinaryFileService(repo repository.BinaryRepository) *BinaryService {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BinaryService) Create(name string, file []byte) (*model.BinaryFile, error) {
|
func (s *BinaryService) Create(name string, file []byte) (*model.BinaryFile, error) {
|
||||||
return s.repo.Create(name, file)
|
return s.repo.Create(name, file, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *BinaryService) FindById(id string) (*model.BinaryFile, error) {
|
func (s *BinaryService) FindById(id string) (*model.BinaryFile, error) {
|
||||||
|
|
|
@ -17,8 +17,9 @@ type BinaryRepository interface {
|
||||||
// Create creates a new binary file
|
// Create creates a new binary file
|
||||||
// The name is the original file name, and is not unique
|
// The name is the original file name, and is not unique
|
||||||
// BinaryFile.Id is a unique identifier
|
// BinaryFile.Id is a unique identifier
|
||||||
Create(name string, data []byte) (*model.BinaryFile, error)
|
Create(name string, data []byte, entry model.Entry) (*model.BinaryFile, error)
|
||||||
FindById(id string) (*model.BinaryFile, error)
|
FindById(id string) (*model.BinaryFile, error)
|
||||||
|
FindByNameForEntry(name string, entry model.Entry) (*model.BinaryFile, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthorRepository interface {
|
type AuthorRepository interface {
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(importCmd)
|
||||||
|
|
||||||
|
importCmd.Flags().StringVarP(&user, "path", "p", "", "Path to the user folder")
|
||||||
|
importCmd.MarkFlagRequired("path")
|
||||||
|
}
|
||||||
|
|
||||||
|
var importCmd = &cobra.Command{
|
||||||
|
Use: "import",
|
||||||
|
Short: "Import data from v1",
|
||||||
|
Long: `Import data from v1`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// db := infra.NewSqliteDB(DbPath)
|
||||||
|
// App(db).ImportV1()
|
||||||
|
|
||||||
|
// TODO: Implement this
|
||||||
|
// For each folder in the user folder
|
||||||
|
// Map to entry types
|
||||||
|
// Convert and save
|
||||||
|
// Import Binary files
|
||||||
|
},
|
||||||
|
}
|
|
@ -1,17 +1,18 @@
|
||||||
package infra
|
package infra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"owl-blogs/app/repository"
|
"owl-blogs/app/repository"
|
||||||
"owl-blogs/domain/model"
|
"owl-blogs/domain/model"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sqlBinaryFile struct {
|
type sqlBinaryFile struct {
|
||||||
Id string `db:"id"`
|
Id string `db:"id"`
|
||||||
Name string `db:"name"`
|
Name string `db:"name"`
|
||||||
|
EntryId *string `db:"entry_id"`
|
||||||
Data []byte `db:"data"`
|
Data []byte `db:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +30,7 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
||||||
CREATE TABLE IF NOT EXISTS binary_files (
|
CREATE TABLE IF NOT EXISTS binary_files (
|
||||||
id VARCHAR(255) PRIMARY KEY,
|
id VARCHAR(255) PRIMARY KEY,
|
||||||
name VARCHAR(255) NOT NULL,
|
name VARCHAR(255) NOT NULL,
|
||||||
|
entry_id VARCHAR(255),
|
||||||
data BLOB NOT NULL
|
data BLOB NOT NULL
|
||||||
);
|
);
|
||||||
`)
|
`)
|
||||||
|
@ -37,15 +39,41 @@ func NewBinaryFileRepo(db Database) repository.BinaryRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create implements repository.BinaryRepository
|
// Create implements repository.BinaryRepository
|
||||||
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte) (*model.BinaryFile, error) {
|
func (repo *DefaultBinaryFileRepo) Create(name string, data []byte, entry model.Entry) (*model.BinaryFile, error) {
|
||||||
id := uuid.New().String()
|
|
||||||
parts := strings.Split(name, ".")
|
parts := strings.Split(name, ".")
|
||||||
if len(parts) > 1 {
|
fileName := strings.Join(parts[:len(parts)-1], ".")
|
||||||
ext := parts[len(parts)-1]
|
fileExt := parts[len(parts)-1]
|
||||||
id = id + "." + ext
|
id := fileName + "." + fileExt
|
||||||
|
|
||||||
|
// check if id exists
|
||||||
|
var count int
|
||||||
|
err := repo.db.Get(&count, "SELECT COUNT(*) FROM binary_files WHERE id = ?", id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := repo.db.Exec("INSERT INTO binary_files (id, name, data) VALUES (?, ?, ?)", id, name, data)
|
if count > 0 {
|
||||||
|
counter := 1
|
||||||
|
for {
|
||||||
|
id = fmt.Sprintf("%s-%d.%s", fileName, counter, fileExt)
|
||||||
|
err := repo.db.Get(&count, "SELECT COUNT(*) FROM binary_files WHERE id = ?", id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
counter++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var entryId *string
|
||||||
|
if entry != nil {
|
||||||
|
eId := entry.ID()
|
||||||
|
entryId = &eId
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = repo.db.Exec("INSERT INTO binary_files (id, name, entry_id, data) VALUES (?, ?, ?, ?)", id, name, entryId, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -61,3 +89,13 @@ func (repo *DefaultBinaryFileRepo) FindById(id string) (*model.BinaryFile, error
|
||||||
}
|
}
|
||||||
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindByNameForEntry implements repository.BinaryRepository
|
||||||
|
func (repo *DefaultBinaryFileRepo) FindByNameForEntry(name string, entry model.Entry) (*model.BinaryFile, error) {
|
||||||
|
var sqlFile sqlBinaryFile
|
||||||
|
err := repo.db.Get(&sqlFile, "SELECT * FROM binary_files WHERE name = ? AND entry_id = ?", name, entry.ID())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &model.BinaryFile{Id: sqlFile.Id, Name: sqlFile.Name, Data: sqlFile.Data}, nil
|
||||||
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ func setupBinaryRepo() repository.BinaryRepository {
|
||||||
func TestBinaryRepoCreate(t *testing.T) {
|
func TestBinaryRepoCreate(t *testing.T) {
|
||||||
repo := setupBinaryRepo()
|
repo := setupBinaryRepo()
|
||||||
|
|
||||||
file, err := repo.Create("name", []byte("😀 😃 😄 😁"))
|
file, err := repo.Create("name", []byte("😀 😃 😄 😁"), nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
file, err = repo.FindById(file.Id)
|
file, err = repo.FindById(file.Id)
|
||||||
|
@ -30,10 +30,10 @@ func TestBinaryRepoCreate(t *testing.T) {
|
||||||
func TestBinaryRepoNoSideEffect(t *testing.T) {
|
func TestBinaryRepoNoSideEffect(t *testing.T) {
|
||||||
repo := setupBinaryRepo()
|
repo := setupBinaryRepo()
|
||||||
|
|
||||||
file, err := repo.Create("name1", []byte("111"))
|
file, err := repo.Create("name1", []byte("111"), nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
file2, err := repo.Create("name2", []byte("222"))
|
file2, err := repo.Create("name2", []byte("222"), nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
file, err = repo.FindById(file.Id)
|
file, err = repo.FindById(file.Id)
|
||||||
|
|
Loading…
Reference in New Issue