Implement Redis caching for S3 list object calls
This commit is contained in:
@ -8,7 +8,6 @@ import (
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
@ -22,26 +21,14 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
var filesData []types.FileData
|
||||
|
||||
for _, file := range files {
|
||||
prefix := fmt.Sprintf("%s/%s/chunk_", file.OwnerID.String(), file.ID.String())
|
||||
|
||||
existingChunks, err := app.Server.Storage.ListObjects(r.Context(), prefix)
|
||||
userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String())
|
||||
if err != nil {
|
||||
app.Server.Logger.Error(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
missingChunk := len(existingChunks) != int(file.TotalChunk)
|
||||
|
||||
filesData = append(filesData, types.FileData{
|
||||
ID: file.ID.String(),
|
||||
Name: file.Name,
|
||||
Size: utils.ConvertFileSize(file.Size),
|
||||
IsPrivate: file.IsPrivate,
|
||||
Type: file.Type,
|
||||
Done: !missingChunk,
|
||||
Downloaded: strconv.FormatUint(file.Downloaded, 10),
|
||||
})
|
||||
filesData = append(filesData, *userFile)
|
||||
}
|
||||
|
||||
allowance, err := app.Server.Database.GetAllowance(userSession.UserID)
|
||||
@ -50,7 +37,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
usage, err := app.Server.Service.GetUserStorageUsage(userSession.UserID.String())
|
||||
usage, err := app.Server.Service.GetUserStorageUsage(r.Context(), userSession.UserID.String())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
|
@ -1,13 +1,10 @@
|
||||
package queryHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
@ -34,26 +31,14 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
var filesData []types.FileData
|
||||
|
||||
for _, file := range files {
|
||||
prefix := fmt.Sprintf("%s/%s/chunk_", file.OwnerID.String(), file.ID.String())
|
||||
|
||||
existingChunks, err := app.Server.Storage.ListObjects(r.Context(), prefix)
|
||||
userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
missingChunk := len(existingChunks) != int(file.TotalChunk)
|
||||
|
||||
filesData = append(filesData, types.FileData{
|
||||
ID: file.ID.String(),
|
||||
Name: file.Name,
|
||||
Size: utils.ConvertFileSize(file.Size),
|
||||
IsPrivate: file.IsPrivate,
|
||||
Type: file.Type,
|
||||
Done: !missingChunk,
|
||||
Downloaded: strconv.FormatUint(file.Downloaded, 10),
|
||||
})
|
||||
filesData = append(filesData, *userFile)
|
||||
}
|
||||
|
||||
if r.Header.Get("hx-request") == "true" {
|
||||
|
@ -1,13 +1,10 @@
|
||||
package renameFileHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func PATCH(w http.ResponseWriter, r *http.Request) {
|
||||
@ -39,28 +36,14 @@ func PATCH(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
prefix := fmt.Sprintf("%s/%s/chunk_", file.OwnerID.String(), file.ID.String())
|
||||
|
||||
existingChunks, err := app.Server.Storage.ListObjects(r.Context(), prefix)
|
||||
userFile, err := app.Server.Service.GetUserFile(r.Context(), newFile.Name, newFile.OwnerID.String())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
missingChunk := len(existingChunks) != int(file.TotalChunk)
|
||||
|
||||
fileData := types.FileData{
|
||||
ID: newFile.ID.String(),
|
||||
Name: newFile.Name,
|
||||
Size: utils.ConvertFileSize(newFile.Size),
|
||||
IsPrivate: newFile.IsPrivate,
|
||||
Type: newFile.Type,
|
||||
Done: !missingChunk,
|
||||
Downloaded: strconv.FormatUint(newFile.Downloaded, 10),
|
||||
}
|
||||
|
||||
component := fileView.JustFile(fileData)
|
||||
component := fileView.JustFile(*userFile)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
@ -16,7 +16,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
file, err := app.Server.Service.GetFile(fileID)
|
||||
file, err := app.Server.Service.GetFile(r.Context(), fileID)
|
||||
if err != nil {
|
||||
app.Server.Logger.Error("error getting upload info: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@ -52,7 +52,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
|
||||
app.Server.Logger.Error("error copying byte to file dst: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
app.Server.Service.UpdateFileChunk(r.Context(), file.ID, file.OwnerID, rawIndex, file.TotalChunk)
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
return
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
package visibilityHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func PUT(w http.ResponseWriter, r *http.Request) {
|
||||
@ -32,26 +29,13 @@ func PUT(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
prefix := fmt.Sprintf("%s/%s/chunk_", file.OwnerID.String(), file.ID.String())
|
||||
|
||||
existingChunks, err := app.Server.Storage.ListObjects(r.Context(), prefix)
|
||||
userFile, err := app.Server.Service.GetUserFile(r.Context(), file.Name, file.OwnerID.String())
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
missingChunk := len(existingChunks) != int(file.TotalChunk)
|
||||
fileData := types.FileData{
|
||||
ID: file.ID.String(),
|
||||
Name: file.Name,
|
||||
Size: utils.ConvertFileSize(file.Size),
|
||||
IsPrivate: !file.IsPrivate,
|
||||
Type: file.Type,
|
||||
Done: !missingChunk,
|
||||
Downloaded: strconv.FormatUint(file.Downloaded, 10),
|
||||
}
|
||||
component := fileView.JustFile(fileData)
|
||||
component := fileView.JustFile(*userFile)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
Reference in New Issue
Block a user