add realistion for sysStorage instead of seaweedfs
This commit is contained in:
parent
bf723e96a8
commit
14b361e5fd
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"donat-widget/infrastructure/sysStorage"
|
||||
"donat-widget/internal/app/http"
|
||||
"donat-widget/internal/config"
|
||||
"donat-widget/internal/docs"
|
||||
@ -39,8 +40,8 @@ func main() {
|
||||
|
||||
// INFRASTRUCTURE
|
||||
db := pg.NewPgPool(context.Background(), cfg.Db.Username, cfg.Db.Password, cfg.Db.Host, cfg.Db.Port, cfg.Db.DBName)
|
||||
storage := weed.NewWeed(cfg.Storage.Filer, cfg.Storage.Master)
|
||||
|
||||
//storage := weed.NewWeed(cfg.Storage.Filer, cfg.Storage.Master)
|
||||
storage := sysStorage.NewLocalStorage("")
|
||||
// CLIENTS
|
||||
paymentClient := PaymentClient.New(cfg.PaymentService.Host, cfg.PaymentService.Port)
|
||||
authClient := AuthClient.New(cfg.AuthService.Host, cfg.AuthService.Port)
|
||||
|
2
go.mod
2
go.mod
@ -25,6 +25,7 @@ require (
|
||||
github.com/go-openapi/swag v0.23.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
@ -43,6 +44,7 @@ require (
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
golang.org/x/tools v0.24.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -30,6 +30,8 @@ github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4
|
||||
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
|
||||
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
@ -102,6 +104,8 @@ golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
95
infrastructure/sysStorage/sysStorage.go
Normal file
95
infrastructure/sysStorage/sysStorage.go
Normal file
@ -0,0 +1,95 @@
|
||||
package sysStorage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"donat-widget/internal/model"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type LocalStorage struct {
|
||||
basePath string
|
||||
}
|
||||
|
||||
func NewLocalStorage(basePath string) *LocalStorage {
|
||||
if basePath == "" {
|
||||
var err error
|
||||
basePath, err = os.Getwd()
|
||||
if err != nil {
|
||||
panic("Failed to get current directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
|
||||
panic("Failed to create storage directory: " + err.Error())
|
||||
}
|
||||
return &LocalStorage{basePath: basePath}
|
||||
}
|
||||
|
||||
func (ls *LocalStorage) Upload(
|
||||
file model.UploadFile,
|
||||
filename string,
|
||||
size int64,
|
||||
collection string,
|
||||
) (model.FileID, error) {
|
||||
collectionPath := filepath.Join(ls.basePath, collection)
|
||||
if err := os.MkdirAll(collectionPath, os.ModePerm); err != nil {
|
||||
return "error", err
|
||||
}
|
||||
|
||||
filePath := filepath.Join(collectionPath, filename)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return "error creating", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, *file)
|
||||
if err != nil {
|
||||
return "error copy", err
|
||||
}
|
||||
|
||||
return model.FileID(filename), nil
|
||||
}
|
||||
|
||||
func (ls *LocalStorage) Download(
|
||||
FileID model.FileID,
|
||||
) (model.DownloadFile, error) {
|
||||
filePath := string(FileID)
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var fileBuffer bytes.Buffer
|
||||
_, err = io.Copy(&fileBuffer, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fileBuffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func (ls *LocalStorage) Update(
|
||||
file model.UploadFile,
|
||||
fileID model.FileID,
|
||||
filename string,
|
||||
size int64,
|
||||
collection string,
|
||||
) error {
|
||||
filePath := string(fileID)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = io.Copy(f, *file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -37,8 +37,8 @@ func (weed *Weed) Upload(
|
||||
filename string,
|
||||
size int64,
|
||||
collection string,
|
||||
) (model.FileData, error) {
|
||||
var fileData model.FileData
|
||||
) (model.FileID, error) {
|
||||
var fileData model.WeedData
|
||||
fileData, err := weed.operation.Upload(
|
||||
*file,
|
||||
filename,
|
||||
@ -47,10 +47,10 @@ func (weed *Weed) Upload(
|
||||
"",
|
||||
)
|
||||
if err != nil {
|
||||
return fileData, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fileData, nil
|
||||
return model.FileID(fileData.FileID), nil
|
||||
}
|
||||
|
||||
func (weed *Weed) Download(
|
||||
|
@ -179,8 +179,26 @@ func UpdateDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
||||
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Unprocessable Entity")
|
||||
}
|
||||
|
||||
backgroundFile, backgroundOk := form.File["background"]
|
||||
avatarFile, avatarOk := form.File["avatar"]
|
||||
if form != nil {
|
||||
if backgroundFiles, ok := form.File["background"]; ok && len(backgroundFiles) > 0 {
|
||||
backgroundFile := backgroundFiles[0]
|
||||
err := saveUploadedFile(backgroundFile.Open(), "uploads/backgrounds/")
|
||||
if err != nil {
|
||||
slog.Error("Failed to save background:", err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to save background")
|
||||
}
|
||||
}
|
||||
|
||||
// Проверяем и сохраняем avatar, если он передан
|
||||
if avatarFiles, ok := form.File["avatar"]; ok && len(avatarFiles) > 0 {
|
||||
avatarFile := avatarFiles[0]
|
||||
err := saveUploadedFile(avatarFile, "uploads/avatars/")
|
||||
if err != nil {
|
||||
slog.Error("Failed to save avatar:", err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to save avatar")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +81,7 @@ type Error interface {
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
Upload(file UploadFile, filename string, size int64, collection string) (FileData, error)
|
||||
Upload(file UploadFile, filename string, size int64, collection string) (FileID, error)
|
||||
Download(FileID FileID) (DownloadFile, error)
|
||||
Update(file UploadFile, fileID FileID, filename string, size int64, collection string) error
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ type Display bool
|
||||
type Widgets []*Widget
|
||||
type WidgetHTML string
|
||||
|
||||
type FileData *goseaweedfs.FilePart
|
||||
type WeedData *goseaweedfs.FilePart
|
||||
type UploadFile *multipart.File
|
||||
type DownloadFile []byte
|
||||
type FileID string
|
||||
|
Loading…
x
Reference in New Issue
Block a user