2023-09-18 23:57:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-09-27 22:58:47 +00:00
|
|
|
"crypto/md5"
|
2023-10-10 23:06:12 +00:00
|
|
|
"database/sql"
|
2023-09-18 23:57:18 +00:00
|
|
|
"fmt"
|
2023-10-31 03:35:37 +00:00
|
|
|
"io"
|
2023-11-18 04:10:59 +00:00
|
|
|
"math"
|
2023-09-23 18:14:57 +00:00
|
|
|
"mime/multipart"
|
2023-09-18 23:57:18 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-01-24 04:00:51 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2023-09-23 18:14:57 +00:00
|
|
|
"strings"
|
2023-09-23 02:12:36 +00:00
|
|
|
"time"
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
argon2 "github.com/alexedwards/argon2id"
|
2023-09-23 18:14:57 +00:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2023-09-18 23:57:18 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-09-23 18:14:57 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2024-10-04 23:40:03 +00:00
|
|
|
"reichard.io/antholume/api/renderer"
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/database"
|
|
|
|
"reichard.io/antholume/metadata"
|
2024-10-04 23:40:03 +00:00
|
|
|
"reichard.io/antholume/ngtemplates/common"
|
|
|
|
"reichard.io/antholume/ngtemplates/pages"
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/search"
|
|
|
|
"reichard.io/antholume/utils"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
2024-01-11 01:23:36 +00:00
|
|
|
type backupType string
|
2024-01-10 02:08:40 +00:00
|
|
|
|
|
|
|
const (
|
2024-01-11 01:23:36 +00:00
|
|
|
backupCovers backupType = "COVERS"
|
|
|
|
backupDocuments backupType = "DOCUMENTS"
|
2024-01-10 02:08:40 +00:00
|
|
|
)
|
|
|
|
|
2023-09-26 23:14:33 +00:00
|
|
|
type queryParams struct {
|
|
|
|
Page *int64 `form:"page"`
|
|
|
|
Limit *int64 `form:"limit"`
|
2023-11-18 02:24:19 +00:00
|
|
|
Search *string `form:"search"`
|
2023-09-26 23:14:33 +00:00
|
|
|
Document *string `form:"document"`
|
|
|
|
}
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
type searchParams struct {
|
2023-11-25 23:38:18 +00:00
|
|
|
Query *string `form:"query"`
|
|
|
|
Source *search.Source `form:"source"`
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 23:52:01 +00:00
|
|
|
type requestDocumentUpload struct {
|
|
|
|
DocumentFile *multipart.FileHeader `form:"document_file"`
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
type requestDocumentEdit struct {
|
|
|
|
Title *string `form:"title"`
|
|
|
|
Author *string `form:"author"`
|
|
|
|
Description *string `form:"description"`
|
2023-09-26 22:09:02 +00:00
|
|
|
ISBN10 *string `form:"isbn_10"`
|
|
|
|
ISBN13 *string `form:"isbn_13"`
|
2023-09-23 18:14:57 +00:00
|
|
|
RemoveCover *string `form:"remove_cover"`
|
2023-09-26 22:09:02 +00:00
|
|
|
CoverGBID *string `form:"cover_gbid"`
|
|
|
|
CoverFile *multipart.FileHeader `form:"cover_file"`
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type requestDocumentIdentify struct {
|
|
|
|
Title *string `form:"title"`
|
|
|
|
Author *string `form:"author"`
|
|
|
|
ISBN *string `form:"isbn"`
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
type requestSettingsEdit struct {
|
|
|
|
Password *string `form:"password"`
|
|
|
|
NewPassword *string `form:"new_password"`
|
2024-03-11 17:13:26 +00:00
|
|
|
Timezone *string `form:"timezone"`
|
2023-09-27 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
type requestDocumentAdd struct {
|
2023-11-25 23:38:18 +00:00
|
|
|
ID string `form:"id"`
|
|
|
|
Title *string `form:"title"`
|
|
|
|
Author *string `form:"author"`
|
|
|
|
Source search.Source `form:"source"`
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appWebManifest(c *gin.Context) {
|
2023-09-18 23:57:18 +00:00
|
|
|
c.Header("Content-Type", "application/manifest+json")
|
2024-01-27 19:56:01 +00:00
|
|
|
c.FileFromFS("assets/manifest.json", http.FS(api.assets))
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appServiceWorker(c *gin.Context) {
|
2024-01-27 19:56:01 +00:00
|
|
|
c.FileFromFS("assets/sw.js", http.FS(api.assets))
|
2023-10-29 00:07:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appFaviconIcon(c *gin.Context) {
|
2024-01-27 19:56:01 +00:00
|
|
|
c.FileFromFS("assets/icons/favicon.ico", http.FS(api.assets))
|
2023-11-26 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appLocalDocuments(c *gin.Context) {
|
2024-01-27 19:56:01 +00:00
|
|
|
c.FileFromFS("assets/local/index.htm", http.FS(api.assets))
|
2023-10-29 00:07:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appDocumentReader(c *gin.Context) {
|
2024-01-27 19:56:01 +00:00
|
|
|
c.FileFromFS("assets/reader/index.htm", http.FS(api.assets))
|
2023-10-29 00:07:24 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetDocuments(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("documents", c)
|
2024-01-01 04:12:46 +00:00
|
|
|
qParams := bindQueryParams(c, 9)
|
2023-09-21 00:35:01 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
var query *string
|
|
|
|
if qParams.Search != nil && *qParams.Search != "" {
|
|
|
|
search := "%" + *qParams.Search + "%"
|
|
|
|
query = &search
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
documents, err := api.db.Queries.GetDocumentsWithStats(api.db.Ctx, database.GetDocumentsWithStatsParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2024-01-01 04:12:46 +00:00
|
|
|
Query: query,
|
|
|
|
Offset: (*qParams.Page - 1) * *qParams.Limit,
|
|
|
|
Limit: *qParams.Limit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDocumentsWithStats DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-27 02:41:17 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
length, err := api.db.Queries.GetDocumentsSize(api.db.Ctx, query)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDocumentsSize DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsSize DB Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-18 02:24:19 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if err = api.getDocumentsWordCount(documents); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Unable to Get Word Counts: ", err)
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
totalPages := int64(math.Ceil(float64(length) / float64(*qParams.Limit)))
|
|
|
|
nextPage := *qParams.Page + 1
|
|
|
|
previousPage := *qParams.Page - 1
|
2023-11-18 04:10:59 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if nextPage <= totalPages {
|
|
|
|
templateVars["NextPage"] = nextPage
|
|
|
|
}
|
2023-10-01 23:17:22 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if previousPage >= 0 {
|
|
|
|
templateVars["PreviousPage"] = previousPage
|
|
|
|
}
|
2023-11-18 04:10:59 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["PageLimit"] = *qParams.Limit
|
|
|
|
templateVars["Data"] = documents
|
2023-11-18 04:10:59 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/documents", templateVars)
|
|
|
|
}
|
2023-11-18 04:10:59 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetDocument(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("document", c)
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid URI Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
document, err := api.db.Queries.GetDocumentWithStats(api.db.Ctx, database.GetDocumentWithStatsParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2024-01-01 04:12:46 +00:00
|
|
|
DocumentID: rDocID.DocumentID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-11-27 02:41:17 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = document
|
|
|
|
templateVars["TotalTimeLeftSeconds"] = int64((100.0 - document.Percentage) * float64(document.SecondsPerPercent))
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/document", templateVars)
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetProgress(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("progress", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
qParams := bindQueryParams(c, 15)
|
2023-09-21 00:35:01 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
progressFilter := database.GetProgressParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2024-01-01 04:12:46 +00:00
|
|
|
Offset: (*qParams.Page - 1) * *qParams.Limit,
|
|
|
|
Limit: *qParams.Limit,
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if qParams.Document != nil {
|
|
|
|
progressFilter.DocFilter = true
|
|
|
|
progressFilter.DocumentID = *qParams.Document
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
progress, err := api.db.Queries.GetProgress(api.db.Ctx, progressFilter)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetProgress DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-21 00:35:01 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = progress
|
2023-09-27 22:58:47 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/progress", templateVars)
|
|
|
|
}
|
2023-09-27 22:58:47 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetActivity(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("activity", c)
|
2024-01-01 04:12:46 +00:00
|
|
|
qParams := bindQueryParams(c, 15)
|
2023-10-26 10:20:56 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
activityFilter := database.GetActivityParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2024-01-01 04:12:46 +00:00
|
|
|
Offset: (*qParams.Page - 1) * *qParams.Limit,
|
|
|
|
Limit: *qParams.Limit,
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if qParams.Document != nil {
|
|
|
|
activityFilter.DocFilter = true
|
|
|
|
activityFilter.DocumentID = *qParams.Document
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
activity, err := api.db.Queries.GetActivity(api.db.Ctx, activityFilter)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetActivity DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = activity
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/activity", templateVars)
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
func (api *API) appGetHomeOld(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("home", c)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
start := time.Now()
|
2024-01-27 19:56:01 +00:00
|
|
|
graphData, err := api.db.Queries.GetDailyReadStats(api.db.Ctx, auth.UserName)
|
2024-01-24 04:00:51 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDailyReadStats DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDailyReadStats DB Error: %v", err))
|
2024-01-24 04:00:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("GetDailyReadStats DB Performance: ", time.Since(start))
|
2024-01-24 04:00:51 +00:00
|
|
|
|
|
|
|
start = time.Now()
|
2024-01-27 19:56:01 +00:00
|
|
|
databaseInfo, err := api.db.Queries.GetDatabaseInfo(api.db.Ctx, auth.UserName)
|
2024-01-24 04:00:51 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDatabaseInfo DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDatabaseInfo DB Error: %v", err))
|
2024-01-24 04:00:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("GetDatabaseInfo DB Performance: ", time.Since(start))
|
2024-01-01 04:12:46 +00:00
|
|
|
|
|
|
|
start = time.Now()
|
2024-01-27 19:56:01 +00:00
|
|
|
streaks, err := api.db.Queries.GetUserStreaks(api.db.Ctx, auth.UserName)
|
2024-01-24 04:00:51 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetUserStreaks DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStreaks DB Error: %v", err))
|
2024-01-24 04:00:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("GetUserStreaks DB Performance: ", time.Since(start))
|
2024-01-01 04:12:46 +00:00
|
|
|
|
2024-01-24 04:00:51 +00:00
|
|
|
start = time.Now()
|
2024-01-27 19:56:01 +00:00
|
|
|
userStatistics, err := api.db.Queries.GetUserStatistics(api.db.Ctx)
|
2024-01-24 04:00:51 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetUserStatistics DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStatistics DB Error: %v", err))
|
2024-01-24 04:00:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("GetUserStatistics DB Performance: ", time.Since(start))
|
2024-01-01 04:12:46 +00:00
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"Streaks": streaks,
|
|
|
|
"GraphData": graphData,
|
|
|
|
"DatabaseInfo": databaseInfo,
|
2024-01-24 04:00:51 +00:00
|
|
|
"UserStatistics": arrangeUserStatistics(userStatistics),
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "page/home", templateVars)
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
func (api *API) appGetHome(c *gin.Context) {
|
|
|
|
settings, auth := api.getBaseTemplateVarsNew(common.RouteHome, c)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
graphData, err := api.db.Queries.GetDailyReadStats(api.db.Ctx, auth.UserName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetDailyReadStats DB Error: ", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDailyReadStats DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("GetDailyReadStats DB Performance: ", time.Since(start))
|
|
|
|
|
|
|
|
start = time.Now()
|
|
|
|
databaseInfo, err := api.db.Queries.GetDatabaseInfo(api.db.Ctx, auth.UserName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetDatabaseInfo DB Error: ", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDatabaseInfo DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("GetDatabaseInfo DB Performance: ", time.Since(start))
|
|
|
|
|
|
|
|
start = time.Now()
|
|
|
|
streaks, err := api.db.Queries.GetUserStreaks(api.db.Ctx, auth.UserName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserStreaks DB Error: ", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStreaks DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("GetUserStreaks DB Performance: ", time.Since(start))
|
|
|
|
|
|
|
|
start = time.Now()
|
|
|
|
userStatistics, err := api.db.Queries.GetUserStatistics(api.db.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserStatistics DB Error: ", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStatistics DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug("GetUserStatistics DB Performance: ", time.Since(start))
|
|
|
|
|
|
|
|
r := renderer.New(c.Request.Context(), http.StatusOK, pages.Home(
|
|
|
|
settings,
|
|
|
|
getSVGGraphData(graphData, 800, 70),
|
|
|
|
streaks,
|
|
|
|
arrangeUserStatistics(userStatistics),
|
|
|
|
pages.UserMetadata{
|
|
|
|
DocumentCount: int(databaseInfo.DocumentsSize),
|
|
|
|
ActivityCount: int(databaseInfo.ActivitySize),
|
|
|
|
ProgressCount: int(databaseInfo.ProgressSize),
|
|
|
|
DeviceCount: int(databaseInfo.DevicesSize),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
c.Render(http.StatusOK, r)
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetSettings(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("settings", c)
|
2024-01-01 04:12:46 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
user, err := api.db.Queries.GetUser(api.db.Ctx, auth.UserName)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetUser DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
devices, err := api.db.Queries.GetDevices(api.db.Ctx, auth.UserName)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDevices DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = gin.H{
|
2024-03-11 17:13:26 +00:00
|
|
|
"Timezone": *user.Timezone,
|
|
|
|
"Devices": devices,
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/settings", templateVars)
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-20 19:26:26 +00:00
|
|
|
// Tabs:
|
|
|
|
// - General (Import, Backup & Restore, Version (githash?), Stats?)
|
|
|
|
// - Users
|
|
|
|
// - Metadata
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetSearch(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, _ := api.getBaseTemplateVars("search", c)
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
var sParams searchParams
|
2024-05-18 20:47:26 +00:00
|
|
|
err := c.BindQuery(&sParams)
|
|
|
|
if err != nil {
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("Invalid Form Bind: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
// Only Handle Query
|
|
|
|
if sParams.Query != nil && sParams.Source != nil {
|
|
|
|
// Search
|
|
|
|
searchResults, err := search.SearchBook(*sParams.Query, *sParams.Source)
|
|
|
|
if err != nil {
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("Search Error: %v", err))
|
2024-01-01 04:12:46 +00:00
|
|
|
return
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = searchResults
|
|
|
|
templateVars["Source"] = *sParams.Source
|
|
|
|
} else if sParams.Query != nil || sParams.Source != nil {
|
|
|
|
templateVars["SearchErrorMessage"] = "Invalid Query"
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/search", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appGetLogin(c *gin.Context) {
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, _ := api.getBaseTemplateVars("login", c)
|
2024-01-27 19:56:01 +00:00
|
|
|
templateVars["RegistrationEnabled"] = api.cfg.RegistrationEnabled
|
2024-01-01 04:12:46 +00:00
|
|
|
c.HTML(http.StatusOK, "page/login", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appGetRegister(c *gin.Context) {
|
2024-01-27 19:56:01 +00:00
|
|
|
if !api.cfg.RegistrationEnabled {
|
2024-01-01 04:12:46 +00:00
|
|
|
c.Redirect(http.StatusFound, "/login")
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, _ := api.getBaseTemplateVars("login", c)
|
2024-01-27 19:56:01 +00:00
|
|
|
templateVars["RegistrationEnabled"] = api.cfg.RegistrationEnabled
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Register"] = true
|
|
|
|
c.HTML(http.StatusOK, "page/login", templateVars)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetDocumentProgress(c *gin.Context) {
|
2024-01-10 02:08:40 +00:00
|
|
|
var auth authData
|
|
|
|
if data, _ := c.Get("Authorization"); data != nil {
|
|
|
|
auth = data.(authData)
|
|
|
|
}
|
2023-10-10 23:06:12 +00:00
|
|
|
|
|
|
|
var rDoc requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid URI Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2023-10-10 23:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
progress, err := api.db.Queries.GetDocumentProgress(api.db.Ctx, database.GetDocumentProgressParams{
|
2023-10-10 23:06:12 +00:00
|
|
|
DocumentID: rDoc.DocumentID,
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2023-10-10 23:06:12 +00:00
|
|
|
})
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2024-05-18 20:47:26 +00:00
|
|
|
log.Error("GetDocumentProgress DB Error: ", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentProgress DB Error: %v", err))
|
2023-10-10 23:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
document, err := api.db.Queries.GetDocumentWithStats(api.db.Ctx, database.GetDocumentWithStatsParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2023-10-10 23:06:12 +00:00
|
|
|
DocumentID: rDoc.DocumentID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
2023-10-10 23:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-29 00:07:24 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"id": document.ID,
|
|
|
|
"title": document.Title,
|
|
|
|
"author": document.Author,
|
|
|
|
"words": document.Words,
|
|
|
|
"progress": progress.Progress,
|
|
|
|
"percentage": document.Percentage,
|
2023-10-10 23:06:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetDevices(c *gin.Context) {
|
2024-01-10 02:08:40 +00:00
|
|
|
var auth authData
|
|
|
|
if data, _ := c.Get("Authorization"); data != nil {
|
|
|
|
auth = data.(authData)
|
|
|
|
}
|
2023-11-27 02:41:17 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
devices, err := api.db.Queries.GetDevices(api.db.Ctx, auth.UserName)
|
2023-11-27 02:41:17 +00:00
|
|
|
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDevices DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
2023-11-27 02:41:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, devices)
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appUploadNewDocument(c *gin.Context) {
|
2023-10-25 23:52:01 +00:00
|
|
|
var rDocUpload requestDocumentUpload
|
|
|
|
if err := c.ShouldBind(&rDocUpload); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rDocUpload.DocumentFile == nil {
|
|
|
|
c.Redirect(http.StatusFound, "./documents")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Temp File
|
|
|
|
tempFile, err := os.CreateTemp("", "book")
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Warn("Temp File Create Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to create temp file")
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-31 03:35:37 +00:00
|
|
|
defer os.Remove(tempFile.Name())
|
2023-10-25 23:52:01 +00:00
|
|
|
defer tempFile.Close()
|
|
|
|
|
2024-02-25 01:45:26 +00:00
|
|
|
// Save Temp File
|
2023-10-25 23:52:01 +00:00
|
|
|
err = c.SaveUploadedFile(rDocUpload.DocumentFile, tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("File Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to save file")
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Metadata
|
|
|
|
metadataInfo, err := metadata.GetMetadata(tempFile.Name())
|
|
|
|
if err != nil {
|
2024-02-25 01:45:26 +00:00
|
|
|
log.Errorf("unable to acquire metadata: %v", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to acquire metadata")
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-25 01:45:26 +00:00
|
|
|
// Check Already Exists
|
|
|
|
_, err = api.db.Queries.GetDocument(api.db.Ctx, *metadataInfo.PartialMD5)
|
2023-10-25 23:52:01 +00:00
|
|
|
if err == nil {
|
2024-02-25 01:45:26 +00:00
|
|
|
log.Warnf("document already exists: %s", *metadataInfo.PartialMD5)
|
|
|
|
c.Redirect(http.StatusFound, fmt.Sprintf("./documents/%s", *metadataInfo.PartialMD5))
|
2023-11-03 23:38:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 23:52:01 +00:00
|
|
|
// Derive & Sanitize File Name
|
2024-02-25 01:45:26 +00:00
|
|
|
fileName := deriveBaseFileName(metadataInfo)
|
2024-05-18 20:47:26 +00:00
|
|
|
basePath := filepath.Join(api.cfg.DataPath, "documents")
|
|
|
|
safePath := filepath.Join(basePath, fileName)
|
2024-02-25 01:45:26 +00:00
|
|
|
|
|
|
|
// Open Destination File
|
2023-10-31 03:35:37 +00:00
|
|
|
destFile, err := os.Create(safePath)
|
|
|
|
if err != nil {
|
2024-02-25 01:45:26 +00:00
|
|
|
log.Errorf("unable to open destination file: %v", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to open destination file")
|
2023-10-31 03:35:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer destFile.Close()
|
2023-10-25 23:52:01 +00:00
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
// Copy File
|
|
|
|
if _, err = io.Copy(destFile, tempFile); err != nil {
|
2024-02-25 01:45:26 +00:00
|
|
|
log.Errorf("unable to save file: %v", err)
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to save file")
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upsert Document
|
2024-01-27 19:56:01 +00:00
|
|
|
if _, err = api.db.Queries.UpsertDocument(api.db.Ctx, database.UpsertDocumentParams{
|
2024-02-25 01:45:26 +00:00
|
|
|
ID: *metadataInfo.PartialMD5,
|
2023-10-25 23:52:01 +00:00
|
|
|
Title: metadataInfo.Title,
|
|
|
|
Author: metadataInfo.Author,
|
|
|
|
Description: metadataInfo.Description,
|
2024-02-25 01:45:26 +00:00
|
|
|
Md5: metadataInfo.MD5,
|
|
|
|
Words: metadataInfo.WordCount,
|
2023-10-25 23:52:01 +00:00
|
|
|
Filepath: &fileName,
|
2024-05-18 20:47:26 +00:00
|
|
|
Basepath: &basePath,
|
2023-10-25 23:52:01 +00:00
|
|
|
}); err != nil {
|
2024-02-25 01:45:26 +00:00
|
|
|
log.Errorf("UpsertDocument DB Error: %v", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
2023-10-25 23:52:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-25 01:45:26 +00:00
|
|
|
c.Redirect(http.StatusFound, fmt.Sprintf("./documents/%s", *metadataInfo.PartialMD5))
|
2023-10-25 23:52:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appEditDocument(c *gin.Context) {
|
2023-09-23 18:14:57 +00:00
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid URI Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rDocEdit requestDocumentEdit
|
|
|
|
if err := c.ShouldBind(&rDocEdit); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Something Exists
|
|
|
|
if rDocEdit.Author == nil &&
|
|
|
|
rDocEdit.Title == nil &&
|
|
|
|
rDocEdit.Description == nil &&
|
2023-09-26 22:09:02 +00:00
|
|
|
rDocEdit.ISBN10 == nil &&
|
|
|
|
rDocEdit.ISBN13 == nil &&
|
|
|
|
rDocEdit.RemoveCover == nil &&
|
|
|
|
rDocEdit.CoverGBID == nil &&
|
|
|
|
rDocEdit.CoverFile == nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Missing Form Values")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Cover
|
|
|
|
var coverFileName *string
|
|
|
|
if rDocEdit.RemoveCover != nil && *rDocEdit.RemoveCover == "on" {
|
|
|
|
s := "UNKNOWN"
|
|
|
|
coverFileName = &s
|
|
|
|
} else if rDocEdit.CoverFile != nil {
|
|
|
|
// Validate Type & Derive Extension on MIME
|
|
|
|
uploadedFile, err := rDocEdit.CoverFile.Open()
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("File Error")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to open file")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("MIME Error")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to detect filetype")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fileExtension := fileMime.Extension()
|
|
|
|
|
|
|
|
// Validate Extension
|
|
|
|
if !slices.Contains([]string{".jpg", ".png"}, fileExtension) {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid FileType: ", fileExtension)
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid filetype")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate Storage Path
|
|
|
|
fileName := fmt.Sprintf("%s%s", rDocID.DocumentID, fileExtension)
|
2024-01-27 19:56:01 +00:00
|
|
|
safePath := filepath.Join(api.cfg.DataPath, "covers", fileName)
|
2023-09-23 18:14:57 +00:00
|
|
|
|
|
|
|
// Save
|
|
|
|
err = c.SaveUploadedFile(rDocEdit.CoverFile, safePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("File Error: ", err)
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, "Unable to save file")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
coverFileName = &fileName
|
2023-09-26 22:09:02 +00:00
|
|
|
} else if rDocEdit.CoverGBID != nil {
|
2024-01-27 19:56:01 +00:00
|
|
|
var coverDir string = filepath.Join(api.cfg.DataPath, "covers")
|
2023-10-01 23:17:22 +00:00
|
|
|
fileName, err := metadata.CacheCover(*rDocEdit.CoverGBID, coverDir, rDocID.DocumentID, true)
|
2023-09-26 22:09:02 +00:00
|
|
|
if err == nil {
|
|
|
|
coverFileName = fileName
|
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update Document
|
2024-01-27 19:56:01 +00:00
|
|
|
if _, err := api.db.Queries.UpsertDocument(api.db.Ctx, database.UpsertDocumentParams{
|
2023-09-23 18:14:57 +00:00
|
|
|
ID: rDocID.DocumentID,
|
|
|
|
Title: api.sanitizeInput(rDocEdit.Title),
|
|
|
|
Author: api.sanitizeInput(rDocEdit.Author),
|
|
|
|
Description: api.sanitizeInput(rDocEdit.Description),
|
2023-09-26 22:09:02 +00:00
|
|
|
Isbn10: api.sanitizeInput(rDocEdit.ISBN10),
|
|
|
|
Isbn13: api.sanitizeInput(rDocEdit.ISBN13),
|
2023-09-23 18:14:57 +00:00
|
|
|
Coverfile: coverFileName,
|
|
|
|
}); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("UpsertDocument DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "./")
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appDeleteDocument(c *gin.Context) {
|
2023-09-23 18:14:57 +00:00
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid URI Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-27 19:56:01 +00:00
|
|
|
changed, err := api.db.Queries.DeleteDocument(api.db.Ctx, rDocID.DocumentID)
|
2023-09-23 18:14:57 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("DeleteDocument DB Error")
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("DeleteDocument DB Error: %v", err))
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if changed == 0 {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("DeleteDocument DB Error")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "../")
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appIdentifyDocument(c *gin.Context) {
|
2023-09-23 18:14:57 +00:00
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid URI Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusNotFound, "Invalid document")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rDocIdentify requestDocumentIdentify
|
|
|
|
if err := c.ShouldBind(&rDocIdentify); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow Empty Strings
|
|
|
|
if rDocIdentify.Title != nil && strings.TrimSpace(*rDocIdentify.Title) == "" {
|
|
|
|
rDocIdentify.Title = nil
|
|
|
|
}
|
|
|
|
if rDocIdentify.Author != nil && strings.TrimSpace(*rDocIdentify.Author) == "" {
|
|
|
|
rDocIdentify.Author = nil
|
|
|
|
}
|
|
|
|
if rDocIdentify.ISBN != nil && strings.TrimSpace(*rDocIdentify.ISBN) == "" {
|
|
|
|
rDocIdentify.ISBN = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Values
|
|
|
|
if rDocIdentify.ISBN == nil && rDocIdentify.Title == nil && rDocIdentify.Author == nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
// Get Template Variables
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("document", c)
|
2023-09-26 22:09:02 +00:00
|
|
|
|
|
|
|
// Get Metadata
|
2024-02-25 01:45:26 +00:00
|
|
|
metadataResults, err := metadata.SearchMetadata(metadata.SOURCE_GBOOK, metadata.MetadataInfo{
|
2023-09-23 18:14:57 +00:00
|
|
|
Title: rDocIdentify.Title,
|
|
|
|
Author: rDocIdentify.Author,
|
|
|
|
ISBN10: rDocIdentify.ISBN,
|
|
|
|
ISBN13: rDocIdentify.ISBN,
|
|
|
|
})
|
2023-09-26 22:09:02 +00:00
|
|
|
if err == nil && len(metadataResults) > 0 {
|
|
|
|
firstResult := metadataResults[0]
|
2023-09-23 18:14:57 +00:00
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
// Store First Metadata Result
|
2024-01-27 19:56:01 +00:00
|
|
|
if _, err = api.db.Queries.AddMetadata(api.db.Ctx, database.AddMetadataParams{
|
2023-09-26 22:09:02 +00:00
|
|
|
DocumentID: rDocID.DocumentID,
|
|
|
|
Title: firstResult.Title,
|
|
|
|
Author: firstResult.Author,
|
|
|
|
Description: firstResult.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
Gbid: firstResult.ID,
|
|
|
|
Olid: nil,
|
2023-09-26 22:09:02 +00:00
|
|
|
Isbn10: firstResult.ISBN10,
|
|
|
|
Isbn13: firstResult.ISBN13,
|
|
|
|
}); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("AddMetadata DB Error: ", err)
|
2023-09-26 22:09:02 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["Metadata"] = firstResult
|
|
|
|
} else {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Warn("Metadata Error")
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["MetadataError"] = "No Metadata Found"
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
2023-09-26 22:09:02 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
document, err := api.db.Queries.GetDocumentWithStats(api.db.Ctx, database.GetDocumentWithStatsParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2023-09-26 22:09:02 +00:00
|
|
|
DocumentID: rDocID.DocumentID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
2023-09-26 22:09:02 +00:00
|
|
|
return
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["Data"] = document
|
2023-11-03 23:38:31 +00:00
|
|
|
templateVars["TotalTimeLeftSeconds"] = int64((100.0 - document.Percentage) * float64(document.SecondsPerPercent))
|
2023-09-26 22:09:02 +00:00
|
|
|
|
2023-11-29 01:05:50 +00:00
|
|
|
c.HTML(http.StatusOK, "page/document", templateVars)
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
2023-09-26 23:14:33 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appSaveNewDocument(c *gin.Context) {
|
2023-10-07 01:25:56 +00:00
|
|
|
var rDocAdd requestDocumentAdd
|
|
|
|
if err := c.ShouldBind(&rDocAdd); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-10-07 01:25:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Render Initial Template
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, _ := api.getBaseTemplateVars("search", c)
|
2023-12-01 12:35:51 +00:00
|
|
|
c.HTML(http.StatusOK, "page/search", templateVars)
|
|
|
|
|
|
|
|
// Create Streamer
|
2024-01-20 19:26:26 +00:00
|
|
|
stream := api.newStreamer(c, `
|
|
|
|
<div class="absolute top-0 left-0 w-full h-full z-50">
|
|
|
|
<div class="fixed top-0 left-0 bg-black opacity-50 w-screen h-screen"></div>
|
|
|
|
<div id="stream-main" class="relative max-h-[95%] -translate-x-2/4 top-1/2 left-1/2 w-5/6">`)
|
|
|
|
defer stream.close(`</div></div>`)
|
2023-12-01 12:35:51 +00:00
|
|
|
|
|
|
|
// Stream Helper Function
|
|
|
|
sendDownloadMessage := func(msg string, args ...map[string]any) {
|
|
|
|
// Merge Defaults & Overrides
|
|
|
|
var templateVars = gin.H{
|
|
|
|
"Message": msg,
|
|
|
|
"ButtonText": "Close",
|
|
|
|
"ButtonHref": "./search",
|
|
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
|
|
for key := range args[0] {
|
|
|
|
templateVars[key] = args[0][key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.send("component/download-progress", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Downloading document...", gin.H{"Progress": 10})
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
// Save Book
|
2023-11-25 23:38:18 +00:00
|
|
|
tempFilePath, err := search.SaveBook(rDocAdd.ID, rDocAdd.Source)
|
2023-10-07 01:25:56 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Warn("Temp File Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to download file", gin.H{"Error": true})
|
2023-10-07 01:25:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Calculating partial MD5...", gin.H{"Progress": 60})
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
// Calculate Partial MD5 ID
|
2023-10-25 23:52:01 +00:00
|
|
|
partialMD5, err := utils.CalculatePartialMD5(tempFilePath)
|
2023-10-07 01:25:56 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Warn("Partial MD5 Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to calculate partial MD5", gin.H{"Error": true})
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Saving file...", gin.H{"Progress": 60})
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
// Derive Extension on MIME
|
|
|
|
fileMime, err := mimetype.DetectFile(tempFilePath)
|
2024-05-18 20:47:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("MIME Detect Error: ", err)
|
|
|
|
sendDownloadMessage("Unable to download file", gin.H{"Error": true})
|
|
|
|
return
|
|
|
|
}
|
2023-10-07 01:25:56 +00:00
|
|
|
fileExtension := fileMime.Extension()
|
|
|
|
|
|
|
|
// Derive Filename
|
|
|
|
var fileName string
|
|
|
|
if *rDocAdd.Author != "" {
|
|
|
|
fileName = fileName + *rDocAdd.Author
|
|
|
|
} else {
|
|
|
|
fileName = fileName + "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
if *rDocAdd.Title != "" {
|
|
|
|
fileName = fileName + " - " + *rDocAdd.Title
|
|
|
|
} else {
|
|
|
|
fileName = fileName + " - Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove Slashes
|
|
|
|
fileName = strings.ReplaceAll(fileName, "/", "")
|
|
|
|
|
|
|
|
// Derive & Sanitize File Name
|
2024-02-25 01:45:26 +00:00
|
|
|
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, *partialMD5, fileExtension))
|
2023-10-07 01:25:56 +00:00
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
// Open Source File
|
|
|
|
sourceFile, err := os.Open(tempFilePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Source File Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to open file", gin.H{"Error": true})
|
2023-10-31 03:35:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(tempFilePath)
|
|
|
|
defer sourceFile.Close()
|
|
|
|
|
|
|
|
// Generate Storage Path & Open File
|
2024-05-18 20:47:26 +00:00
|
|
|
basePath := filepath.Join(api.cfg.DataPath, "documents")
|
|
|
|
safePath := filepath.Join(basePath, fileName)
|
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
destFile, err := os.Create(safePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Dest File Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to create file", gin.H{"Error": true})
|
2023-10-31 03:35:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer destFile.Close()
|
2023-10-07 01:25:56 +00:00
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
// Copy File
|
|
|
|
if _, err = io.Copy(destFile, sourceFile); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Copy Temp File Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to save file", gin.H{"Error": true})
|
2023-10-07 01:25:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Calculating MD5...", gin.H{"Progress": 70})
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
// Get MD5 Hash
|
|
|
|
fileHash, err := getFileMD5(safePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Hash Failure: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to calculate MD5", gin.H{"Error": true})
|
2023-10-07 01:25:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Calculating word count...", gin.H{"Progress": 80})
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
// Get Word Count
|
|
|
|
wordCount, err := metadata.GetWordCount(safePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Word Count Failure: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to calculate word count", gin.H{"Error": true})
|
2023-11-03 23:38:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Saving to database...", gin.H{"Progress": 90})
|
|
|
|
|
2023-10-07 01:25:56 +00:00
|
|
|
// Upsert Document
|
2024-01-27 19:56:01 +00:00
|
|
|
if _, err = api.db.Queries.UpsertDocument(api.db.Ctx, database.UpsertDocumentParams{
|
2024-02-25 01:45:26 +00:00
|
|
|
ID: *partialMD5,
|
2023-10-07 01:25:56 +00:00
|
|
|
Title: rDocAdd.Title,
|
|
|
|
Author: rDocAdd.Author,
|
|
|
|
Md5: fileHash,
|
2024-02-25 01:45:26 +00:00
|
|
|
Words: wordCount,
|
2024-05-18 20:47:26 +00:00
|
|
|
Filepath: &fileName,
|
|
|
|
Basepath: &basePath,
|
2023-10-07 01:25:56 +00:00
|
|
|
}); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("UpsertDocument DB Error: ", err)
|
2023-12-01 12:35:51 +00:00
|
|
|
sendDownloadMessage("Unable to save to database", gin.H{"Error": true})
|
2023-10-07 01:25:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-01 12:35:51 +00:00
|
|
|
// Send Message
|
|
|
|
sendDownloadMessage("Download Success", gin.H{
|
|
|
|
"Progress": 100,
|
|
|
|
"ButtonText": "Go to Book",
|
2024-02-25 01:45:26 +00:00
|
|
|
"ButtonHref": fmt.Sprintf("./documents/%s", *partialMD5),
|
2023-12-01 12:35:51 +00:00
|
|
|
})
|
2023-10-07 01:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appEditSettings(c *gin.Context) {
|
2023-09-27 22:58:47 +00:00
|
|
|
var rUserSettings requestSettingsEdit
|
|
|
|
if err := c.ShouldBind(&rUserSettings); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Invalid Form Bind")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Something Exists
|
2024-03-11 17:13:26 +00:00
|
|
|
if rUserSettings.Password == nil && rUserSettings.NewPassword == nil && rUserSettings.Timezone == nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Missing Form Values")
|
2024-02-25 01:45:26 +00:00
|
|
|
appErrorPage(c, http.StatusBadRequest, "Invalid or missing form values")
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-20 19:26:26 +00:00
|
|
|
templateVars, auth := api.getBaseTemplateVars("settings", c)
|
2024-01-01 04:12:46 +00:00
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
newUserSettings := database.UpdateUserParams{
|
2024-01-10 02:08:40 +00:00
|
|
|
UserID: auth.UserName,
|
2024-03-11 01:48:43 +00:00
|
|
|
Admin: auth.IsAdmin,
|
2023-09-27 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set New Password
|
|
|
|
if rUserSettings.Password != nil && rUserSettings.NewPassword != nil {
|
|
|
|
password := fmt.Sprintf("%x", md5.Sum([]byte(*rUserSettings.Password)))
|
2024-01-10 02:08:40 +00:00
|
|
|
data := api.authorizeCredentials(auth.UserName, password)
|
|
|
|
if data == nil {
|
|
|
|
templateVars["PasswordErrorMessage"] = "Invalid Password"
|
|
|
|
} else {
|
2023-09-27 22:58:47 +00:00
|
|
|
password := fmt.Sprintf("%x", md5.Sum([]byte(*rUserSettings.NewPassword)))
|
|
|
|
hashedPassword, err := argon2.CreateHash(password, argon2.DefaultParams)
|
|
|
|
if err != nil {
|
|
|
|
templateVars["PasswordErrorMessage"] = "Unknown Error"
|
|
|
|
} else {
|
|
|
|
templateVars["PasswordMessage"] = "Password Updated"
|
|
|
|
newUserSettings.Password = &hashedPassword
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Time Offset
|
2024-03-11 17:13:26 +00:00
|
|
|
if rUserSettings.Timezone != nil {
|
2023-09-27 22:58:47 +00:00
|
|
|
templateVars["TimeOffsetMessage"] = "Time Offset Updated"
|
2024-03-11 17:13:26 +00:00
|
|
|
newUserSettings.Timezone = rUserSettings.Timezone
|
2023-09-27 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update User
|
2024-01-27 19:56:01 +00:00
|
|
|
_, err := api.db.Queries.UpdateUser(api.db.Ctx, newUserSettings)
|
2023-09-27 22:58:47 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("UpdateUser DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpdateUser DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get User
|
2024-01-27 19:56:01 +00:00
|
|
|
user, err := api.db.Queries.GetUser(api.db.Ctx, auth.UserName)
|
2023-09-27 22:58:47 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetUser DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Devices
|
2024-01-27 19:56:01 +00:00
|
|
|
devices, err := api.db.Queries.GetDevices(api.db.Ctx, auth.UserName)
|
2023-09-27 22:58:47 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("GetDevices DB Error: ", err)
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
2024-03-11 17:13:26 +00:00
|
|
|
"Timezone": *user.Timezone,
|
|
|
|
"Devices": devices,
|
2023-09-27 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:05:50 +00:00
|
|
|
c.HTML(http.StatusOK, "page/settings", templateVars)
|
2023-09-27 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appDemoModeError(c *gin.Context) {
|
2024-01-27 03:07:30 +00:00
|
|
|
appErrorPage(c, http.StatusUnauthorized, "Not Allowed in Demo Mode")
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 23:17:22 +00:00
|
|
|
func (api *API) getDocumentsWordCount(documents []database.GetDocumentsWithStatsRow) error {
|
|
|
|
// Do Transaction
|
2024-01-27 19:56:01 +00:00
|
|
|
tx, err := api.db.DB.Begin()
|
2023-10-01 23:17:22 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Transaction Begin DB Error: ", err)
|
2023-10-01 23:17:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer & Start Transaction
|
2024-05-18 20:47:26 +00:00
|
|
|
defer func() {
|
|
|
|
if err := tx.Rollback(); err != nil {
|
|
|
|
log.Error("DB Rollback Error:", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-01-27 19:56:01 +00:00
|
|
|
qtx := api.db.Queries.WithTx(tx)
|
2023-10-01 23:17:22 +00:00
|
|
|
|
|
|
|
for _, item := range documents {
|
|
|
|
if item.Words == nil && item.Filepath != nil {
|
2024-01-27 19:56:01 +00:00
|
|
|
filePath := filepath.Join(api.cfg.DataPath, "documents", *item.Filepath)
|
2023-10-01 23:17:22 +00:00
|
|
|
wordCount, err := metadata.GetWordCount(filePath)
|
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Warn("Word Count Error: ", err)
|
2023-10-01 23:17:22 +00:00
|
|
|
} else {
|
2024-01-27 19:56:01 +00:00
|
|
|
if _, err := qtx.UpsertDocument(api.db.Ctx, database.UpsertDocumentParams{
|
2023-10-01 23:17:22 +00:00
|
|
|
ID: item.ID,
|
2024-02-25 01:45:26 +00:00
|
|
|
Words: wordCount,
|
2023-10-01 23:17:22 +00:00
|
|
|
}); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("UpsertDocument DB Error: ", err)
|
2023-10-01 23:17:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit Transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Error("Transaction Commit DB Error: ", err)
|
2023-10-01 23:17:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-20 19:26:26 +00:00
|
|
|
func (api *API) getBaseTemplateVars(routeName string, c *gin.Context) (gin.H, authData) {
|
2024-01-10 02:08:40 +00:00
|
|
|
var auth authData
|
|
|
|
if data, _ := c.Get("Authorization"); data != nil {
|
|
|
|
auth = data.(authData)
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gin.H{
|
2024-01-10 02:08:40 +00:00
|
|
|
"Authorization": auth,
|
|
|
|
"RouteName": routeName,
|
2024-01-01 04:12:46 +00:00
|
|
|
"Config": gin.H{
|
2024-01-27 19:56:01 +00:00
|
|
|
"Version": api.cfg.Version,
|
|
|
|
"SearchEnabled": api.cfg.SearchEnabled,
|
|
|
|
"RegistrationEnabled": api.cfg.RegistrationEnabled,
|
2024-01-01 04:12:46 +00:00
|
|
|
},
|
2024-01-20 19:26:26 +00:00
|
|
|
}, auth
|
2024-01-01 04:12:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
func (api *API) getBaseTemplateVarsNew(route common.Route, c *gin.Context) (common.Settings, authData) {
|
|
|
|
var auth authData
|
|
|
|
if data, _ := c.Get("Authorization"); data != nil {
|
|
|
|
auth = data.(authData)
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.Settings{
|
|
|
|
Route: route,
|
|
|
|
User: auth.UserName,
|
|
|
|
IsAdmin: auth.IsAdmin,
|
|
|
|
SearchEnabled: api.cfg.SearchEnabled,
|
|
|
|
Version: api.cfg.Version,
|
|
|
|
}, auth
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:41:17 +00:00
|
|
|
func bindQueryParams(c *gin.Context, defaultLimit int64) queryParams {
|
2023-09-26 23:14:33 +00:00
|
|
|
var qParams queryParams
|
2024-05-18 20:47:26 +00:00
|
|
|
err := c.BindQuery(&qParams)
|
|
|
|
if err != nil {
|
|
|
|
appErrorPage(c, http.StatusInternalServerError, fmt.Sprintf("Invalid Form Bind: %v", err))
|
|
|
|
return qParams
|
|
|
|
}
|
2023-09-26 23:14:33 +00:00
|
|
|
|
|
|
|
if qParams.Limit == nil {
|
2023-11-27 02:41:17 +00:00
|
|
|
qParams.Limit = &defaultLimit
|
2023-09-26 23:14:33 +00:00
|
|
|
} else if *qParams.Limit < 0 {
|
|
|
|
var zeroValue int64 = 0
|
|
|
|
qParams.Limit = &zeroValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if qParams.Page == nil || *qParams.Page < 1 {
|
2023-11-18 04:10:59 +00:00
|
|
|
var oneValue int64 = 1
|
2023-09-26 23:14:33 +00:00
|
|
|
qParams.Page = &oneValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return qParams
|
|
|
|
}
|
2023-10-25 23:52:01 +00:00
|
|
|
|
2024-01-27 03:07:30 +00:00
|
|
|
func appErrorPage(c *gin.Context, errorCode int, errorMessage string) {
|
2023-10-25 23:52:01 +00:00
|
|
|
var errorHuman string = "We're not even sure what happened."
|
|
|
|
|
|
|
|
switch errorCode {
|
|
|
|
case http.StatusInternalServerError:
|
|
|
|
errorHuman = "Server hiccup."
|
|
|
|
case http.StatusNotFound:
|
|
|
|
errorHuman = "Something's missing."
|
|
|
|
case http.StatusBadRequest:
|
|
|
|
errorHuman = "We didn't expect that."
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
errorHuman = "You're not allowed to do that."
|
|
|
|
}
|
|
|
|
|
2023-11-29 01:05:50 +00:00
|
|
|
c.HTML(errorCode, "page/error", gin.H{
|
2023-10-25 23:52:01 +00:00
|
|
|
"Status": errorCode,
|
|
|
|
"Error": errorHuman,
|
|
|
|
"Message": errorMessage,
|
|
|
|
})
|
|
|
|
}
|
2024-01-24 04:00:51 +00:00
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
func arrangeUserStatistics(userStatistics []database.GetUserStatisticsRow) pages.UserStatistics {
|
2024-01-24 04:00:51 +00:00
|
|
|
// Item Sorter
|
2024-10-04 23:40:03 +00:00
|
|
|
sortItem := func(userStatistics []database.GetUserStatisticsRow, key string, less func(i int, j int) bool) []pages.UserStatisticEntry {
|
2024-01-24 04:00:51 +00:00
|
|
|
sortedData := append([]database.GetUserStatisticsRow(nil), userStatistics...)
|
|
|
|
sort.SliceStable(sortedData, less)
|
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
newData := make([]pages.UserStatisticEntry, 0)
|
2024-01-24 04:00:51 +00:00
|
|
|
for _, item := range sortedData {
|
|
|
|
v := reflect.Indirect(reflect.ValueOf(item))
|
|
|
|
|
|
|
|
var value string
|
|
|
|
if strings.Contains(key, "Wpm") {
|
|
|
|
rawVal := v.FieldByName(key).Float()
|
|
|
|
value = fmt.Sprintf("%.2f WPM", rawVal)
|
|
|
|
} else if strings.Contains(key, "Seconds") {
|
|
|
|
rawVal := v.FieldByName(key).Int()
|
|
|
|
value = niceSeconds(rawVal)
|
|
|
|
} else if strings.Contains(key, "Words") {
|
|
|
|
rawVal := v.FieldByName(key).Int()
|
|
|
|
value = niceNumbers(rawVal)
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
newData = append(newData, pages.UserStatisticEntry{
|
|
|
|
UserID: item.UserID,
|
|
|
|
Value: value,
|
2024-01-24 04:00:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return newData
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:40:03 +00:00
|
|
|
return pages.UserStatistics{
|
|
|
|
WPM: map[string][]pages.UserStatisticEntry{
|
2024-01-24 04:00:51 +00:00
|
|
|
"All": sortItem(userStatistics, "TotalWpm", func(i, j int) bool {
|
|
|
|
return userStatistics[i].TotalWpm > userStatistics[j].TotalWpm
|
|
|
|
}),
|
|
|
|
"Year": sortItem(userStatistics, "YearlyWpm", func(i, j int) bool {
|
|
|
|
return userStatistics[i].YearlyWpm > userStatistics[j].YearlyWpm
|
|
|
|
}),
|
|
|
|
"Month": sortItem(userStatistics, "MonthlyWpm", func(i, j int) bool {
|
|
|
|
return userStatistics[i].MonthlyWpm > userStatistics[j].MonthlyWpm
|
|
|
|
}),
|
|
|
|
"Week": sortItem(userStatistics, "WeeklyWpm", func(i, j int) bool {
|
|
|
|
return userStatistics[i].WeeklyWpm > userStatistics[j].WeeklyWpm
|
|
|
|
}),
|
|
|
|
},
|
2024-10-04 23:40:03 +00:00
|
|
|
Duration: map[string][]pages.UserStatisticEntry{
|
2024-01-24 04:00:51 +00:00
|
|
|
"All": sortItem(userStatistics, "TotalSeconds", func(i, j int) bool {
|
|
|
|
return userStatistics[i].TotalSeconds > userStatistics[j].TotalSeconds
|
|
|
|
}),
|
|
|
|
"Year": sortItem(userStatistics, "YearlySeconds", func(i, j int) bool {
|
|
|
|
return userStatistics[i].YearlySeconds > userStatistics[j].YearlySeconds
|
|
|
|
}),
|
|
|
|
"Month": sortItem(userStatistics, "MonthlySeconds", func(i, j int) bool {
|
|
|
|
return userStatistics[i].MonthlySeconds > userStatistics[j].MonthlySeconds
|
|
|
|
}),
|
|
|
|
"Week": sortItem(userStatistics, "WeeklySeconds", func(i, j int) bool {
|
|
|
|
return userStatistics[i].WeeklySeconds > userStatistics[j].WeeklySeconds
|
|
|
|
}),
|
|
|
|
},
|
2024-10-04 23:40:03 +00:00
|
|
|
Words: map[string][]pages.UserStatisticEntry{
|
2024-01-24 04:00:51 +00:00
|
|
|
"All": sortItem(userStatistics, "TotalWordsRead", func(i, j int) bool {
|
|
|
|
return userStatistics[i].TotalWordsRead > userStatistics[j].TotalWordsRead
|
|
|
|
}),
|
|
|
|
"Year": sortItem(userStatistics, "YearlyWordsRead", func(i, j int) bool {
|
|
|
|
return userStatistics[i].YearlyWordsRead > userStatistics[j].YearlyWordsRead
|
|
|
|
}),
|
|
|
|
"Month": sortItem(userStatistics, "MonthlyWordsRead", func(i, j int) bool {
|
|
|
|
return userStatistics[i].MonthlyWordsRead > userStatistics[j].MonthlyWordsRead
|
|
|
|
}),
|
|
|
|
"Week": sortItem(userStatistics, "WeeklyWordsRead", func(i, j int) bool {
|
|
|
|
return userStatistics[i].WeeklyWordsRead > userStatistics[j].WeeklyWordsRead
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|