fix alot
This commit is contained in:
78
src/main.go
78
src/main.go
@@ -1,10 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -19,24 +22,24 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal("DB Connection failed:", err)
|
||||
}
|
||||
db.AutoMigrate(&FileRecord{})
|
||||
db.AutoMigrate(&User{}, &FileRecord{})
|
||||
ensureAdmin()
|
||||
|
||||
go cleanupWorker()
|
||||
|
||||
router := gin.Default()
|
||||
router.MaxMultipartMemory = 100 << 20 // 100 MiB limit
|
||||
router.MaxMultipartMemory = 10 << 30
|
||||
router.LoadHTMLGlob("templates/*")
|
||||
|
||||
// Public Routes
|
||||
router.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", nil) })
|
||||
router.POST("/api/upload", uploadHandler)
|
||||
router.GET("/f/:id", downloadHandler)
|
||||
router.DELETE("/api/file/:del_id", deleteHandler)
|
||||
router.GET("/api/file/delete/:del_id", deleteHandler)
|
||||
|
||||
// Protected Admin Routes
|
||||
admin := router.Group("/admin", gin.BasicAuth(gin.Accounts{
|
||||
"admin": "password123", // CHANGE THIS
|
||||
}))
|
||||
router.POST("/api/upload", uploadHandler)
|
||||
|
||||
admin := router.Group("/admin")
|
||||
admin.Use(authMiddleware())
|
||||
|
||||
admin.GET("/", func(c *gin.Context) {
|
||||
var files []FileRecord
|
||||
@@ -52,6 +55,65 @@ func main() {
|
||||
c.Redirect(303, "/admin")
|
||||
})
|
||||
|
||||
admin.GET("/download/:id", func(c *gin.Context) {
|
||||
var record FileRecord
|
||||
fmt.Printf("Getting file id: %v\n", c.Param("id"))
|
||||
if err := db.First(&record, "id = ?", c.Param("id")).Error; err != nil {
|
||||
fmt.Println("Admin download failed:", err)
|
||||
c.Header("Content-Type", "text/plain")
|
||||
c.String(418, "File not found")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Path: %s, Filename: %s\n", record.Path, record.Filename)
|
||||
|
||||
absPath, err := filepath.Abs(record.Path)
|
||||
if err != nil {
|
||||
c.String(500, "Path error")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Serving:", absPath)
|
||||
c.Header("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, record.Filename))
|
||||
c.File(record.Path)
|
||||
})
|
||||
|
||||
admin.GET("/files", func(c *gin.Context) {
|
||||
var files []FileRecord
|
||||
db.Order("created_at desc").Find(&files)
|
||||
c.JSON(200, files)
|
||||
})
|
||||
|
||||
router.POST("/login", loginHandler)
|
||||
|
||||
router.GET("/login", func(c *gin.Context) {
|
||||
c.HTML(200, "login.html", nil)
|
||||
})
|
||||
|
||||
router.GET("/logout", func(c *gin.Context) {
|
||||
c.SetCookie("auth", "", -1, "/", "", false, true)
|
||||
c.Redirect(302, "/")
|
||||
})
|
||||
|
||||
log.Println("Server starting at http://localhost:8080")
|
||||
router.Run(":8080")
|
||||
}
|
||||
|
||||
func ensureAdmin() {
|
||||
var count int64
|
||||
db.Model(&User{}).Where("username = ?", "admin").Count(&count)
|
||||
|
||||
if count == 0 {
|
||||
hash, _ := bcrypt.GenerateFromPassword(
|
||||
[]byte("change_this_password"),
|
||||
bcrypt.DefaultCost,
|
||||
)
|
||||
|
||||
db.Create(&User{
|
||||
Username: "admin",
|
||||
Password: string(hash),
|
||||
})
|
||||
|
||||
log.Println("Admin user created")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user