Add responsiveness to file page and use correct file icons based on file type

This commit is contained in:
2024-09-16 11:02:25 +07:00
parent b246d85fac
commit dd8d0c0f4f
21 changed files with 797 additions and 590 deletions

View File

@ -15,7 +15,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
userSession := r.Context().Value("user").(types.User)
files, err := app.Server.Database.GetFiles(userSession.UserID.String())
if err != nil {
fmt.Println(err.Error())
app.Server.Logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -26,15 +26,34 @@ func GET(w http.ResponseWriter, r *http.Request) {
Name: files[i].Name,
Size: utils.ConvertFileSize(files[i].Size),
IsPrivate: files[i].IsPrivate,
Type: files[i].Type,
Downloaded: strconv.FormatUint(files[i].Downloaded, 10),
})
}
allowance, err := app.Server.Database.GetAllowance(userSession.UserID)
if err != nil {
app.Server.Logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
usage, err := app.Server.Service.GetUserStorageUsage(userSession.UserID.String())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
allowanceStats := &types.Allowance{
AllowanceByte: utils.ConvertFileSize(allowance.AllowanceByte),
AllowanceUsedByte: utils.ConvertFileSize(usage),
AllowanceUsedPercent: fmt.Sprintf("%.2f", float64(usage)/float64(allowance.AllowanceByte)*100),
}
var component templ.Component
if r.Header.Get("hx-request") == "true" {
component = fileView.MainContent(filesData, userSession)
component = fileView.MainContent(filesData, userSession, allowanceStats)
} else {
component = fileView.Main("File Dashboard", filesData, userSession)
component = fileView.Main("File Dashboard", filesData, userSession, allowanceStats)
}
err = component.Render(r.Context(), w)
if err != nil {

View File

@ -35,6 +35,7 @@ func PUT(w http.ResponseWriter, r *http.Request) {
Name: file.Name,
Size: utils.ConvertFileSize(file.Size),
IsPrivate: !file.IsPrivate,
Type: file.Type,
Downloaded: strconv.FormatUint(file.Downloaded, 10),
}
component := fileView.JustFile(fileData)

View File

@ -17,6 +17,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
)
var errorMessages = map[string]string{
@ -157,6 +158,21 @@ func handlerWS(conn *websocket.Conn, userSession types.User) {
}
var file *models.File
file, err = app.Server.Database.GetUserFile(uploadNewFile.Name, userSession.UserID.String())
allowedFileTypes := []string{"jpg", "jpeg", "png", "gif", "bmp", "tiff", "pdf", "doc", "docx", "txt", "odt", "xls", "xlsx", "ppt", "pptx", "zip", "rar", "tar", "gz", "7z", "bz2"}
isAllowedFileType := func(fileType string) bool {
for _, allowed := range allowedFileTypes {
if fileType == allowed {
return true
}
}
return false
}
fileName := strings.Split(uploadNewFile.Name, ".")
fileType := fileName[len(fileName)-1]
if !isAllowedFileType(fileType) {
fileType = "doc"
}
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
newFile := models.File{
@ -166,6 +182,7 @@ func handlerWS(conn *websocket.Conn, userSession types.User) {
Size: uploadNewFile.Size,
StartHash: uploadNewFile.StartHash,
EndHash: uploadNewFile.EndHash,
Type: fileType,
TotalChunk: uploadNewFile.Chunk,
Downloaded: 0,
}