html-bypass/download.go

61 lines
1.8 KiB
Go

package main
import (
"embed"
"encoding/base64"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strings"
)
//go:embed files/*
var Files embed.FS
func DownloadFile(c *gin.Context) {
fileFormat := c.DefaultQuery("type", "control")
fileType := c.Param("fileType")
var err error
var fileContent []byte
var sourceFile string
switch fileType {
case "word":
sourceFile = "files/Invoice.docm"
case "jspdf":
sourceFile = "files/Invoice.pdf.js"
case "sh":
sourceFile = "files/example.sh"
default:
c.String(http.StatusNotFound, "Could not find file Type %s", fileType)
log.Printf("Could not find file type %s", fileType)
return
}
fileContent, err = Files.ReadFile(sourceFile)
if err != nil {
c.String(http.StatusNotFound, "Could not open file type %s: %v", fileType, err)
log.Printf("Could not find file type %s: %v", fileType, err)
return
}
switch fileFormat {
case "base64":
base64String := base64.StdEncoding.EncodeToString(fileContent)
c.String(http.StatusOK, "data:application/octet-stream;base64,%s", base64String)
case "control":
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strings.Split(sourceFile, "/")[1]))
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", fileContent)
case "jpg":
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "image.jpg"))
c.Data(http.StatusOK, "image/jpeg", fileContent)
// TODO: Iso image
// case "iso":
// diskImg := fmt.Sprintf("%s.iso", strings.Split(sourceFile, "/")[1])
// diskSize := (len(fileContent)/1024 + 1) * 1024 // Slightly padding out disk Image
default:
c.String(http.StatusNotFound, "Could not find Encoding %s", fileFormat)
log.Printf("Could not find file type %s", fileFormat)
return
}
}