Separate database initialization into NewMYSQLdb function

This commit is contained in:
2024-04-28 20:49:41 +07:00
parent 1a7ac48330
commit b4f303463d
10 changed files with 242 additions and 92 deletions

View File

@ -1,29 +1,33 @@
package downloadFileHandler
import (
"github.com/fossyy/filekeeper/utils"
"net/http"
"os"
"path/filepath"
"github.com/fossyy/filekeeper/db"
"github.com/fossyy/filekeeper/logger"
"github.com/fossyy/filekeeper/types/models"
)
var log *logger.AggregatedLogger
// TESTTING VAR
var database db.Database
func init() {
log = logger.Logger()
database = db.NewMYSQLdb(utils.Getenv("DB_USERNAME"), utils.Getenv("DB_PASSWORD"), utils.Getenv("DB_HOST"), utils.Getenv("DB_PORT"), utils.Getenv("DB_NAME"))
}
func GET(w http.ResponseWriter, r *http.Request) {
fileID := r.PathValue("id")
var file models.File
err := db.DB.Table("files").Where("id = ?", fileID).First(&file).Error
file, err := database.GetFile(fileID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error(err.Error())
return
}
uploadDir := "uploads"
@ -42,6 +46,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error(err.Error())
return
}
defer openFile.Close()
@ -49,6 +54,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error(err.Error())
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+stat.Name())