From 1ae70dbd9124675d4a510954619b01edd5f1f6c3 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:21:56 +0200 Subject: ref: Move away other repositories Finish moving all repositories to a repository package. This should reduce the amount of packages. --- pkg/components/filesystem/controller.go | 6 ++-- pkg/components/filesystem/model.go | 10 ------ pkg/components/media/model.go | 64 --------------------------------- pkg/components/settings/model.go | 15 -------- pkg/database/repository/filesystem.go | 10 ++++++ pkg/database/repository/media.go | 64 +++++++++++++++++++++++++++++++++ pkg/database/repository/settings.go | 15 ++++++++ pkg/database/sql/media.go | 28 +++++++-------- pkg/database/sql/settings.go | 10 +++--- pkg/database/sql/user_test.go | 20 +++++------ pkg/fileop/exif.go | 8 ++--- pkg/view/filesystem.go | 6 ++-- pkg/view/media.go | 16 ++++----- pkg/view/settings.go | 9 +++-- pkg/worker/exif_scanner.go | 16 ++++----- pkg/worker/file_scanner.go | 8 ++--- 16 files changed, 153 insertions(+), 152 deletions(-) delete mode 100644 pkg/components/filesystem/model.go delete mode 100644 pkg/components/media/model.go delete mode 100644 pkg/components/settings/model.go create mode 100644 pkg/database/repository/filesystem.go create mode 100644 pkg/database/repository/media.go create mode 100644 pkg/database/repository/settings.go (limited to 'pkg') diff --git a/pkg/components/filesystem/controller.go b/pkg/components/filesystem/controller.go index 6b478a5..6c613a3 100644 --- a/pkg/components/filesystem/controller.go +++ b/pkg/components/filesystem/controller.go @@ -5,11 +5,13 @@ import ( "net/url" "path" "strings" + + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) type ( Controller struct { - repository Repository + repository repository.FileSystemRepository } DirectoryParam struct { @@ -28,7 +30,7 @@ type ( } ) -func NewController(repository Repository) *Controller { +func NewController(repository repository.FileSystemRepository) *Controller { return &Controller{ repository: repository, } diff --git a/pkg/components/filesystem/model.go b/pkg/components/filesystem/model.go deleted file mode 100644 index 2caed82..0000000 --- a/pkg/components/filesystem/model.go +++ /dev/null @@ -1,10 +0,0 @@ -package filesystem - -import "io/fs" - -type ( - Repository interface { - List(path string) ([]fs.FileInfo, error) - Stat(path string) (fs.FileInfo, error) - } -) diff --git a/pkg/components/media/model.go b/pkg/components/media/model.go deleted file mode 100644 index 1962a23..0000000 --- a/pkg/components/media/model.go +++ /dev/null @@ -1,64 +0,0 @@ -package media - -import ( - "context" - "strings" - "time" -) - -type ( - Media struct { - ID uint - Name string - Path string - PathHash string - MIMEType string - } - - MediaEXIF struct { - Width *float64 - Height *float64 - Description *string - Camera *string - Maker *string - Lens *string - DateShot *time.Time - Exposure *float64 - Aperture *float64 - Iso *int64 - FocalLength *float64 - Flash *int64 - Orientation *int64 - ExposureProgram *int64 - GPSLatitude *float64 - GPSLongitude *float64 - } - - Pagination struct { - Page int - Size int - } - - CreateMedia struct { - Name string - Path string - PathHash string - MIMEType string - } - - Repository interface { - Create(context.Context, *CreateMedia) error - Exists(context.Context, string) (bool, error) - List(context.Context, *Pagination) ([]*Media, error) - Get(context.Context, string) (*Media, error) - GetPath(context.Context, string) (string, error) - - GetEmptyEXIF(context.Context, *Pagination) ([]*Media, error) - GetEXIF(context.Context, uint) (*MediaEXIF, error) - CreateEXIF(context.Context, uint, *MediaEXIF) error - } -) - -func (m *Media) IsVideo() bool { - return strings.HasPrefix(m.MIMEType, "video") -} diff --git a/pkg/components/settings/model.go b/pkg/components/settings/model.go deleted file mode 100644 index da07f2c..0000000 --- a/pkg/components/settings/model.go +++ /dev/null @@ -1,15 +0,0 @@ -package settings - -import "context" - -type ( - Settings struct { - ShowMode bool - ShowOwner bool - } - - Repository interface { - Save(context.Context, *Settings) error - Load(context.Context) (*Settings, error) - } -) diff --git a/pkg/database/repository/filesystem.go b/pkg/database/repository/filesystem.go new file mode 100644 index 0000000..f553b3f --- /dev/null +++ b/pkg/database/repository/filesystem.go @@ -0,0 +1,10 @@ +package repository + +import "io/fs" + +type ( + FileSystemRepository interface { + List(path string) ([]fs.FileInfo, error) + Stat(path string) (fs.FileInfo, error) + } +) diff --git a/pkg/database/repository/media.go b/pkg/database/repository/media.go new file mode 100644 index 0000000..2e94ff3 --- /dev/null +++ b/pkg/database/repository/media.go @@ -0,0 +1,64 @@ +package repository + +import ( + "context" + "strings" + "time" +) + +type ( + Media struct { + ID uint + Name string + Path string + PathHash string + MIMEType string + } + + MediaEXIF struct { + Width *float64 + Height *float64 + Description *string + Camera *string + Maker *string + Lens *string + DateShot *time.Time + Exposure *float64 + Aperture *float64 + Iso *int64 + FocalLength *float64 + Flash *int64 + Orientation *int64 + ExposureProgram *int64 + GPSLatitude *float64 + GPSLongitude *float64 + } + + Pagination struct { + Page int + Size int + } + + CreateMedia struct { + Name string + Path string + PathHash string + MIMEType string + } + + MediaRepository interface { + Create(context.Context, *CreateMedia) error + Exists(context.Context, string) (bool, error) + List(context.Context, *Pagination) ([]*Media, error) + Get(context.Context, string) (*Media, error) + GetPath(context.Context, string) (string, error) + + GetEmptyEXIF(context.Context, *Pagination) ([]*Media, error) + GetEXIF(context.Context, uint) (*MediaEXIF, error) + CreateEXIF(context.Context, uint, *MediaEXIF) error + } +) + +func (m *Media) IsVideo() bool { + return strings.HasPrefix(m.MIMEType, "video") +} diff --git a/pkg/database/repository/settings.go b/pkg/database/repository/settings.go new file mode 100644 index 0000000..6ed1eb6 --- /dev/null +++ b/pkg/database/repository/settings.go @@ -0,0 +1,15 @@ +package repository + +import "context" + +type ( + Settings struct { + ShowMode bool + ShowOwner bool + } + + SettingsRepository interface { + Save(context.Context, *Settings) error + Load(context.Context) (*Settings, error) + } +) diff --git a/pkg/database/sql/media.go b/pkg/database/sql/media.go index 3446f79..27f8cf0 100644 --- a/pkg/database/sql/media.go +++ b/pkg/database/sql/media.go @@ -6,7 +6,7 @@ import ( "gorm.io/gorm" - "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/list" ) @@ -46,10 +46,10 @@ type ( } ) -var _ media.Repository = &MediaRepository{} +var _ repository.MediaRepository = &MediaRepository{} -func (self *Media) ToModel() *media.Media { - return &media.Media{ +func (self *Media) ToModel() *repository.Media { + return &repository.Media{ ID: self.ID, Path: self.Path, PathHash: self.PathHash, @@ -58,8 +58,8 @@ func (self *Media) ToModel() *media.Media { } } -func (m *MediaEXIF) ToModel() *media.MediaEXIF { - return &media.MediaEXIF{ +func (m *MediaEXIF) ToModel() *repository.MediaEXIF { + return &repository.MediaEXIF{ Height: m.Height, Width: m.Width, Description: m.Description, @@ -85,7 +85,7 @@ func NewMediaRepository(db *gorm.DB) *MediaRepository { } } -func (self *MediaRepository) Create(ctx context.Context, createMedia *media.CreateMedia) error { +func (self *MediaRepository) Create(ctx context.Context, createMedia *repository.CreateMedia) error { media := &Media{ Name: createMedia.Name, Path: createMedia.Path, @@ -119,7 +119,7 @@ func (self *MediaRepository) Exists(ctx context.Context, path string) (bool, err return exists, nil } -func (self *MediaRepository) List(ctx context.Context, pagination *media.Pagination) ([]*media.Media, error) { +func (self *MediaRepository) List(ctx context.Context, pagination *repository.Pagination) ([]*repository.Media, error) { medias := make([]*Media, 0) result := self.db. WithContext(ctx). @@ -133,14 +133,14 @@ func (self *MediaRepository) List(ctx context.Context, pagination *media.Paginat return nil, result.Error } - m := list.Map(medias, func(s *Media) *media.Media { + m := list.Map(medias, func(s *Media) *repository.Media { return s.ToModel() }) return m, nil } -func (self *MediaRepository) Get(ctx context.Context, pathHash string) (*media.Media, error) { +func (self *MediaRepository) Get(ctx context.Context, pathHash string) (*repository.Media, error) { m := &Media{} result := self.db. WithContext(ctx). @@ -173,7 +173,7 @@ func (self *MediaRepository) GetPath(ctx context.Context, pathHash string) (stri return path, nil } -func (m *MediaRepository) GetEXIF(ctx context.Context, mediaID uint) (*media.MediaEXIF, error) { +func (m *MediaRepository) GetEXIF(ctx context.Context, mediaID uint) (*repository.MediaEXIF, error) { exif := &MediaEXIF{} result := m.db. WithContext(ctx). @@ -189,7 +189,7 @@ func (m *MediaRepository) GetEXIF(ctx context.Context, mediaID uint) (*media.Med return exif.ToModel(), nil } -func (s *MediaRepository) CreateEXIF(ctx context.Context, id uint, info *media.MediaEXIF) error { +func (s *MediaRepository) CreateEXIF(ctx context.Context, id uint, info *repository.MediaEXIF) error { media := &MediaEXIF{ MediaID: id, Width: info.Width, @@ -220,7 +220,7 @@ func (s *MediaRepository) CreateEXIF(ctx context.Context, id uint, info *media.M return nil } -func (r *MediaRepository) GetEmptyEXIF(ctx context.Context, pagination *media.Pagination) ([]*media.Media, error) { +func (r *MediaRepository) GetEmptyEXIF(ctx context.Context, pagination *repository.Pagination) ([]*repository.Media, error) { medias := make([]*Media, 0) result := r.db. WithContext(ctx). @@ -236,7 +236,7 @@ func (r *MediaRepository) GetEmptyEXIF(ctx context.Context, pagination *media.Pa return nil, result.Error } - m := list.Map(medias, func(s *Media) *media.Media { + m := list.Map(medias, func(s *Media) *repository.Media { return s.ToModel() }) diff --git a/pkg/database/sql/settings.go b/pkg/database/sql/settings.go index 7ad718b..4e73878 100644 --- a/pkg/database/sql/settings.go +++ b/pkg/database/sql/settings.go @@ -5,7 +5,7 @@ import ( "gorm.io/gorm" - "git.sr.ht/~gabrielgio/img/pkg/components/settings" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) type ( @@ -20,7 +20,7 @@ type ( } ) -var _ settings.Repository = &SettingsRepository{} +var _ repository.SettingsRepository = &SettingsRepository{} func NewSettingsRespository(db *gorm.DB) *SettingsRepository { return &SettingsRepository{ @@ -41,7 +41,7 @@ func (self *SettingsRepository) ensureSettings(ctx context.Context) (*Settings, return s, nil } -func (self *SettingsRepository) Save(ctx context.Context, toSaveSettings *settings.Settings) error { +func (self *SettingsRepository) Save(ctx context.Context, toSaveSettings *repository.Settings) error { db := self.db.WithContext(ctx) s, err := self.ensureSettings(ctx) @@ -56,13 +56,13 @@ func (self *SettingsRepository) Save(ctx context.Context, toSaveSettings *settin return result.Error } -func (self *SettingsRepository) Load(ctx context.Context) (*settings.Settings, error) { +func (self *SettingsRepository) Load(ctx context.Context) (*repository.Settings, error) { s, err := self.ensureSettings(ctx) if err != nil { return nil, err } - return &settings.Settings{ + return &repository.Settings{ ShowMode: s.ShowMode, ShowOwner: s.ShowOwner, }, nil diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go index f0d89ad..01f6729 100644 --- a/pkg/database/sql/user_test.go +++ b/pkg/database/sql/user_test.go @@ -12,7 +12,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" - "git.sr.ht/~gabrielgio/img/pkg/components/user" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) func setup(t *testing.T) (*gorm.DB, func()) { @@ -46,9 +46,9 @@ func TestCreate(t *testing.T) { db, tearDown := setup(t) defer tearDown() - repository := NewUserRepository(db) + userRepository := NewUserRepository(db) - id, err := repository.Create(context.Background(), &user.CreateUser{ + id, err := userRepository.Create(context.Background(), &repository.CreateUser{ Username: "new_username", Name: "new_name", }) @@ -56,11 +56,11 @@ func TestCreate(t *testing.T) { t.Fatalf("Error creating: %s", err.Error()) } - got, err := repository.Get(context.Background(), id) + got, err := userRepository.Get(context.Background(), id) if err != nil { t.Fatalf("Error getting: %s", err.Error()) } - want := &user.User{ + want := &repository.User{ ID: id, Username: "new_username", Name: "new_name", @@ -76,9 +76,9 @@ func TestUpdate(t *testing.T) { db, tearDown := setup(t) defer tearDown() - repository := NewUserRepository(db) + userRepository := NewUserRepository(db) - id, err := repository.Create(context.Background(), &user.CreateUser{ + id, err := userRepository.Create(context.Background(), &repository.CreateUser{ Username: "username", Name: "name", }) @@ -86,7 +86,7 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error creating user: %s", err.Error()) } - err = repository.Update(context.Background(), id, &user.UpdateUser{ + err = userRepository.Update(context.Background(), id, &repository.UpdateUser{ Username: "new_username", Name: "new_name", }) @@ -94,11 +94,11 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error update user: %s", err.Error()) } - got, err := repository.Get(context.Background(), 1) + got, err := userRepository.Get(context.Background(), 1) if err != nil { t.Fatalf("Error getting user: %s", err.Error()) } - want := &user.User{ + want := &repository.User{ ID: id, Username: "new_username", Name: "new_name", diff --git a/pkg/fileop/exif.go b/pkg/fileop/exif.go index 79716eb..49cd0ce 100644 --- a/pkg/fileop/exif.go +++ b/pkg/fileop/exif.go @@ -6,17 +6,17 @@ import ( "github.com/barasher/go-exiftool" - "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) -func ReadExif(path string) (*media.MediaEXIF, error) { +func ReadExif(path string) (*repository.MediaEXIF, error) { et, err := exiftool.NewExiftool() if err != nil { return nil, err } defer et.Close() - newExif := &media.MediaEXIF{} + newExif := &repository.MediaEXIF{} fileInfo := et.ExtractMetadata(path)[0] // Get description @@ -159,7 +159,7 @@ func isFloatReal(v float64) bool { // sanitizeEXIF removes any EXIF float64 field that is not a real number (+Inf, // -Inf or Nan) -func sanitizeEXIF(exif *media.MediaEXIF) { +func sanitizeEXIF(exif *repository.MediaEXIF) { if exif.Exposure != nil && !isFloatReal(*exif.Exposure) { exif.Exposure = nil } diff --git a/pkg/view/filesystem.go b/pkg/view/filesystem.go index f10d788..d2ebff4 100644 --- a/pkg/view/filesystem.go +++ b/pkg/view/filesystem.go @@ -5,14 +5,14 @@ import ( "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/components/filesystem" - "git.sr.ht/~gabrielgio/img/pkg/components/settings" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" ) type ( FileSystemView struct { controller filesystem.Controller - settings settings.Repository + settings repository.SettingsRepository } FilePage struct { Page *filesystem.Page @@ -23,7 +23,7 @@ type ( func NewFileSystemView( controller filesystem.Controller, - settingsRepository settings.Repository, + settingsRepository repository.SettingsRepository, ) *FileSystemView { return &FileSystemView{ controller: controller, diff --git a/pkg/view/media.go b/pkg/view/media.go index 66e3020..ce9e272 100644 --- a/pkg/view/media.go +++ b/pkg/view/media.go @@ -6,22 +6,22 @@ import ( "github.com/valyala/fasthttp" "git.sr.ht/~gabrielgio/img" - "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" ) type ( MediaView struct { - mediaRepository media.Repository + mediaRepository repository.MediaRepository } Page struct { - Medias []*media.Media - Next *media.Pagination + Medias []*repository.Media + Next *repository.Pagination } ) -func getPagination(ctx *fasthttp.RequestCtx) *media.Pagination { +func getPagination(ctx *fasthttp.RequestCtx) *repository.Pagination { var ( size int page int @@ -45,13 +45,13 @@ func getPagination(ctx *fasthttp.RequestCtx) *media.Pagination { page = p } - return &media.Pagination{ + return &repository.Pagination{ Page: page, Size: size, } } -func NewMediaView(mediaRepository media.Repository) *MediaView { +func NewMediaView(mediaRepository repository.MediaRepository) *MediaView { return &MediaView{ mediaRepository: mediaRepository, } @@ -68,7 +68,7 @@ func (self *MediaView) Index(ctx *fasthttp.RequestCtx) error { Title: "Media", Data: &Page{ Medias: medias, - Next: &media.Pagination{ + Next: &repository.Pagination{ Size: p.Size, Page: p.Page + 1, }, diff --git a/pkg/view/settings.go b/pkg/view/settings.go index 954cc98..14f4e49 100644 --- a/pkg/view/settings.go +++ b/pkg/view/settings.go @@ -4,7 +4,6 @@ import ( "github.com/valyala/fasthttp" "git.sr.ht/~gabrielgio/img" - "git.sr.ht/~gabrielgio/img/pkg/components/settings" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/ext" ) @@ -12,18 +11,18 @@ import ( type ( SettingsView struct { // there is not need to create a controller for this - settingsRepository settings.Repository + settingsRepository repository.SettingsRepository userRepository repository.UserRepository } SettingsPage struct { - Settings *settings.Settings + Settings *repository.Settings Users []*repository.User } ) func NewSettingsView( - settingsRespository settings.Repository, + settingsRespository repository.SettingsRepository, userRepository repository.UserRepository, ) *SettingsView { return &SettingsView{ @@ -58,7 +57,7 @@ func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { showOwner = string(ctx.FormValue("showOwner")) == "on" ) - err := self.settingsRepository.Save(ctx, &settings.Settings{ + err := self.settingsRepository.Save(ctx, &repository.Settings{ ShowMode: showMode, ShowOwner: showOwner, }) diff --git a/pkg/worker/exif_scanner.go b/pkg/worker/exif_scanner.go index 91eed12..97790a0 100644 --- a/pkg/worker/exif_scanner.go +++ b/pkg/worker/exif_scanner.go @@ -3,27 +3,27 @@ package worker import ( "context" - "git.sr.ht/~gabrielgio/img/pkg/components/media" "git.sr.ht/~gabrielgio/img/pkg/coroutine" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/fileop" ) type ( EXIFScanner struct { - repository media.Repository + repository repository.MediaRepository } ) -var _ BatchProcessor[*media.Media] = &EXIFScanner{} +var _ BatchProcessor[*repository.Media] = &EXIFScanner{} -func NewEXIFScanner(repository media.Repository) *EXIFScanner { +func NewEXIFScanner(repository repository.MediaRepository) *EXIFScanner { return &EXIFScanner{ repository: repository, } } -func (e *EXIFScanner) Query(ctx context.Context) ([]*media.Media, error) { - medias, err := e.repository.GetEmptyEXIF(ctx, &media.Pagination{ +func (e *EXIFScanner) Query(ctx context.Context) ([]*repository.Media, error) { + medias, err := e.repository.GetEmptyEXIF(ctx, &repository.Pagination{ Page: 0, Size: 100, }) @@ -34,8 +34,8 @@ func (e *EXIFScanner) Query(ctx context.Context) ([]*media.Media, error) { return medias, nil } -func (e *EXIFScanner) Process(ctx context.Context, m *media.Media) error { - exif, err := coroutine.WrapProcess(ctx, func() (*media.MediaEXIF, error) { return fileop.ReadExif(m.Path) }) +func (e *EXIFScanner) Process(ctx context.Context, m *repository.Media) error { + exif, err := coroutine.WrapProcess(ctx, func() (*repository.MediaEXIF, error) { return fileop.ReadExif(m.Path) }) if err != nil { return err } diff --git a/pkg/worker/file_scanner.go b/pkg/worker/file_scanner.go index a51f60b..aa79035 100644 --- a/pkg/worker/file_scanner.go +++ b/pkg/worker/file_scanner.go @@ -9,19 +9,19 @@ import ( "path/filepath" "strings" - "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) type ( FileScanner struct { root string - repository media.Repository + repository repository.MediaRepository } ) var _ ChanProcessor[string] = &FileScanner{} -func NewFileScanner(root string, repository media.Repository) *FileScanner { +func NewFileScanner(root string, repository repository.MediaRepository) *FileScanner { return &FileScanner{ root: root, repository: repository, @@ -77,7 +77,7 @@ func (f *FileScanner) Process(ctx context.Context, path string) error { return nil } - return f.repository.Create(ctx, &media.CreateMedia{ + return f.repository.Create(ctx, &repository.CreateMedia{ Name: name, Path: path, PathHash: str, -- cgit v1.2.3