package handler import ( "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "gitea.kmux.cn/zhilv/scriptforge/internal/service" ) func GetRawScript(svc *service.ScriptService) gin.HandlerFunc { return func(c *gin.Context) { id := c.Param("id") script, err := svc.GetByID(id) if errors.Is(err, service.ErrNotFound) { c.String(http.StatusNotFound, "script not found or expired\n") return } if err != nil { c.String(http.StatusInternalServerError, "internal error\n") return } // Use default variant for MIME/extension variant, verr := svc.GetDefaultVariant(script.CategoryID) mime := "text/plain" ext := ".sh" if verr == nil { mime = variant.MIMEType ext = variant.Extension } c.Header("Content-Type", mime+"; charset=utf-8") c.Header("Content-Disposition", fmt.Sprintf(`attachment; filename="script%s"`, ext)) c.Header("X-Content-Type-Options", "nosniff") c.String(http.StatusOK, script.Content) } }