2023-09-18 23:57:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-01-10 02:08:40 +00:00
|
|
|
"archive/zip"
|
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"
|
2024-01-10 02:08:40 +00:00
|
|
|
"io/fs"
|
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"
|
2024-01-10 02:08:40 +00:00
|
|
|
"path"
|
2023-09-18 23:57:18 +00:00
|
|
|
"path/filepath"
|
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"
|
2023-09-18 23:57:18 +00:00
|
|
|
"reichard.io/bbank/database"
|
|
|
|
"reichard.io/bbank/metadata"
|
2023-10-07 01:25:56 +00:00
|
|
|
"reichard.io/bbank/search"
|
2023-10-25 23:52:01 +00:00
|
|
|
"reichard.io/bbank/utils"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
2024-01-10 02:08:40 +00:00
|
|
|
type AdminAction string
|
|
|
|
|
|
|
|
const (
|
|
|
|
AA_IMPORT AdminAction = "IMPORT"
|
|
|
|
AA_BACKUP AdminAction = "BACKUP"
|
|
|
|
AA_RESTORE AdminAction = "RESTORE"
|
|
|
|
AA_METADATA_MATCH AdminAction = "METADATA_MATCH"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImportType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
IMPORT_TYPE_DIRECT ImportType = "DIRECT"
|
|
|
|
IMPORT_TYPE_COPY ImportType = "COPY"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BackupType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
BACKUP_TYPE_COVERS BackupType = "COVERS"
|
|
|
|
BACKUP_TYPE_DOCUMENTS BackupType = "DOCUMENTS"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-10 02:08:40 +00:00
|
|
|
type requestAdminAction struct {
|
|
|
|
Action AdminAction `form:"action"`
|
|
|
|
|
|
|
|
// Import Action
|
|
|
|
ImportDirectory *string `form:"import_directory"`
|
|
|
|
ImportType *ImportType `form:"import_type"`
|
|
|
|
|
|
|
|
// Backup Action
|
|
|
|
BackupTypes []BackupType `form:"backup_types"`
|
|
|
|
|
|
|
|
// Restore Action
|
|
|
|
RestoreFile *multipart.FileHeader `form:"restore_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"`
|
|
|
|
TimeOffset *string `form:"time_offset"`
|
|
|
|
}
|
|
|
|
|
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")
|
2023-11-29 03:25:47 +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) {
|
2023-11-29 03:25:47 +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) {
|
2023-11-29 03:25:47 +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) {
|
2023-11-29 03:25:47 +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) {
|
2023-11-29 03:25:47 +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) {
|
|
|
|
templateVars := api.getBaseTemplateVars("documents", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
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-01 04:12:46 +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 {
|
|
|
|
log.Error("[appGetDocuments] GetDocumentsWithStats DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2023-11-27 02:41:17 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
length, err := api.DB.Queries.GetDocumentsSize(api.DB.Ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[appGetDocuments] GetDocumentsSize DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsSize DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2023-11-18 02:24:19 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
if err = api.getDocumentsWordCount(documents); err != nil {
|
|
|
|
log.Error("[appGetDocuments] Unable to Get Word Counts: ", err)
|
|
|
|
}
|
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) {
|
|
|
|
templateVars := api.getBaseTemplateVars("document", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
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 {
|
|
|
|
log.Error("[appGetDocument] Invalid URI Bind")
|
|
|
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
|
|
|
return
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +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 {
|
|
|
|
log.Error("[appGetDocument] GetDocumentWithStats DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
|
|
|
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) {
|
|
|
|
templateVars := api.getBaseTemplateVars("progress", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
progress, err := api.DB.Queries.GetProgress(api.DB.Ctx, progressFilter)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[appGetProgress] GetProgress DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
|
|
|
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) {
|
|
|
|
templateVars := api.getBaseTemplateVars("activity", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
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-01 04:12:46 +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-01 04:12:46 +00:00
|
|
|
log.Error("[appGetActivity] GetActivity DB Error:", err)
|
|
|
|
errorPage(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-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetHome(c *gin.Context) {
|
|
|
|
templateVars := api.getBaseTemplateVars("home", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
start := time.Now()
|
2024-01-10 02:08:40 +00:00
|
|
|
graphData, _ := api.DB.Queries.GetDailyReadStats(api.DB.Ctx, auth.UserName)
|
|
|
|
log.Debug("[appGetHome] GetDailyReadStats Performance: ", time.Since(start))
|
2024-01-01 04:12:46 +00:00
|
|
|
|
|
|
|
start = time.Now()
|
2024-01-10 02:08:40 +00:00
|
|
|
databaseInfo, _ := api.DB.Queries.GetDatabaseInfo(api.DB.Ctx, auth.UserName)
|
|
|
|
log.Debug("[appGetHome] GetDatabaseInfo Performance: ", time.Since(start))
|
2024-01-01 04:12:46 +00:00
|
|
|
|
2024-01-10 02:08:40 +00:00
|
|
|
streaks, _ := api.DB.Queries.GetUserStreaks(api.DB.Ctx, auth.UserName)
|
2024-01-01 04:12:46 +00:00
|
|
|
WPMLeaderboard, _ := api.DB.Queries.GetWPMLeaderboard(api.DB.Ctx)
|
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"Streaks": streaks,
|
|
|
|
"GraphData": graphData,
|
|
|
|
"DatabaseInfo": databaseInfo,
|
|
|
|
"WPMLeaderboard": WPMLeaderboard,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "page/home", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appGetSettings(c *gin.Context) {
|
|
|
|
templateVars := api.getBaseTemplateVars("settings", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
2024-01-01 04:12:46 +00:00
|
|
|
|
2024-01-10 02:08:40 +00:00
|
|
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, auth.UserName)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[appGetSettings] GetUser DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-10 02:08:40 +00:00
|
|
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[appGetSettings] GetDevices DB Error:", err)
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"TimeOffset": *user.TimeOffset,
|
|
|
|
"Devices": devices,
|
|
|
|
}
|
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-10 02:08:40 +00:00
|
|
|
func (api *API) appGetAdmin(c *gin.Context) {
|
|
|
|
templateVars := api.getBaseTemplateVars("admin", c)
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "page/admin", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appGetAdminLogs(c *gin.Context) {
|
|
|
|
// Open Log File
|
|
|
|
logPath := path.Join(api.Config.ConfigPath, "logs/antholume.log")
|
|
|
|
logFile, err := os.Open(logPath)
|
|
|
|
if err != nil {
|
|
|
|
errorPage(c, http.StatusBadRequest, "Missing AnthoLume log file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer logFile.Close()
|
|
|
|
|
|
|
|
// Write Log File
|
|
|
|
c.Stream(func(w io.Writer) bool {
|
|
|
|
_, err = io.Copy(w, logFile)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appPerformAdminAction(c *gin.Context) {
|
|
|
|
templateVars := api.getBaseTemplateVars("admin", c)
|
|
|
|
|
|
|
|
var rAdminAction requestAdminAction
|
|
|
|
if err := c.ShouldBind(&rAdminAction); err != nil {
|
|
|
|
log.Error("[appPerformAdminAction] Invalid Form Bind")
|
|
|
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rAdminAction.Action {
|
|
|
|
case AA_IMPORT:
|
|
|
|
// TODO
|
|
|
|
case AA_METADATA_MATCH:
|
|
|
|
// TODO
|
|
|
|
// 1. Documents xref most recent metadata table?
|
|
|
|
// 2. Select all / deselect?
|
|
|
|
case AA_RESTORE:
|
|
|
|
// TODO
|
|
|
|
// 1. Consume backup ZIP
|
|
|
|
// 2. Move existing to "backup" folder (db, wal, shm, covers, documents)
|
|
|
|
// 3. Extract backup zip
|
|
|
|
// 4. Restart server?
|
|
|
|
case AA_BACKUP:
|
|
|
|
// Get File Paths
|
|
|
|
fileName := fmt.Sprintf("%s.db", api.Config.DBName)
|
|
|
|
dbLocation := path.Join(api.Config.ConfigPath, fileName)
|
|
|
|
|
|
|
|
c.Header("Content-type", "application/octet-stream")
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"AnthoLumeExport_%s.zip\"", time.Now().Format("20060102")))
|
|
|
|
|
|
|
|
// Stream Backup ZIP Archive
|
|
|
|
c.Stream(func(w io.Writer) bool {
|
|
|
|
ar := zip.NewWriter(w)
|
|
|
|
|
|
|
|
exportWalker := func(currentPath string, f fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open File on Disk
|
|
|
|
file, err := os.Open(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// Derive Export Structure
|
|
|
|
fileName := filepath.Base(currentPath)
|
|
|
|
folderName := filepath.Base(filepath.Dir(currentPath))
|
|
|
|
|
|
|
|
// Create File in Export
|
|
|
|
newF, err := ar.Create(path.Join(folderName, fileName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy File in Export
|
|
|
|
_, err = io.Copy(newF, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy Database File
|
|
|
|
dbFile, _ := os.Open(dbLocation)
|
|
|
|
newDbFile, _ := ar.Create(fileName)
|
|
|
|
io.Copy(newDbFile, dbFile)
|
|
|
|
|
|
|
|
// Backup Covers & Documents
|
|
|
|
for _, item := range rAdminAction.BackupTypes {
|
|
|
|
if item == BACKUP_TYPE_COVERS {
|
|
|
|
filepath.WalkDir(path.Join(api.Config.ConfigPath, "covers"), exportWalker)
|
|
|
|
|
|
|
|
} else if item == BACKUP_TYPE_DOCUMENTS {
|
|
|
|
filepath.WalkDir(path.Join(api.Config.ConfigPath, "documents"), exportWalker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ar.Close()
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "page/admin", templateVars)
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) appGetSearch(c *gin.Context) {
|
|
|
|
templateVars := api.getBaseTemplateVars("search", c)
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
var sParams searchParams
|
|
|
|
c.BindQuery(&sParams)
|
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 {
|
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("Search Error: %v", err))
|
|
|
|
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) {
|
|
|
|
templateVars := api.getBaseTemplateVars("login", c)
|
|
|
|
templateVars["RegistrationEnabled"] = api.Config.RegistrationEnabled
|
|
|
|
c.HTML(http.StatusOK, "page/login", templateVars)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) appGetRegister(c *gin.Context) {
|
|
|
|
if !api.Config.RegistrationEnabled {
|
|
|
|
c.Redirect(http.StatusFound, "/login")
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars := api.getBaseTemplateVars("login", c)
|
|
|
|
templateVars["RegistrationEnabled"] = api.Config.RegistrationEnabled
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appGetDocumentProgress] Invalid URI Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
2023-10-10 23:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +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-01-01 04:12:46 +00:00
|
|
|
log.Error("[appGetDocumentProgress] UpsertDocument DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
2023-10-10 23:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appGetDocumentProgress] GetDocumentWithStats DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-10 02:08:40 +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-01 04:12:46 +00:00
|
|
|
log.Error("[appGetDevices] GetDevices DB Error:", err)
|
2023-11-27 02:41:17 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] Invalid Form Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rDocUpload.DocumentFile == nil {
|
|
|
|
c.Redirect(http.StatusFound, "./documents")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Type & Derive Extension on MIME
|
|
|
|
uploadedFile, err := rDocUpload.DocumentFile.Open()
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] File Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] MIME Error")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileExtension := fileMime.Extension()
|
|
|
|
|
|
|
|
// Validate Extension
|
|
|
|
if !slices.Contains([]string{".epub"}, fileExtension) {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] Invalid FileType: ", fileExtension)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Temp File
|
|
|
|
tempFile, err := os.CreateTemp("", "book")
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Warn("[appUploadNewDocument] Temp File Create Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to create temp file.")
|
|
|
|
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()
|
|
|
|
|
|
|
|
// Save Temp
|
|
|
|
err = c.SaveUploadedFile(rDocUpload.DocumentFile, tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] File Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Metadata
|
|
|
|
metadataInfo, err := metadata.GetMetadata(tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Warn("[appUploadNewDocument] GetMetadata Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to acquire file metadata.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate Partial MD5 ID
|
|
|
|
partialMD5, err := utils.CalculatePartialMD5(tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Warn("[appUploadNewDocument] Partial MD5 Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate partial MD5.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Exists
|
|
|
|
_, err = api.DB.Queries.GetDocument(api.DB.Ctx, partialMD5)
|
|
|
|
if err == nil {
|
|
|
|
c.Redirect(http.StatusFound, fmt.Sprintf("./documents/%s", partialMD5))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate Actual MD5
|
|
|
|
fileHash, err := getFileMD5(tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] MD5 Hash Failure:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate MD5.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
// Get Word Count
|
|
|
|
wordCount, err := metadata.GetWordCount(tempFile.Name())
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] Word Count Failure:", err)
|
2023-11-03 23:38:31 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate word count.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-25 23:52:01 +00:00
|
|
|
// Derive Filename
|
|
|
|
var fileName string
|
|
|
|
if *metadataInfo.Author != "" {
|
|
|
|
fileName = fileName + *metadataInfo.Author
|
|
|
|
} else {
|
|
|
|
fileName = fileName + "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
if *metadataInfo.Title != "" {
|
|
|
|
fileName = fileName + " - " + *metadataInfo.Title
|
|
|
|
} else {
|
|
|
|
fileName = fileName + " - Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove Slashes
|
|
|
|
fileName = strings.ReplaceAll(fileName, "/", "")
|
|
|
|
|
|
|
|
// Derive & Sanitize File Name
|
|
|
|
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension))
|
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
// Generate Storage Path & Open File
|
2023-10-25 23:52:01 +00:00
|
|
|
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
2023-10-31 03:35:37 +00:00
|
|
|
destFile, err := os.Create(safePath)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] Dest File Error:", err)
|
2023-10-31 03:35:37 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
|
|
|
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-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] Copy Temp File Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upsert Document
|
|
|
|
if _, err = api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
|
|
|
ID: partialMD5,
|
|
|
|
Title: metadataInfo.Title,
|
|
|
|
Author: metadataInfo.Author,
|
|
|
|
Description: metadataInfo.Description,
|
2023-11-03 23:38:31 +00:00
|
|
|
Words: &wordCount,
|
2023-10-25 23:52:01 +00:00
|
|
|
Md5: fileHash,
|
|
|
|
Filepath: &fileName,
|
|
|
|
}); err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appUploadNewDocument] UpsertDocument DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, fmt.Sprintf("./documents/%s", partialMD5))
|
|
|
|
}
|
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] Invalid URI Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] Invalid Form Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] Missing Form Values")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] File Error")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] MIME Error")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] Invalid FileType: ", fileExtension)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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)
|
|
|
|
safePath := filepath.Join(api.Config.DataPath, "covers", fileName)
|
|
|
|
|
|
|
|
// Save
|
|
|
|
err = c.SaveUploadedFile(rDocEdit.CoverFile, safePath)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] File Error: ", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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 {
|
|
|
|
var coverDir string = filepath.Join(api.Config.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
|
|
|
|
if _, err := api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditDocument] UpsertDocument DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "./")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appDeleteDocument] Invalid URI Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
changed, err := api.DB.Queries.DeleteDocument(api.DB.Ctx, rDocID.DocumentID)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appDeleteDocument] DeleteDocument DB Error")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("DeleteDocument DB Error: %v", err))
|
2023-09-23 18:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if changed == 0 {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appDeleteDocument] DeleteDocument DB Error")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appIdentifyDocument] Invalid URI Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appIdentifyDocument] Invalid Form Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appIdentifyDocument] Invalid Form")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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
|
|
|
|
templateVars := api.getBaseTemplateVars("document", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
2023-09-26 22:09:02 +00:00
|
|
|
|
|
|
|
// Get Metadata
|
2023-10-01 23:17:22 +00:00
|
|
|
metadataResults, err := metadata.SearchMetadata(metadata.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
|
|
|
|
if _, err = api.DB.Queries.AddMetadata(api.DB.Ctx, database.AddMetadataParams{
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appIdentifyDocument] 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-01 04:12:46 +00:00
|
|
|
log.Warn("[appIdentifyDocument] 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
|
|
|
|
|
|
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appIdentifyDocument] GetDocumentWithStats DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] Invalid Form Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(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-01 04:12:46 +00:00
|
|
|
templateVars := api.getBaseTemplateVars("search", c)
|
2023-12-01 12:35:51 +00:00
|
|
|
c.HTML(http.StatusOK, "page/search", templateVars)
|
|
|
|
|
|
|
|
// Create Streamer
|
|
|
|
stream := api.newStreamer(c)
|
|
|
|
defer stream.close()
|
|
|
|
|
|
|
|
// 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-01 04:12:46 +00:00
|
|
|
log.Warn("[appSaveNewDocument] 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-01 04:12:46 +00:00
|
|
|
log.Warn("[appSaveNewDocument] 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)
|
|
|
|
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
|
|
|
|
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, partialMD5, fileExtension))
|
|
|
|
|
2023-10-31 03:35:37 +00:00
|
|
|
// Open Source File
|
|
|
|
sourceFile, err := os.Open(tempFilePath)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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
|
2023-10-07 01:25:56 +00:00
|
|
|
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
2023-10-31 03:35:37 +00:00
|
|
|
destFile, err := os.Create(safePath)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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
|
|
|
|
if _, err = api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
|
|
|
ID: partialMD5,
|
|
|
|
Title: rDocAdd.Title,
|
|
|
|
Author: rDocAdd.Author,
|
|
|
|
Md5: fileHash,
|
|
|
|
Filepath: &fileName,
|
2023-11-03 23:38:31 +00:00
|
|
|
Words: &wordCount,
|
2023-10-07 01:25:56 +00:00
|
|
|
}); err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appSaveNewDocument] 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",
|
|
|
|
"ButtonHref": fmt.Sprintf("./documents/%s", partialMD5),
|
|
|
|
})
|
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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditSettings] Invalid Form Bind")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Something Exists
|
|
|
|
if rUserSettings.Password == nil && rUserSettings.NewPassword == nil && rUserSettings.TimeOffset == nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appEditSettings] Missing Form Values")
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
templateVars := api.getBaseTemplateVars("settings", c)
|
2024-01-10 02:08:40 +00:00
|
|
|
auth := templateVars["Authorization"].(authData)
|
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,
|
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
|
|
|
|
if rUserSettings.TimeOffset != nil {
|
|
|
|
templateVars["TimeOffsetMessage"] = "Time Offset Updated"
|
|
|
|
newUserSettings.TimeOffset = rUserSettings.TimeOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update User
|
|
|
|
_, err := api.DB.Queries.UpdateUser(api.DB.Ctx, newUserSettings)
|
|
|
|
if err != nil {
|
2024-01-01 04:12:46 +00:00
|
|
|
log.Error("[appEditSettings] UpdateUser DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpdateUser DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get User
|
2024-01-10 02:08:40 +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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditSettings] GetUser DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Devices
|
2024-01-10 02:08:40 +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-01 04:12:46 +00:00
|
|
|
log.Error("[appEditSettings] GetDevices DB Error:", err)
|
2023-10-25 23:52:01 +00:00
|
|
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
2023-09-27 22:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
2024-01-01 04:12:46 +00:00
|
|
|
"TimeOffset": *user.TimeOffset,
|
|
|
|
"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) {
|
|
|
|
errorPage(c, http.StatusUnauthorized, "Not Allowed in Demo Mode")
|
|
|
|
}
|
|
|
|
|
2023-10-01 23:17:22 +00:00
|
|
|
func (api *API) getDocumentsWordCount(documents []database.GetDocumentsWithStatsRow) error {
|
|
|
|
// Do Transaction
|
|
|
|
tx, err := api.DB.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[getDocumentsWordCount] Transaction Begin DB Error:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer & Start Transaction
|
|
|
|
defer tx.Rollback()
|
|
|
|
qtx := api.DB.Queries.WithTx(tx)
|
|
|
|
|
|
|
|
for _, item := range documents {
|
|
|
|
if item.Words == nil && item.Filepath != nil {
|
|
|
|
filePath := filepath.Join(api.Config.DataPath, "documents", *item.Filepath)
|
|
|
|
wordCount, err := metadata.GetWordCount(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("[getDocumentsWordCount] Word Count Error - ", err)
|
|
|
|
} else {
|
|
|
|
if _, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
|
|
|
ID: item.ID,
|
|
|
|
Words: &wordCount,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("[getDocumentsWordCount] UpsertDocument DB Error - ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit Transaction
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
log.Error("[getDocumentsWordCount] Transaction Commit DB Error:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (api *API) getBaseTemplateVars(routeName string, c *gin.Context) gin.H {
|
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{
|
|
|
|
"Version": api.Config.Version,
|
|
|
|
"SearchEnabled": api.Config.SearchEnabled,
|
|
|
|
"RegistrationEnabled": api.Config.RegistrationEnabled,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
c.BindQuery(&qParams)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func errorPage(c *gin.Context, errorCode int, errorMessage string) {
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|