This commit is contained in:
m
2026-07-11 22:47:33 +02:00
parent 8e728c9042
commit 1cc8b94b60
3 changed files with 66 additions and 17 deletions
+19 -1
View File
@@ -4,9 +4,12 @@ import (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"github.com/gin-gonic/gin"
"github.com/vaultdrop/backend/internal/service"
)
func ListFiles(c *gin.Context) {
@@ -15,7 +18,6 @@ func ListFiles(c *gin.Context) {
if err != nil {
fmt.Println("What")
c.JSON(http.StatusInternalServerError, gin.H{
"data": []interface{}{},
"meta": gin.H{
@@ -28,6 +30,7 @@ func ListFiles(c *gin.Context) {
}
type Finfo struct {
Url string `json:"url"`
Name string `json:"name"`
Size int64 `json:"size"`
Tags []string `json:"tags"`
@@ -48,6 +51,7 @@ func ListFiles(c *gin.Context) {
}
ps := Finfo{
Url: service.GenerateFileDownloadUrl(i.Name()),
Name: i.Name(),
Size: i.Size(),
Tags: []string{},
@@ -67,12 +71,26 @@ func ListFiles(c *gin.Context) {
}
func GetFile(c *gin.Context) {
exp, _ := strconv.ParseInt(c.Query("expires"), 10, 64)
r := service.Validate(c.Params.ByName("id"), c.Query("sig"), exp)
if r != true {
c.JSON(http.StatusNotFound, gin.H{
"error": gin.H{
"code": "FILE_NOT_FOUND",
"message": "File not found",
},
})
return
}
c.File(path.Join("./uploads/", c.Params.ByName("id")))
}
func UploadFile(c *gin.Context) {
+41
View File
@@ -0,0 +1,41 @@
package service
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
)
const secret = "thisismyrandomstring"
func sign(fileID string, expires int64, secret string) string {
data := fmt.Sprintf("%s:%d", fileID, expires)
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(data))
return hex.EncodeToString(mac.Sum(nil))
}
func GenerateFileDownloadUrl(fileID string) string {
expires := time.Now().Add(10 * time.Minute).Unix()
sig := sign(fileID, expires, secret)
url := fmt.Sprintf(
"http://192.168.1.142:8080/api/v1/files/%s?expires=%d&sig=%s",
fileID,
expires,
sig,
)
return url
}
func Validate(fileID, sig string, expires int64) bool {
if time.Now().Unix() > expires {
return false
}
expected := sign(fileID, expires, secret)
return hmac.Equal([]byte(sig), []byte(expected))
}
-10
View File
@@ -1,10 +0,0 @@
package service
import (
"fmt"
"mime/multipart"
)
func UploadFile(f *multipart.FileHeader) {
fmt.Println(f.Filename)
}