add fix for create url in widgets

This commit is contained in:
harold 2025-03-06 19:06:28 +05:00
parent 06b9624c12
commit 3759690470
13 changed files with 33 additions and 15 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
internal/config/config.yaml
docker
docker
storage

View File

@ -43,7 +43,7 @@ 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 := sysStorage.NewLocalStorage("")
storage := sysStorage.NewLocalStorage(cfg.Storage.Location, cfg.HOST)
// CLIENTS
paymentClient := PaymentClient.New(cfg.PaymentService.Host, cfg.PaymentService.Port)
authClient := AuthClient.New(cfg.AuthService.Host, cfg.AuthService.Port)
@ -55,7 +55,7 @@ func main() {
fileRepo := FileRepo.New(db, storage)
// SERVICES
widgetService := WidgetService.New(widgetRepo, authClient, fileRepo)
widgetService := WidgetService.New(widgetRepo, authClient, fileRepo, storage)
donatService := DonatService.New(
donatRepo,
widgetRepo,
@ -64,7 +64,7 @@ func main() {
storage,
)
targetService := TargetService.New(targetRepo, authClient)
fileService := FileService.New(authClient, fileRepo)
fileService := FileService.New(authClient, fileRepo, storage)
http.NewApp(
db,

View File

@ -26,6 +26,7 @@ services:
volumes:
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./docker/postgres/data:/var/lib/postgresql/data
- ./storage:/storage
networks:
donat-network:
external: true

View File

@ -3,6 +3,7 @@ package sysStorage
import (
"bytes"
"fmt"
"github.com/google/uuid"
"io"
"mime/multipart"
"os"
@ -13,9 +14,10 @@ import (
type LocalStorage struct {
basePath string
FileUrl string
}
func NewLocalStorage(basePath string) *LocalStorage {
func NewLocalStorage(basePath, fileUrl string) *LocalStorage {
if basePath == "" {
var err error
basePath, err = os.Getwd()
@ -27,7 +29,8 @@ func NewLocalStorage(basePath string) *LocalStorage {
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
panic("Failed to create storage directory: " + err.Error())
}
return &LocalStorage{basePath: basePath}
fileUrl = "http://" + fileUrl
return &LocalStorage{basePath: basePath, FileUrl: fileUrl}
}
func (ls *LocalStorage) Upload(
@ -101,6 +104,12 @@ func (ls *LocalStorage) Delete(
return nil
}
func (ls *LocalStorage) DownloadLink(
fileId uuid.UUID,
) string {
return ls.FileUrl + "/api/widget/files/" + fileId.String()
}
func (ls *LocalStorage) ensureUniqueFileName(
dirPath, fileName string,
) string {

View File

@ -61,7 +61,7 @@ func AddNewFile(fileService model.FileService) echo.HandlerFunc {
// @Tags Files
// @Accept json
// @Produce octet-stream
// @Param file_id path int true "File ID"
// @Param file_id path string true "File ID" format:"uuid"
// @Success 200 {file} binary "File content"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"

View File

@ -22,8 +22,7 @@ type Database struct {
}
type Storage struct {
Filer string `yaml:"filer"`
Master string `yaml:"master"`
Location string `yaml:"location"`
}
type PaymentService struct {

View File

@ -208,7 +208,7 @@ const docTemplate = `{
"summary": "Get a file",
"parameters": [
{
"type": "integer",
"type": "string",
"description": "File ID",
"name": "file_id",
"in": "path",

View File

@ -201,7 +201,7 @@
"summary": "Get a file",
"parameters": [
{
"type": "integer",
"type": "string",
"description": "File ID",
"name": "file_id",
"in": "path",

View File

@ -344,7 +344,7 @@ paths:
in: path
name: file_id
required: true
type: integer
type: string
produces:
- application/octet-stream
responses:

View File

@ -135,6 +135,7 @@ type Storage interface {
Upload(file *multipart.FileHeader, streamerID int) (string, error)
Download(filename, folder string) ([]byte, error)
Delete(filename, extension, filePath string) error
DownloadLink(fileId uuid.UUID) string
}
type PaymentClient interface {

View File

@ -18,16 +18,19 @@ import (
func New(
authClient model.AuthClient,
fileRepo model.FileRepo,
storage model.Storage,
) *ServiceFile {
return &ServiceFile{
authClient: authClient,
fileRepo: fileRepo,
storage: storage,
}
}
type ServiceFile struct {
authClient model.AuthClient
fileRepo model.FileRepo
storage model.Storage
}
func (fileService *ServiceFile) AddNewFile(
@ -90,7 +93,7 @@ func (fileService *ServiceFile) WidgetsFiles(
}
for _, file := range files {
file.FileLink = "http://localhost/file/" + file.ID.String()
file.FileLink = fileService.storage.DownloadLink(file.ID)
}
return files, nil

View File

@ -14,11 +14,13 @@ func New(
widgetRepo model.WidgetRepo,
authClient model.AuthClient,
fileRepo model.FileRepo,
storage model.Storage,
) *ServiceWidget {
return &ServiceWidget{
widgetRepo: widgetRepo,
authClient: authClient,
fileRepo: fileRepo,
storage: storage,
}
}
@ -26,6 +28,7 @@ type ServiceWidget struct {
widgetRepo model.WidgetRepo
authClient model.AuthClient
fileRepo model.FileRepo
storage model.Storage
}
func (widgetService *ServiceWidget) CreateWidget(
@ -80,8 +83,9 @@ func (widgetService *ServiceWidget) GetWidgetsByStreamer(
groupedWidgets := make(map[int][]*model.GetWidgetDb)
for _, widget := range widgets {
widget.AudioLink = fmt.Sprintf("http://localhost/%d", widget.AudioFileId)
widget.ImageLink = fmt.Sprintf("http://localhost/%d", widget.ImageLink)
widget.AudioLink = widgetService.storage.DownloadLink(widget.AudioFileId)
widget.ImageLink = widgetService.storage.DownloadLink(widget.ImageFileId)
println(widget.AudioLink)
groupedWidgets[widget.GroupID] = append(groupedWidgets[widget.GroupID], widget)
}

0
storage/.gitkeep Normal file
View File