From e5265e35e4f400d538775d331fe78c132f3fc020 Mon Sep 17 00:00:00 2001
From: grngxd <36968271+grngxd@users.noreply.github.com>
Date: Sun, 8 Jun 2025 00:00:56 +0100
Subject: [PATCH] sharing base64 file data

---
 internal/api/routes/files.go | 17 +++++++++++++++++
 internal/types/types.go      |  1 +
 2 files changed, 18 insertions(+)

diff --git a/internal/api/routes/files.go b/internal/api/routes/files.go
index 0471632..f90fbd6 100644
--- a/internal/api/routes/files.go
+++ b/internal/api/routes/files.go
@@ -1,6 +1,7 @@
 package routes
 
 import (
+	"encoding/base64"
 	"os"
 	"path/filepath"
 	"time"
@@ -40,11 +41,17 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
 			return
 		}
 
+		// conver file variable to base64 string
+		b64, err := convertToBase64(filePath)
+		if err != nil {
+		}
+
 		fileMeta := types.File{
 			ID:        uid + "_" + file.Filename,
 			Path:      filePath,
 			Owner:     uid,
 			CreatedAt: time.Now(),
+			Base64:    b64,
 		}
 
 		if err := cfg.Database.Create(&fileMeta).Error; err != nil {
@@ -74,3 +81,13 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
 		c.JSON(200, files)
 	})
 }
+
+func convertToBase64(filePath string) (string, error) {
+	file, err := os.ReadFile(filePath)
+	if err != nil {
+		return "", err
+	}
+
+	b64 := base64.StdEncoding.EncodeToString(file)
+	return b64, nil
+}
diff --git a/internal/types/types.go b/internal/types/types.go
index 98dd231..6120f08 100644
--- a/internal/types/types.go
+++ b/internal/types/types.go
@@ -27,4 +27,5 @@ type File struct {
 	Path      string    `gorm:"not null;index"`
 	Owner     string    `gorm:"not null;index"`
 	CreatedAt time.Time `gorm:"autoCreateTime"`
+	Base64    string    `gorm:"type:text"`
 }