Add Redis support for session and user management

This commit is contained in:
2024-09-08 00:03:43 +07:00
parent b9ac29d301
commit 2c5de2b336
26 changed files with 642 additions and 548 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"github.com/fossyy/filekeeper/app"
"github.com/fossyy/filekeeper/cache"
"io"
"net/http"
"os"
@ -31,7 +30,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
return
}
fileData, err := cache.GetUserFile(fileInfo.Name, userSession.UserID.String())
fileData, err := app.Server.Service.GetUserFile(fileInfo.Name, userSession.UserID.String())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
upload, err := handleNewUpload(userSession, fileInfo)

View File

@ -1,15 +1,8 @@
package uploadHandler
import (
"github.com/fossyy/filekeeper/app"
"github.com/fossyy/filekeeper/cache"
"github.com/fossyy/filekeeper/types"
filesView "github.com/fossyy/filekeeper/view/client/upload"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
)
func GET(w http.ResponseWriter, r *http.Request) {
@ -21,73 +14,74 @@ func GET(w http.ResponseWriter, r *http.Request) {
}
func POST(w http.ResponseWriter, r *http.Request) {
fileID := r.PathValue("id")
if err := r.ParseMultipartForm(32 << 20); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
userSession := r.Context().Value("user").(types.User)
uploadDir := "uploads"
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
app.Server.Logger.Error("error getting upload info: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
}
file, err := cache.GetFile(fileID)
if err != nil {
app.Server.Logger.Error("error getting upload info: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
currentDir, _ := os.Getwd()
basePath := filepath.Join(currentDir, uploadDir)
saveFolder := filepath.Join(basePath, userSession.UserID.String(), file.ID.String())
if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) {
app.Server.Logger.Error("invalid path")
w.WriteHeader(http.StatusInternalServerError)
return
}
fileByte, fileHeader, err := r.FormFile("chunk")
if err != nil {
app.Server.Logger.Error("error getting upload info: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
defer fileByte.Close()
rawIndex := r.FormValue("index")
index, err := strconv.Atoi(rawIndex)
if err != nil {
return
}
file.UpdateProgress(int64(index), file.UploadedByte+int64(fileHeader.Size))
dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
app.Server.Logger.Error("error making upload folder: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
defer dst.Close()
if _, err := io.Copy(dst, fileByte); err != nil {
app.Server.Logger.Error("error copying byte to file dst: " + err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
if file.UploadedByte >= file.Size {
file.FinalizeFileUpload()
return
}
return
//fileID := r.PathValue("id")
//if err := r.ParseMultipartForm(32 << 20); err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//
//userSession := r.Context().Value("user").(types.User)
//
//uploadDir := "uploads"
//if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
// if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
// app.Server.Logger.Error("error getting upload info: " + err.Error())
// w.WriteHeader(http.StatusInternalServerError)
// return
// }
//}
//
//file, err := app.Server.Service.GetFile(fileID)
//if err != nil {
// app.Server.Logger.Error("error getting upload info: " + err.Error())
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//
//currentDir, _ := os.Getwd()
//basePath := filepath.Join(currentDir, uploadDir)
//saveFolder := filepath.Join(basePath, userSession.UserID.String(), file.ID.String())
//
//if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) {
// app.Server.Logger.Error("invalid path")
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//
//fileByte, fileHeader, err := r.FormFile("chunk")
//if err != nil {
// app.Server.Logger.Error("error getting upload info: " + err.Error())
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//defer fileByte.Close()
//
//rawIndex := r.FormValue("index")
//index, err := strconv.Atoi(rawIndex)
//if err != nil {
// return
//}
//
//file.UpdateProgress(int64(index), file.UploadedByte+int64(fileHeader.Size))
//
//dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
//if err != nil {
// app.Server.Logger.Error("error making upload folder: " + err.Error())
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//
//defer dst.Close()
//if _, err := io.Copy(dst, fileByte); err != nil {
// app.Server.Logger.Error("error copying byte to file dst: " + err.Error())
// w.WriteHeader(http.StatusInternalServerError)
// return
//}
//
//if file.UploadedByte >= file.Size {
// file.FinalizeFileUpload()
// return
//}
//return
}