feat: add file visibility toggle for files
This commit is contained in:
@ -3,6 +3,7 @@ package downloadHandler
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/session"
|
||||
"github.com/fossyy/filekeeper/types/models"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -21,6 +22,17 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
status, userSession, _ := session.GetSession(r)
|
||||
if file.IsPrivate {
|
||||
if status == session.Unauthorized || status == session.InvalidSession {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
} else if file.OwnerID != userSession.UserID {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
uploadDir := "uploads"
|
||||
currentDir, _ := os.Getwd()
|
||||
basePath := filepath.Join(currentDir, uploadDir)
|
||||
|
@ -25,13 +25,14 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
||||
ID: files[i].ID.String(),
|
||||
Name: files[i].Name,
|
||||
Size: utils.ConvertFileSize(files[i].Size),
|
||||
IsPrivate: files[i].IsPrivate,
|
||||
Downloaded: strconv.FormatUint(files[i].Downloaded, 10),
|
||||
})
|
||||
}
|
||||
|
||||
var component templ.Component
|
||||
if r.Header.Get("hx-request") == "true" {
|
||||
component = fileView.MainContent(filesData)
|
||||
component = fileView.MainContent(filesData, userSession)
|
||||
} else {
|
||||
component = fileView.Main("File Dashboard", filesData, userSession)
|
||||
}
|
||||
|
46
handler/file/visibility/visibility.go
Normal file
46
handler/file/visibility/visibility.go
Normal file
@ -0,0 +1,46 @@
|
||||
package visibilityHandler
|
||||
|
||||
import (
|
||||
"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) {
|
||||
fileID := r.PathValue("id")
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
file, err := app.Server.Database.GetFile(fileID)
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if file.OwnerID != userSession.UserID {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.Server.Database.ChangeFileVisibility(fileID)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
fileData := types.FileData{
|
||||
ID: file.ID.String(),
|
||||
Name: file.Name,
|
||||
Size: utils.ConvertFileSize(file.Size),
|
||||
IsPrivate: !file.IsPrivate,
|
||||
Downloaded: strconv.FormatUint(file.Downloaded, 10),
|
||||
}
|
||||
component := fileView.JustFile(fileData)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user