sharing base64 file data
This commit is contained in:
parent
c4f7f79cfd
commit
f947cba89d
2 changed files with 18 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue