2023-09-18 23:57:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-09-27 22:58:47 +00:00
|
|
|
"crypto/md5"
|
2023-09-18 23:57:18 +00:00
|
|
|
"fmt"
|
2023-09-23 18:14:57 +00:00
|
|
|
"mime/multipart"
|
2023-09-18 23:57:18 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"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-09-26 23:14:33 +00:00
|
|
|
type queryParams struct {
|
|
|
|
Page *int64 `form:"page"`
|
|
|
|
Limit *int64 `form:"limit"`
|
|
|
|
Document *string `form:"document"`
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
TimeOffset *string `form:"time_offset"`
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
func baseResourceRoute(template string, args ...map[string]any) func(c *gin.Context) {
|
|
|
|
variables := gin.H{"RouteName": template}
|
|
|
|
if len(args) > 0 {
|
|
|
|
variables = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
rUser, _ := c.Get("AuthorizedUser")
|
|
|
|
variables["User"] = rUser
|
|
|
|
c.HTML(http.StatusOK, template, variables)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
func (api *API) webManifest(c *gin.Context) {
|
|
|
|
c.Header("Content-Type", "application/manifest+json")
|
|
|
|
c.File("./assets/manifest.json")
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
func (api *API) createAppResourcesRoute(routeName string, args ...map[string]any) func(*gin.Context) {
|
|
|
|
// Merge Optional Template Data
|
2023-09-21 00:35:01 +00:00
|
|
|
var templateVarsBase = gin.H{}
|
2023-09-18 23:57:18 +00:00
|
|
|
if len(args) > 0 {
|
2023-09-21 00:35:01 +00:00
|
|
|
templateVarsBase = args[0]
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-21 00:35:01 +00:00
|
|
|
templateVarsBase["RouteName"] = routeName
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
rUser, _ := c.Get("AuthorizedUser")
|
2023-09-21 00:35:01 +00:00
|
|
|
|
|
|
|
// Copy Base & Update
|
|
|
|
templateVars := gin.H{}
|
|
|
|
for k, v := range templateVarsBase {
|
|
|
|
templateVars[k] = v
|
|
|
|
}
|
2023-09-18 23:57:18 +00:00
|
|
|
templateVars["User"] = rUser
|
|
|
|
|
2023-09-21 00:35:01 +00:00
|
|
|
// Potential URL Parameters
|
|
|
|
qParams := bindQueryParams(c)
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
if routeName == "documents" {
|
|
|
|
documents, err := api.DB.Queries.GetDocumentsWithStats(api.DB.Ctx, database.GetDocumentsWithStatsParams{
|
|
|
|
UserID: rUser.(string),
|
|
|
|
Offset: (*qParams.Page - 1) * *qParams.Limit,
|
|
|
|
Limit: *qParams.Limit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-21 00:35:01 +00:00
|
|
|
log.Error("[createAppResourcesRoute] GetDocumentsWithStats DB Error:", err)
|
2023-09-18 23:57:18 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = documents
|
2023-09-23 02:12:36 +00:00
|
|
|
} else if routeName == "document" {
|
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] Invalid URI Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
document, err := api.DB.Queries.GetDocumentWithStats(api.DB.Ctx, database.GetDocumentWithStatsParams{
|
|
|
|
UserID: rUser.(string),
|
|
|
|
DocumentID: rDocID.DocumentID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] GetDocumentWithStats DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["RelBase"] = "../"
|
2023-09-23 02:12:36 +00:00
|
|
|
templateVars["Data"] = document
|
2023-09-21 00:35:01 +00:00
|
|
|
} else if routeName == "activity" {
|
2023-09-23 02:12:36 +00:00
|
|
|
activityFilter := database.GetActivityParams{
|
2023-09-21 00:35:01 +00:00
|
|
|
UserID: rUser.(string),
|
|
|
|
Offset: (*qParams.Page - 1) * *qParams.Limit,
|
|
|
|
Limit: *qParams.Limit,
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if qParams.Document != nil {
|
|
|
|
activityFilter.DocFilter = true
|
|
|
|
activityFilter.DocumentID = *qParams.Document
|
|
|
|
}
|
|
|
|
|
|
|
|
activity, err := api.DB.Queries.GetActivity(api.DB.Ctx, activityFilter)
|
2023-09-21 00:35:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] GetActivity DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = activity
|
2023-09-18 23:57:18 +00:00
|
|
|
} else if routeName == "home" {
|
2023-09-23 02:12:36 +00:00
|
|
|
start_time := time.Now()
|
2023-09-21 00:35:01 +00:00
|
|
|
weekly_streak, err := api.DB.Queries.GetUserWindowStreaks(api.DB.Ctx, database.GetUserWindowStreaksParams{
|
2023-09-18 23:57:18 +00:00
|
|
|
UserID: rUser.(string),
|
|
|
|
Window: "WEEK",
|
|
|
|
})
|
2023-09-21 00:35:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("[createAppResourcesRoute] GetUserWindowStreaks DB Error:", err)
|
|
|
|
}
|
2023-09-26 22:09:02 +00:00
|
|
|
log.Debug("GetUserWindowStreaks - WEEK - ", time.Since(start_time))
|
2023-09-23 02:12:36 +00:00
|
|
|
start_time = time.Now()
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-21 00:35:01 +00:00
|
|
|
daily_streak, err := api.DB.Queries.GetUserWindowStreaks(api.DB.Ctx, database.GetUserWindowStreaksParams{
|
2023-09-18 23:57:18 +00:00
|
|
|
UserID: rUser.(string),
|
|
|
|
Window: "DAY",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-21 00:35:01 +00:00
|
|
|
log.Warn("[createAppResourcesRoute] GetUserWindowStreaks DB Error:", err)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-26 22:09:02 +00:00
|
|
|
log.Debug("GetUserWindowStreaks - DAY - ", time.Since(start_time))
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
start_time = time.Now()
|
2023-09-21 00:35:01 +00:00
|
|
|
database_info, _ := api.DB.Queries.GetDatabaseInfo(api.DB.Ctx, rUser.(string))
|
2023-09-26 22:09:02 +00:00
|
|
|
log.Debug("GetDatabaseInfo - ", time.Since(start_time))
|
2023-09-23 02:12:36 +00:00
|
|
|
|
|
|
|
start_time = time.Now()
|
2023-09-21 00:35:01 +00:00
|
|
|
read_graph_data, _ := api.DB.Queries.GetDailyReadStats(api.DB.Ctx, rUser.(string))
|
2023-09-26 22:09:02 +00:00
|
|
|
log.Debug("GetDailyReadStats - ", time.Since(start_time))
|
2023-09-21 00:35:01 +00:00
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"DailyStreak": daily_streak,
|
|
|
|
"WeeklyStreak": weekly_streak,
|
|
|
|
"DatabaseInfo": database_info,
|
|
|
|
"GraphData": read_graph_data,
|
|
|
|
}
|
2023-09-27 22:58:47 +00:00
|
|
|
} else if routeName == "settings" {
|
|
|
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, rUser.(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] GetUser DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, rUser.(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] GetDevices DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"Settings": gin.H{
|
|
|
|
"TimeOffset": *user.TimeOffset,
|
|
|
|
},
|
|
|
|
"Devices": devices,
|
|
|
|
}
|
2023-09-19 23:29:55 +00:00
|
|
|
} else if routeName == "login" {
|
|
|
|
templateVars["RegistrationEnabled"] = api.Config.RegistrationEnabled
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, routeName, templateVars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) getDocumentCover(c *gin.Context) {
|
|
|
|
var rDoc requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
2023-09-21 00:35:01 +00:00
|
|
|
log.Error("[getDocumentCover] Invalid URI Bind")
|
2023-09-18 23:57:18 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Document Exists in DB
|
|
|
|
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
|
|
|
if err != nil {
|
2023-09-21 00:35:01 +00:00
|
|
|
log.Error("[getDocumentCover] GetDocument DB Error:", err)
|
2023-09-18 23:57:18 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Identified Document
|
2023-09-23 18:14:57 +00:00
|
|
|
if document.Coverfile != nil {
|
|
|
|
if *document.Coverfile == "UNKNOWN" {
|
2023-09-21 00:35:01 +00:00
|
|
|
c.File("./assets/no-cover.jpg")
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive Path
|
2023-09-23 18:14:57 +00:00
|
|
|
safePath := filepath.Join(api.Config.DataPath, "covers", *document.Coverfile)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
// Validate File Exists
|
|
|
|
_, err = os.Stat(safePath)
|
|
|
|
if err != nil {
|
2023-09-23 18:14:57 +00:00
|
|
|
log.Error("[getDocumentCover] File Should But Doesn't Exist:", err)
|
2023-09-21 00:35:01 +00:00
|
|
|
c.File("./assets/no-cover.jpg")
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.File(safePath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
// --- Attempt Metadata ---
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
var coverDir string = filepath.Join(api.Config.DataPath, "covers")
|
|
|
|
var coverFile string = "UNKNOWN"
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
// Identify Documents & Save Covers
|
2023-09-23 18:14:57 +00:00
|
|
|
metadataResults, err := metadata.GetMetadata(metadata.MetadataInfo{
|
2023-09-23 02:12:36 +00:00
|
|
|
Title: document.Title,
|
|
|
|
Author: document.Author,
|
2023-09-23 18:14:57 +00:00
|
|
|
})
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
if err == nil && len(metadataResults) > 0 && metadataResults[0].GBID != nil {
|
|
|
|
firstResult := metadataResults[0]
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
// Save Cover
|
2023-09-26 22:09:02 +00:00
|
|
|
fileName, err := metadata.SaveCover(*firstResult.GBID, coverDir, document.ID, false)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err == nil {
|
2023-09-23 18:14:57 +00:00
|
|
|
coverFile = *fileName
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
// Store First Metadata Result
|
|
|
|
if _, err = api.DB.Queries.AddMetadata(api.DB.Ctx, database.AddMetadataParams{
|
|
|
|
DocumentID: document.ID,
|
|
|
|
Title: firstResult.Title,
|
|
|
|
Author: firstResult.Author,
|
|
|
|
Description: firstResult.Description,
|
|
|
|
Gbid: firstResult.GBID,
|
|
|
|
Olid: firstResult.OLID,
|
|
|
|
Isbn10: firstResult.ISBN10,
|
|
|
|
Isbn13: firstResult.ISBN13,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("[getDocumentCover] AddMetadata DB Error:", err)
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 02:12:36 +00:00
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
// Upsert Document
|
|
|
|
if _, err = api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
2023-09-23 18:14:57 +00:00
|
|
|
ID: document.ID,
|
|
|
|
Coverfile: &coverFile,
|
2023-09-18 23:57:18 +00:00
|
|
|
}); err != nil {
|
2023-09-21 00:35:01 +00:00
|
|
|
log.Warn("[getDocumentCover] UpsertDocument DB Error:", err)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return Unknown Cover
|
2023-09-23 18:14:57 +00:00
|
|
|
if coverFile == "UNKNOWN" {
|
2023-09-21 00:35:01 +00:00
|
|
|
c.File("./assets/no-cover.jpg")
|
2023-09-18 23:57:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
coverFilePath := filepath.Join(coverDir, coverFile)
|
2023-09-23 02:12:36 +00:00
|
|
|
c.File(coverFilePath)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
|
|
|
|
func (api *API) editDocument(c *gin.Context) {
|
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] Invalid URI Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rDocEdit requestDocumentEdit
|
|
|
|
if err := c.ShouldBind(&rDocEdit); err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] Invalid Form Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
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 {
|
2023-09-23 18:14:57 +00:00
|
|
|
log.Error("[createAppResourcesRoute] Missing Form Values")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
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 {
|
|
|
|
log.Error("[createAppResourcesRoute] File Error")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[createAppResourcesRoute] MIME Error")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileExtension := fileMime.Extension()
|
|
|
|
|
|
|
|
// Validate Extension
|
|
|
|
if !slices.Contains([]string{".jpg", ".png"}, fileExtension) {
|
|
|
|
log.Error("[uploadDocumentFile] Invalid FileType: ", fileExtension)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Filetype"})
|
|
|
|
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 {
|
|
|
|
log.Error("[createAppResourcesRoute] File Error")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
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")
|
|
|
|
fileName, err := metadata.SaveCover(*rDocEdit.CoverGBID, coverDir, rDocID.DocumentID, true)
|
|
|
|
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 {
|
|
|
|
log.Error("[createAppResourcesRoute] UpsertDocument DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "./")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) deleteDocument(c *gin.Context) {
|
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
|
|
|
log.Error("[deleteDocument] Invalid URI Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
changed, err := api.DB.Queries.DeleteDocument(api.DB.Ctx, rDocID.DocumentID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[deleteDocument] DeleteDocument DB Error")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if changed == 0 {
|
|
|
|
log.Error("[deleteDocument] DeleteDocument DB Error")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "../")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) identifyDocument(c *gin.Context) {
|
2023-09-26 22:09:02 +00:00
|
|
|
rUser, _ := c.Get("AuthorizedUser")
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
var rDocID requestDocumentID
|
|
|
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
|
|
|
log.Error("[identifyDocument] Invalid URI Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rDocIdentify requestDocumentIdentify
|
|
|
|
if err := c.ShouldBind(&rDocIdentify); err != nil {
|
|
|
|
log.Error("[identifyDocument] Invalid Form Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
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 {
|
|
|
|
log.Error("[identifyDocument] Invalid Form")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
// Template Variables
|
|
|
|
templateVars := gin.H{
|
|
|
|
"RelBase": "../../",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Metadata
|
2023-09-23 18:14:57 +00:00
|
|
|
metadataResults, err := metadata.GetMetadata(metadata.MetadataInfo{
|
|
|
|
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,
|
|
|
|
Gbid: firstResult.GBID,
|
|
|
|
Olid: firstResult.OLID,
|
|
|
|
Isbn10: firstResult.ISBN10,
|
|
|
|
Isbn13: firstResult.ISBN13,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("[identifyDocument] AddMetadata DB Error:", err)
|
|
|
|
}
|
2023-09-23 18:14:57 +00:00
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["Metadata"] = firstResult
|
|
|
|
} else {
|
|
|
|
log.Warn("[identifyDocument] Metadata Error")
|
|
|
|
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{
|
|
|
|
UserID: rUser.(string),
|
|
|
|
DocumentID: rDocID.DocumentID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[identifyDocument] GetDocumentWithStats DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 22:09:02 +00:00
|
|
|
templateVars["Data"] = document
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "document", templateVars)
|
2023-09-23 18:14:57 +00:00
|
|
|
}
|
2023-09-26 23:14:33 +00:00
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
func (api *API) editSettings(c *gin.Context) {
|
|
|
|
rUser, _ := c.Get("AuthorizedUser")
|
|
|
|
|
|
|
|
var rUserSettings requestSettingsEdit
|
|
|
|
if err := c.ShouldBind(&rUserSettings); err != nil {
|
|
|
|
log.Error("[editSettings] Invalid Form Bind")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate Something Exists
|
|
|
|
if rUserSettings.Password == nil && rUserSettings.NewPassword == nil && rUserSettings.TimeOffset == nil {
|
|
|
|
log.Error("[editSettings] Missing Form Values")
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars := gin.H{
|
|
|
|
"User": rUser,
|
|
|
|
}
|
|
|
|
newUserSettings := database.UpdateUserParams{
|
|
|
|
UserID: rUser.(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set New Password
|
|
|
|
if rUserSettings.Password != nil && rUserSettings.NewPassword != nil {
|
|
|
|
password := fmt.Sprintf("%x", md5.Sum([]byte(*rUserSettings.Password)))
|
|
|
|
authorized := api.authorizeCredentials(rUser.(string), password)
|
|
|
|
if authorized == true {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templateVars["PasswordErrorMessage"] = "Invalid Password"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
log.Error("[editSettings] UpdateUser DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get User
|
|
|
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, rUser.(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[editSettings] GetUser DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Devices
|
|
|
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, rUser.(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[editSettings] GetDevices DB Error:", err)
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateVars["Data"] = gin.H{
|
|
|
|
"Settings": gin.H{
|
|
|
|
"TimeOffset": *user.TimeOffset,
|
|
|
|
},
|
|
|
|
"Devices": devices,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(http.StatusOK, "settings", templateVars)
|
|
|
|
}
|
|
|
|
|
2023-09-26 23:14:33 +00:00
|
|
|
func bindQueryParams(c *gin.Context) queryParams {
|
|
|
|
var qParams queryParams
|
|
|
|
c.BindQuery(&qParams)
|
|
|
|
|
|
|
|
if qParams.Limit == nil {
|
|
|
|
var defaultValue int64 = 50
|
|
|
|
qParams.Limit = &defaultValue
|
|
|
|
} else if *qParams.Limit < 0 {
|
|
|
|
var zeroValue int64 = 0
|
|
|
|
qParams.Limit = &zeroValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if qParams.Page == nil || *qParams.Page < 1 {
|
|
|
|
var oneValue int64 = 0
|
|
|
|
qParams.Page = &oneValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return qParams
|
|
|
|
}
|