Implement file caching for faster retrieval and reduced database load
This commit is contained in:
@ -1,22 +1,18 @@
|
||||
package uploadHandler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/fossyy/filekeeper/db"
|
||||
"github.com/fossyy/filekeeper/cache"
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
filesView "github.com/fossyy/filekeeper/view/upload"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/fossyy/filekeeper/logger"
|
||||
filesView "github.com/fossyy/filekeeper/view/upload"
|
||||
)
|
||||
|
||||
var log *logger.AggregatedLogger
|
||||
var mu sync.Mutex
|
||||
|
||||
func init() {
|
||||
log = logger.Logger()
|
||||
@ -25,7 +21,7 @@ func init() {
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
component := filesView.Main("upload page")
|
||||
if err := component.Render(r.Context(), w); err != nil {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -33,32 +29,31 @@ 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 {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
|
||||
if r.FormValue("done") == "true" {
|
||||
db.DB.FinalizeFileUpload(fileID)
|
||||
return
|
||||
}
|
||||
|
||||
uploadDir := "uploads"
|
||||
if err := createUploadDirectory(uploadDir); err != nil {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
return
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
|
||||
log.Error("error getting upload info: " + err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
file, err := db.DB.GetUploadInfo(fileID)
|
||||
file, err := cache.GetFile(fileID)
|
||||
if err != nil {
|
||||
log.Error("error getting upload info: " + err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
currentDir, _ := os.Getwd()
|
||||
basePath := filepath.Join(currentDir, uploadDir)
|
||||
saveFolder := filepath.Join(basePath, userSession.UserID.String(), file.FileID.String())
|
||||
saveFolder := filepath.Join(basePath, userSession.UserID.String(), file.ID.String())
|
||||
|
||||
if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) {
|
||||
log.Error("invalid path")
|
||||
@ -66,49 +61,39 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
fileByte, _, err := r.FormFile("chunk")
|
||||
fileByte, fileHeader, err := r.FormFile("chunk")
|
||||
if err != nil {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
log.Error("error getting upload info: " + err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer fileByte.Close()
|
||||
|
||||
dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
if _, err := io.Copy(dst, fileByte); err != nil {
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
rawIndex := r.FormValue("index")
|
||||
index, err := strconv.Atoi(rawIndex)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
db.DB.UpdateUpdateIndex(index, fileID)
|
||||
}
|
||||
|
||||
func createUploadDirectory(uploadDir string) error {
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
file.UpdateProgress(int64(index), file.UploadedByte+int64(fileHeader.Size))
|
||||
|
||||
func handleCookieError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if errors.Is(err, http.ErrNoCookie) {
|
||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
||||
dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.Error("error making upload folder: " + err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
handleError(w, err, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, err error, status int) {
|
||||
http.Error(w, err.Error(), status)
|
||||
log.Error(err.Error())
|
||||
defer dst.Close()
|
||||
if _, err := io.Copy(dst, fileByte); err != nil {
|
||||
log.Error("error copying byte to file dst: " + err.Error())
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if file.UploadedByte >= file.Size {
|
||||
file.FinalizeFileUpload()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user