Replaced the gin framwwork with echo

This commit is contained in:
2025-01-13 11:57:48 +01:00
parent 8e2d22b899
commit 034ba35fbb
32 changed files with 348 additions and 266 deletions

View File

@@ -3,7 +3,6 @@ package helpers
import (
"embed"
"fmt"
"io"
"io/fs"
"net/http"
"os"
@@ -12,6 +11,7 @@ import (
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/labstack/echo"
)
func SetCorsAndNoCacheHeaders() gin.HandlerFunc {
@@ -61,15 +61,15 @@ func EmbedFolder(fsEmbed embed.FS, targetPath string, index bool) static.ServeFi
}
}
func SendSong(ctx *gin.Context, Filename string) {
func SendSong(ctx echo.Context, Filename string) error {
fmt.Println("Client requests: " + Filename)
//Check if file exists and open
openFile, err := os.Open(Filename)
if err != nil {
//File not found, send 404
http.Error(ctx.Writer, "Song not found.", 404)
return
//http.Error(ctx.Writer, "Song not found.", 404)
return ctx.String(http.StatusNotFound, "Song not found.")
}
defer func(openFile *os.File) {
_ = openFile.Close()
@@ -91,13 +91,13 @@ func SendSong(ctx *gin.Context, Filename string) {
//Send the headers
//writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
ctx.Writer.Header().Set("Content-Type", "audio/mpeg")
ctx.Writer.Header().Set("Content-Length", FileSize)
ctx.Writer.Header().Set("Expires", "Tue, 03 Jul 2001 06:00:00 GMT")
ctx.Writer.Header().Set("Last-Modified", time.Now().String()+" GMT")
ctx.Writer.Header().Set("Cache-Control", "no-cache, no-store, private, max-age=0")
ctx.Writer.Header().Set("Pragma", "no-cache")
ctx.Writer.Header().Set("X-Accel-Expires", "0")
ctx.Response().Header().Set("Content-Type", "audio/mpeg")
ctx.Response().Header().Set("Content-Length", FileSize)
ctx.Response().Header().Set("Expires", "Tue, 03 Jul 2001 06:00:00 GMT")
ctx.Response().Header().Set("Last-Modified", time.Now().String()+" GMT")
ctx.Response().Header().Set("Cache-Control", "no-cache, no-store, private, max-age=0")
ctx.Response().Header().Set("Pragma", "no-cache")
ctx.Response().Header().Set("X-Accel-Expires", "0")
var etagHeaders = []string{
"ETag",
@@ -109,14 +109,14 @@ func SendSong(ctx *gin.Context, Filename string) {
}
for _, v := range etagHeaders {
if ctx.Request.Header.Get(v) != "" {
ctx.Request.Header.Del(v)
if ctx.Request().Header.Get(v) != "" {
ctx.Request().Header.Del(v)
}
}
//Send the file
//We read 512 bytes from the file already, so we reset the offset back to 0
_, _ = openFile.Seek(0, 0)
_, _ = io.Copy(ctx.Writer, openFile) //'Copy' the file to the client
return
//_, _ = io.Copy(ctx.Writer, openFile) //'Copy' the file to the client
return ctx.Stream(http.StatusOK, "audio/mpeg", openFile)
}