feat(logging): improve logging & migrate to json logger
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0bbd5986cb
commit
fd8b6bcdc1
28
README.md
28
README.md
@ -64,6 +64,8 @@ The OPDS API endpoint is located at: `http(s)://<SERVER>/api/opds`
|
|||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
|
**NOTE**: If you're accessing your instance over HTTP (not HTTPS), you must set `COOKIE_SECURE=false`, otherwise you will not be able to login.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Make Data Directory
|
# Make Data Directory
|
||||||
mkdir -p antholume_data
|
mkdir -p antholume_data
|
||||||
@ -71,6 +73,7 @@ mkdir -p antholume_data
|
|||||||
# Run Server
|
# Run Server
|
||||||
docker run \
|
docker run \
|
||||||
-p 8585:8585 \
|
-p 8585:8585 \
|
||||||
|
-e COOKIE_SECURE=false \
|
||||||
-e REGISTRATION_ENABLED=true \
|
-e REGISTRATION_ENABLED=true \
|
||||||
-v ./antholume_data:/config \
|
-v ./antholume_data:/config \
|
||||||
-v ./antholume_data:/data \
|
-v ./antholume_data:/data \
|
||||||
@ -81,18 +84,19 @@ The service is now accessible at: `http://localhost:8585`. I recommend registeri
|
|||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
| Environment Variable | Default Value | Description |
|
| Environment Variable | Default Value | Description |
|
||||||
| -------------------- | ------------- | ------------------------------------------------------------------- |
|
| -------------------- | ------------- | -------------------------------------------------------------------------- |
|
||||||
| DATABASE_TYPE | SQLite | Currently only "SQLite" is supported |
|
| DATABASE_TYPE | SQLite | Currently only "SQLite" is supported |
|
||||||
| DATABASE_NAME | antholume | The database name, or in SQLite's case, the filename |
|
| DATABASE_NAME | antholume | The database name, or in SQLite's case, the filename |
|
||||||
| CONFIG_PATH | /config | Directory where to store SQLite's DB |
|
| CONFIG_PATH | /config | Directory where to store SQLite's DB |
|
||||||
| DATA_PATH | /data | Directory where to store the documents and cover metadata |
|
| DATA_PATH | /data | Directory where to store the documents and cover metadata |
|
||||||
| LISTEN_PORT | 8585 | Port the server listens at |
|
| LISTEN_PORT | 8585 | Port the server listens at |
|
||||||
| LOG_LEVEL | info | Set server log level |
|
| LOG_LEVEL | info | Set server log level |
|
||||||
| REGISTRATION_ENABLED | false | Whether to allow registration (applies to both WebApp & KOSync API) |
|
| REGISTRATION_ENABLED | false | Whether to allow registration (applies to both WebApp & KOSync API) |
|
||||||
| COOKIE_SESSION_KEY | <EMPTY> | Optional secret cookie session key (auto generated if not provided) |
|
| COOKIE_AUTH_KEY | <EMPTY> | Optional secret cookie authentication key (auto generated if not provided) |
|
||||||
| COOKIE_SECURE | true | Set Cookie `Secure` attribute (i.e. only works over HTTPS) |
|
| COOKIE_ENC_KEY | <EMPTY> | Optional secret cookie encryption key (16 or 32 bytes) |
|
||||||
| COOKIE_HTTP_ONLY | true | Set Cookie `HttpOnly` attribute (i.e. inacessible via JavaScript) |
|
| COOKIE_SECURE | true | Set Cookie `Secure` attribute (i.e. only works over HTTPS) |
|
||||||
|
| COOKIE_HTTP_ONLY | true | Set Cookie `HttpOnly` attribute (i.e. inacessible via JavaScript) |
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
|
50
api/api.go
50
api/api.go
@ -46,23 +46,32 @@ func NewApi(db *database.DBManager, c *config.Config, assets *embed.FS) *API {
|
|||||||
assetsDir, _ := fs.Sub(assets, "assets")
|
assetsDir, _ := fs.Sub(assets, "assets")
|
||||||
api.Router.StaticFS("/assets", http.FS(assetsDir))
|
api.Router.StaticFS("/assets", http.FS(assetsDir))
|
||||||
|
|
||||||
// Generate Secure Token
|
// Generate Auth Token
|
||||||
var newToken []byte
|
var newToken []byte
|
||||||
var err error
|
var err error
|
||||||
|
if c.CookieAuthKey != "" {
|
||||||
if c.CookieSessionKey != "" {
|
log.Info("Utilizing environment cookie auth key")
|
||||||
log.Info("[NewApi] Utilizing Environment Cookie Session Key")
|
newToken = []byte(c.CookieAuthKey)
|
||||||
newToken = []byte(c.CookieSessionKey)
|
|
||||||
} else {
|
} else {
|
||||||
log.Info("[NewApi] Generating Cookie Session Key")
|
log.Info("Generating cookie auth key")
|
||||||
newToken, err = generateToken(64)
|
newToken, err = generateToken(64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Unable to generate secure token")
|
log.Panic("Unable to generate cookie auth key")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Enc Token
|
||||||
|
store := cookie.NewStore(newToken)
|
||||||
|
if c.CookieEncKey != "" {
|
||||||
|
if len(c.CookieEncKey) == 16 || len(c.CookieEncKey) == 32 {
|
||||||
|
log.Info("Utilizing environment cookie encryption key")
|
||||||
|
store = cookie.NewStore(newToken, []byte(c.CookieEncKey))
|
||||||
|
} else {
|
||||||
|
log.Panic("Invalid cookie encryption key (must be 16 or 32 bytes)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure Cookie Session Store
|
// Configure Cookie Session Store
|
||||||
store := cookie.NewStore(newToken)
|
|
||||||
store.Options(sessions.Options{
|
store.Options(sessions.Options{
|
||||||
MaxAge: 60 * 60 * 24 * 7,
|
MaxAge: 60 * 60 * 24 * 7,
|
||||||
Secure: c.CookieSecure,
|
Secure: c.CookieSecure,
|
||||||
@ -251,26 +260,29 @@ func apiLogger() gin.HandlerFunc {
|
|||||||
endTime := time.Now()
|
endTime := time.Now()
|
||||||
latency := endTime.Sub(startTime).Round(time.Microsecond)
|
latency := endTime.Sub(startTime).Round(time.Microsecond)
|
||||||
|
|
||||||
|
// Log Data
|
||||||
|
logData := log.Fields{
|
||||||
|
"type": "access",
|
||||||
|
"ip": c.ClientIP(),
|
||||||
|
"latency": fmt.Sprintf("%s", latency),
|
||||||
|
"status": c.Writer.Status(),
|
||||||
|
"method": c.Request.Method,
|
||||||
|
"path": c.Request.URL.Path,
|
||||||
|
}
|
||||||
|
|
||||||
// Get Username
|
// Get Username
|
||||||
var auth authData
|
var auth authData
|
||||||
if data, _ := c.Get("Authorization"); data != nil {
|
if data, _ := c.Get("Authorization"); data != nil {
|
||||||
auth = data.(authData)
|
auth = data.(authData)
|
||||||
}
|
}
|
||||||
|
|
||||||
username := auth.UserName
|
// Log User
|
||||||
if username != "" {
|
if auth.UserName != "" {
|
||||||
username = " (" + username + ")"
|
logData["user"] = auth.UserName
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Result
|
// Log Result
|
||||||
log.Infof("[HTTPRouter] %-15s (%10s) %d %7s %s%s",
|
log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path))
|
||||||
c.ClientIP(),
|
|
||||||
latency,
|
|
||||||
c.Writer.Status(),
|
|
||||||
c.Request.Method,
|
|
||||||
c.Request.URL.Path,
|
|
||||||
username,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
@ -152,20 +153,20 @@ func (api *API) appGetDocuments(c *gin.Context) {
|
|||||||
Limit: *qParams.Limit,
|
Limit: *qParams.Limit,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetDocuments] GetDocumentsWithStats DB Error: ", err)
|
log.Error("GetDocumentsWithStats DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
length, err := api.DB.Queries.GetDocumentsSize(api.DB.Ctx, query)
|
length, err := api.DB.Queries.GetDocumentsSize(api.DB.Ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetDocuments] GetDocumentsSize DB Error: ", err)
|
log.Error("GetDocumentsSize DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsSize DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsSize DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = api.getDocumentsWordCount(documents); err != nil {
|
if err = api.getDocumentsWordCount(documents); err != nil {
|
||||||
log.Error("[appGetDocuments] Unable to Get Word Counts: ", err)
|
log.Error("Unable to Get Word Counts: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
totalPages := int64(math.Ceil(float64(length) / float64(*qParams.Limit)))
|
totalPages := int64(math.Ceil(float64(length) / float64(*qParams.Limit)))
|
||||||
@ -191,7 +192,7 @@ func (api *API) appGetDocument(c *gin.Context) {
|
|||||||
|
|
||||||
var rDocID requestDocumentID
|
var rDocID requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDocID); err != nil {
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
||||||
log.Error("[appGetDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -201,7 +202,7 @@ func (api *API) appGetDocument(c *gin.Context) {
|
|||||||
DocumentID: rDocID.DocumentID,
|
DocumentID: rDocID.DocumentID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetDocument] GetDocumentWithStats DB Error: ", err)
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsWithStats DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -230,7 +231,7 @@ func (api *API) appGetProgress(c *gin.Context) {
|
|||||||
|
|
||||||
progress, err := api.DB.Queries.GetProgress(api.DB.Ctx, progressFilter)
|
progress, err := api.DB.Queries.GetProgress(api.DB.Ctx, progressFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetProgress] GetProgress DB Error: ", err)
|
log.Error("GetProgress DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -257,7 +258,7 @@ func (api *API) appGetActivity(c *gin.Context) {
|
|||||||
|
|
||||||
activity, err := api.DB.Queries.GetActivity(api.DB.Ctx, activityFilter)
|
activity, err := api.DB.Queries.GetActivity(api.DB.Ctx, activityFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetActivity] GetActivity DB Error: ", err)
|
log.Error("GetActivity DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetActivity DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -273,38 +274,38 @@ func (api *API) appGetHome(c *gin.Context) {
|
|||||||
start := time.Now()
|
start := time.Now()
|
||||||
graphData, err := api.DB.Queries.GetDailyReadStats(api.DB.Ctx, auth.UserName)
|
graphData, err := api.DB.Queries.GetDailyReadStats(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetHome] GetDailyReadStats DB Error: ", err)
|
log.Error("GetDailyReadStats DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDailyReadStats DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDailyReadStats DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("[appGetHome] GetDailyReadStats DB Performance: ", time.Since(start))
|
log.Debug("GetDailyReadStats DB Performance: ", time.Since(start))
|
||||||
|
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
databaseInfo, err := api.DB.Queries.GetDatabaseInfo(api.DB.Ctx, auth.UserName)
|
databaseInfo, err := api.DB.Queries.GetDatabaseInfo(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetHome] GetDatabaseInfo DB Error: ", err)
|
log.Error("GetDatabaseInfo DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDatabaseInfo DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDatabaseInfo DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("[appGetHome] GetDatabaseInfo DB Performance: ", time.Since(start))
|
log.Debug("GetDatabaseInfo DB Performance: ", time.Since(start))
|
||||||
|
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
streaks, err := api.DB.Queries.GetUserStreaks(api.DB.Ctx, auth.UserName)
|
streaks, err := api.DB.Queries.GetUserStreaks(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetHome] GetUserStreaks DB Error: ", err)
|
log.Error("GetUserStreaks DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStreaks DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStreaks DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("[appGetHome] GetUserStreaks DB Performance: ", time.Since(start))
|
log.Debug("GetUserStreaks DB Performance: ", time.Since(start))
|
||||||
|
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
userStatistics, err := api.DB.Queries.GetUserStatistics(api.DB.Ctx)
|
userStatistics, err := api.DB.Queries.GetUserStatistics(api.DB.Ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetHome] GetUserStatistics DB Error: ", err)
|
log.Error("GetUserStatistics DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStatistics DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUserStatistics DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debug("[appGetHome] GetUserStatistics DB Performance: ", time.Since(start))
|
log.Debug("GetUserStatistics DB Performance: ", time.Since(start))
|
||||||
|
|
||||||
templateVars["Data"] = gin.H{
|
templateVars["Data"] = gin.H{
|
||||||
"Streaks": streaks,
|
"Streaks": streaks,
|
||||||
@ -321,14 +322,14 @@ func (api *API) appGetSettings(c *gin.Context) {
|
|||||||
|
|
||||||
user, err := api.DB.Queries.GetUser(api.DB.Ctx, auth.UserName)
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetSettings] GetUser DB Error: ", err)
|
log.Error("GetUser DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetSettings] GetDevices DB Error: ", err)
|
log.Error("GetDevices DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -362,7 +363,23 @@ func (api *API) appGetAdminLogs(c *gin.Context) {
|
|||||||
var logLines []string
|
var logLines []string
|
||||||
scanner := bufio.NewScanner(logFile)
|
scanner := bufio.NewScanner(logFile)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
logLines = append(logLines, scanner.Text())
|
rawLog := scanner.Text()
|
||||||
|
|
||||||
|
// Attempt JSON Pretty
|
||||||
|
var jsonMap map[string]interface{}
|
||||||
|
err := json.Unmarshal([]byte(rawLog), &jsonMap)
|
||||||
|
if err != nil {
|
||||||
|
logLines = append(logLines, scanner.Text())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
prettyJSON, err := json.MarshalIndent(jsonMap, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
logLines = append(logLines, scanner.Text())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
logLines = append(logLines, string(prettyJSON))
|
||||||
}
|
}
|
||||||
templateVars["Data"] = logLines
|
templateVars["Data"] = logLines
|
||||||
|
|
||||||
@ -374,7 +391,7 @@ func (api *API) appGetAdminUsers(c *gin.Context) {
|
|||||||
|
|
||||||
users, err := api.DB.Queries.GetUsers(api.DB.Ctx)
|
users, err := api.DB.Queries.GetUsers(api.DB.Ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetAdminUsers] GetUsers DB Error: ", err)
|
log.Error("GetUsers DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUsers DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUsers DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -393,7 +410,7 @@ func (api *API) appPerformAdminAction(c *gin.Context) {
|
|||||||
|
|
||||||
var rAdminAction requestAdminAction
|
var rAdminAction requestAdminAction
|
||||||
if err := c.ShouldBind(&rAdminAction); err != nil {
|
if err := c.ShouldBind(&rAdminAction); err != nil {
|
||||||
log.Error("[appPerformAdminAction] Invalid Form Bind")
|
log.Error("Invalid Form Bind: ", err)
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -401,81 +418,33 @@ func (api *API) appPerformAdminAction(c *gin.Context) {
|
|||||||
switch rAdminAction.Action {
|
switch rAdminAction.Action {
|
||||||
case adminImport:
|
case adminImport:
|
||||||
// TODO
|
// TODO
|
||||||
case adminCacheTables:
|
|
||||||
go api.DB.CacheTempTables()
|
|
||||||
case adminMetadataMatch:
|
case adminMetadataMatch:
|
||||||
// TODO
|
// TODO
|
||||||
// 1. Documents xref most recent metadata table?
|
// 1. Documents xref most recent metadata table?
|
||||||
// 2. Select all / deselect?
|
// 2. Select all / deselect?
|
||||||
|
case adminCacheTables:
|
||||||
|
go api.DB.CacheTempTables()
|
||||||
case adminRestore:
|
case adminRestore:
|
||||||
// TODO
|
api.processRestoreFile(rAdminAction, c)
|
||||||
// 1. Consume backup ZIP
|
|
||||||
// 2. Move existing to "backup" folder (db, wal, shm, covers, documents)
|
|
||||||
// 3. Extract backup zip
|
|
||||||
// 4. Invalidate cookies (see in auth.go logout)
|
|
||||||
// 5. Restart server?
|
|
||||||
case adminBackup:
|
case adminBackup:
|
||||||
// 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-type", "application/octet-stream")
|
||||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"AnthoLumeExport_%s.zip\"", time.Now().Format("20060102")))
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"AnthoLumeBackup_%s.zip\"", time.Now().Format("20060102")))
|
||||||
|
|
||||||
// Stream Backup ZIP Archive
|
// Stream Backup ZIP Archive
|
||||||
c.Stream(func(w io.Writer) bool {
|
c.Stream(func(w io.Writer) bool {
|
||||||
ar := zip.NewWriter(w)
|
var directories []string
|
||||||
|
|
||||||
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 {
|
for _, item := range rAdminAction.BackupTypes {
|
||||||
if item == backupCovers {
|
if item == backupCovers {
|
||||||
filepath.WalkDir(path.Join(api.Config.DataPath, "covers"), exportWalker)
|
directories = append(directories, "covers")
|
||||||
|
|
||||||
} else if item == backupDocuments {
|
} else if item == backupDocuments {
|
||||||
filepath.WalkDir(path.Join(api.Config.DataPath, "documents"), exportWalker)
|
directories = append(directories, "documents")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ar.Close()
|
err := api.createBackup(w, directories)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Backup Error: ", err)
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -535,7 +504,7 @@ func (api *API) appGetDocumentProgress(c *gin.Context) {
|
|||||||
|
|
||||||
var rDoc requestDocumentID
|
var rDoc requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDoc); err != nil {
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
||||||
log.Error("[appGetDocumentProgress] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -546,7 +515,7 @@ func (api *API) appGetDocumentProgress(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
log.Error("[appGetDocumentProgress] UpsertDocument DB Error: ", err)
|
log.Error("UpsertDocument DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -556,7 +525,7 @@ func (api *API) appGetDocumentProgress(c *gin.Context) {
|
|||||||
DocumentID: rDoc.DocumentID,
|
DocumentID: rDoc.DocumentID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appGetDocumentProgress] GetDocumentWithStats DB Error: ", err)
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -580,7 +549,7 @@ func (api *API) appGetDevices(c *gin.Context) {
|
|||||||
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
||||||
|
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
log.Error("[appGetDevices] GetDevices DB Error: ", err)
|
log.Error("GetDevices DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -591,7 +560,7 @@ func (api *API) appGetDevices(c *gin.Context) {
|
|||||||
func (api *API) appUploadNewDocument(c *gin.Context) {
|
func (api *API) appUploadNewDocument(c *gin.Context) {
|
||||||
var rDocUpload requestDocumentUpload
|
var rDocUpload requestDocumentUpload
|
||||||
if err := c.ShouldBind(&rDocUpload); err != nil {
|
if err := c.ShouldBind(&rDocUpload); err != nil {
|
||||||
log.Error("[appUploadNewDocument] Invalid Form Bind")
|
log.Error("Invalid Form Bind")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -604,14 +573,14 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Validate Type & Derive Extension on MIME
|
// Validate Type & Derive Extension on MIME
|
||||||
uploadedFile, err := rDocUpload.DocumentFile.Open()
|
uploadedFile, err := rDocUpload.DocumentFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] File Error: ", err)
|
log.Error("File Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileMime, err := mimetype.DetectReader(uploadedFile)
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] MIME Error")
|
log.Error("MIME Error")
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -619,7 +588,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
|
|
||||||
// Validate Extension
|
// Validate Extension
|
||||||
if !slices.Contains([]string{".epub"}, fileExtension) {
|
if !slices.Contains([]string{".epub"}, fileExtension) {
|
||||||
log.Error("[appUploadNewDocument] Invalid FileType: ", fileExtension)
|
log.Error("Invalid FileType: ", fileExtension)
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -627,7 +596,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Create Temp File
|
// Create Temp File
|
||||||
tempFile, err := os.CreateTemp("", "book")
|
tempFile, err := os.CreateTemp("", "book")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[appUploadNewDocument] Temp File Create Error: ", err)
|
log.Warn("Temp File Create Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to create temp file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to create temp file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -637,7 +606,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Save Temp
|
// Save Temp
|
||||||
err = c.SaveUploadedFile(rDocUpload.DocumentFile, tempFile.Name())
|
err = c.SaveUploadedFile(rDocUpload.DocumentFile, tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] File Error: ", err)
|
log.Error("File Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -645,7 +614,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Get Metadata
|
// Get Metadata
|
||||||
metadataInfo, err := metadata.GetMetadata(tempFile.Name())
|
metadataInfo, err := metadata.GetMetadata(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[appUploadNewDocument] GetMetadata Error: ", err)
|
log.Warn("GetMetadata Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to acquire file metadata.")
|
errorPage(c, http.StatusInternalServerError, "Unable to acquire file metadata.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -653,7 +622,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Calculate Partial MD5 ID
|
// Calculate Partial MD5 ID
|
||||||
partialMD5, err := utils.CalculatePartialMD5(tempFile.Name())
|
partialMD5, err := utils.CalculatePartialMD5(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[appUploadNewDocument] Partial MD5 Error: ", err)
|
log.Warn("Partial MD5 Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to calculate partial MD5.")
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate partial MD5.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -668,7 +637,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Calculate Actual MD5
|
// Calculate Actual MD5
|
||||||
fileHash, err := getFileMD5(tempFile.Name())
|
fileHash, err := getFileMD5(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] MD5 Hash Failure: ", err)
|
log.Error("MD5 Hash Failure: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to calculate MD5.")
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate MD5.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -676,7 +645,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
// Get Word Count
|
// Get Word Count
|
||||||
wordCount, err := metadata.GetWordCount(tempFile.Name())
|
wordCount, err := metadata.GetWordCount(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] Word Count Failure: ", err)
|
log.Error("Word Count Failure: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to calculate word count.")
|
errorPage(c, http.StatusInternalServerError, "Unable to calculate word count.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -705,7 +674,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
||||||
destFile, err := os.Create(safePath)
|
destFile, err := os.Create(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appUploadNewDocument] Dest File Error: ", err)
|
log.Error("Dest File Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -713,7 +682,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
|
|
||||||
// Copy File
|
// Copy File
|
||||||
if _, err = io.Copy(destFile, tempFile); err != nil {
|
if _, err = io.Copy(destFile, tempFile); err != nil {
|
||||||
log.Error("[appUploadNewDocument] Copy Temp File Error: ", err)
|
log.Error("Copy Temp File Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -728,7 +697,7 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
Md5: fileHash,
|
Md5: fileHash,
|
||||||
Filepath: &fileName,
|
Filepath: &fileName,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[appUploadNewDocument] UpsertDocument DB Error: ", err)
|
log.Error("UpsertDocument DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -739,14 +708,14 @@ func (api *API) appUploadNewDocument(c *gin.Context) {
|
|||||||
func (api *API) appEditDocument(c *gin.Context) {
|
func (api *API) appEditDocument(c *gin.Context) {
|
||||||
var rDocID requestDocumentID
|
var rDocID requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDocID); err != nil {
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
||||||
log.Error("[appEditDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var rDocEdit requestDocumentEdit
|
var rDocEdit requestDocumentEdit
|
||||||
if err := c.ShouldBind(&rDocEdit); err != nil {
|
if err := c.ShouldBind(&rDocEdit); err != nil {
|
||||||
log.Error("[appEditDocument] Invalid Form Bind")
|
log.Error("Invalid Form Bind")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -760,7 +729,7 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
rDocEdit.RemoveCover == nil &&
|
rDocEdit.RemoveCover == nil &&
|
||||||
rDocEdit.CoverGBID == nil &&
|
rDocEdit.CoverGBID == nil &&
|
||||||
rDocEdit.CoverFile == nil {
|
rDocEdit.CoverFile == nil {
|
||||||
log.Error("[appEditDocument] Missing Form Values")
|
log.Error("Missing Form Values")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -774,14 +743,14 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
// Validate Type & Derive Extension on MIME
|
// Validate Type & Derive Extension on MIME
|
||||||
uploadedFile, err := rDocEdit.CoverFile.Open()
|
uploadedFile, err := rDocEdit.CoverFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditDocument] File Error")
|
log.Error("File Error")
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileMime, err := mimetype.DetectReader(uploadedFile)
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditDocument] MIME Error")
|
log.Error("MIME Error")
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -789,7 +758,7 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
|
|
||||||
// Validate Extension
|
// Validate Extension
|
||||||
if !slices.Contains([]string{".jpg", ".png"}, fileExtension) {
|
if !slices.Contains([]string{".jpg", ".png"}, fileExtension) {
|
||||||
log.Error("[appEditDocument] Invalid FileType: ", fileExtension)
|
log.Error("Invalid FileType: ", fileExtension)
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -801,7 +770,7 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
// Save
|
// Save
|
||||||
err = c.SaveUploadedFile(rDocEdit.CoverFile, safePath)
|
err = c.SaveUploadedFile(rDocEdit.CoverFile, safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditDocument] File Error: ", err)
|
log.Error("File Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -825,7 +794,7 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
Isbn13: api.sanitizeInput(rDocEdit.ISBN13),
|
Isbn13: api.sanitizeInput(rDocEdit.ISBN13),
|
||||||
Coverfile: coverFileName,
|
Coverfile: coverFileName,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[appEditDocument] UpsertDocument DB Error: ", err)
|
log.Error("UpsertDocument DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -837,18 +806,18 @@ func (api *API) appEditDocument(c *gin.Context) {
|
|||||||
func (api *API) appDeleteDocument(c *gin.Context) {
|
func (api *API) appDeleteDocument(c *gin.Context) {
|
||||||
var rDocID requestDocumentID
|
var rDocID requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDocID); err != nil {
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
||||||
log.Error("[appDeleteDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
changed, err := api.DB.Queries.DeleteDocument(api.DB.Ctx, rDocID.DocumentID)
|
changed, err := api.DB.Queries.DeleteDocument(api.DB.Ctx, rDocID.DocumentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appDeleteDocument] DeleteDocument DB Error")
|
log.Error("DeleteDocument DB Error")
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("DeleteDocument DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("DeleteDocument DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if changed == 0 {
|
if changed == 0 {
|
||||||
log.Error("[appDeleteDocument] DeleteDocument DB Error")
|
log.Error("DeleteDocument DB Error")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -859,14 +828,14 @@ func (api *API) appDeleteDocument(c *gin.Context) {
|
|||||||
func (api *API) appIdentifyDocument(c *gin.Context) {
|
func (api *API) appIdentifyDocument(c *gin.Context) {
|
||||||
var rDocID requestDocumentID
|
var rDocID requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDocID); err != nil {
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
||||||
log.Error("[appIdentifyDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid document.")
|
errorPage(c, http.StatusNotFound, "Invalid document.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var rDocIdentify requestDocumentIdentify
|
var rDocIdentify requestDocumentIdentify
|
||||||
if err := c.ShouldBind(&rDocIdentify); err != nil {
|
if err := c.ShouldBind(&rDocIdentify); err != nil {
|
||||||
log.Error("[appIdentifyDocument] Invalid Form Bind")
|
log.Error("Invalid Form Bind")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -884,7 +853,7 @@ func (api *API) appIdentifyDocument(c *gin.Context) {
|
|||||||
|
|
||||||
// Validate Values
|
// Validate Values
|
||||||
if rDocIdentify.ISBN == nil && rDocIdentify.Title == nil && rDocIdentify.Author == nil {
|
if rDocIdentify.ISBN == nil && rDocIdentify.Title == nil && rDocIdentify.Author == nil {
|
||||||
log.Error("[appIdentifyDocument] Invalid Form")
|
log.Error("Invalid Form")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -913,12 +882,12 @@ func (api *API) appIdentifyDocument(c *gin.Context) {
|
|||||||
Isbn10: firstResult.ISBN10,
|
Isbn10: firstResult.ISBN10,
|
||||||
Isbn13: firstResult.ISBN13,
|
Isbn13: firstResult.ISBN13,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[appIdentifyDocument] AddMetadata DB Error: ", err)
|
log.Error("AddMetadata DB Error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
templateVars["Metadata"] = firstResult
|
templateVars["Metadata"] = firstResult
|
||||||
} else {
|
} else {
|
||||||
log.Warn("[appIdentifyDocument] Metadata Error")
|
log.Warn("Metadata Error")
|
||||||
templateVars["MetadataError"] = "No Metadata Found"
|
templateVars["MetadataError"] = "No Metadata Found"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -927,7 +896,7 @@ func (api *API) appIdentifyDocument(c *gin.Context) {
|
|||||||
DocumentID: rDocID.DocumentID,
|
DocumentID: rDocID.DocumentID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appIdentifyDocument] GetDocumentWithStats DB Error: ", err)
|
log.Error("GetDocumentWithStats DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -941,7 +910,7 @@ func (api *API) appIdentifyDocument(c *gin.Context) {
|
|||||||
func (api *API) appSaveNewDocument(c *gin.Context) {
|
func (api *API) appSaveNewDocument(c *gin.Context) {
|
||||||
var rDocAdd requestDocumentAdd
|
var rDocAdd requestDocumentAdd
|
||||||
if err := c.ShouldBind(&rDocAdd); err != nil {
|
if err := c.ShouldBind(&rDocAdd); err != nil {
|
||||||
log.Error("[appSaveNewDocument] Invalid Form Bind")
|
log.Error("Invalid Form Bind")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -980,7 +949,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
// Save Book
|
// Save Book
|
||||||
tempFilePath, err := search.SaveBook(rDocAdd.ID, rDocAdd.Source)
|
tempFilePath, err := search.SaveBook(rDocAdd.ID, rDocAdd.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[appSaveNewDocument] Temp File Error: ", err)
|
log.Warn("Temp File Error: ", err)
|
||||||
sendDownloadMessage("Unable to download file", gin.H{"Error": true})
|
sendDownloadMessage("Unable to download file", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -991,7 +960,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
// Calculate Partial MD5 ID
|
// Calculate Partial MD5 ID
|
||||||
partialMD5, err := utils.CalculatePartialMD5(tempFilePath)
|
partialMD5, err := utils.CalculatePartialMD5(tempFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[appSaveNewDocument] Partial MD5 Error: ", err)
|
log.Warn("Partial MD5 Error: ", err)
|
||||||
sendDownloadMessage("Unable to calculate partial MD5", gin.H{"Error": true})
|
sendDownloadMessage("Unable to calculate partial MD5", gin.H{"Error": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1025,7 +994,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
// Open Source File
|
// Open Source File
|
||||||
sourceFile, err := os.Open(tempFilePath)
|
sourceFile, err := os.Open(tempFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appSaveNewDocument] Source File Error: ", err)
|
log.Error("Source File Error: ", err)
|
||||||
sendDownloadMessage("Unable to open file", gin.H{"Error": true})
|
sendDownloadMessage("Unable to open file", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1036,7 +1005,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
|
||||||
destFile, err := os.Create(safePath)
|
destFile, err := os.Create(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appSaveNewDocument] Dest File Error: ", err)
|
log.Error("Dest File Error: ", err)
|
||||||
sendDownloadMessage("Unable to create file", gin.H{"Error": true})
|
sendDownloadMessage("Unable to create file", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1044,7 +1013,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
|
|
||||||
// Copy File
|
// Copy File
|
||||||
if _, err = io.Copy(destFile, sourceFile); err != nil {
|
if _, err = io.Copy(destFile, sourceFile); err != nil {
|
||||||
log.Error("[appSaveNewDocument] Copy Temp File Error: ", err)
|
log.Error("Copy Temp File Error: ", err)
|
||||||
sendDownloadMessage("Unable to save file", gin.H{"Error": true})
|
sendDownloadMessage("Unable to save file", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1055,7 +1024,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
// Get MD5 Hash
|
// Get MD5 Hash
|
||||||
fileHash, err := getFileMD5(safePath)
|
fileHash, err := getFileMD5(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appSaveNewDocument] Hash Failure: ", err)
|
log.Error("Hash Failure: ", err)
|
||||||
sendDownloadMessage("Unable to calculate MD5", gin.H{"Error": true})
|
sendDownloadMessage("Unable to calculate MD5", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1066,7 +1035,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
// Get Word Count
|
// Get Word Count
|
||||||
wordCount, err := metadata.GetWordCount(safePath)
|
wordCount, err := metadata.GetWordCount(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appSaveNewDocument] Word Count Failure: ", err)
|
log.Error("Word Count Failure: ", err)
|
||||||
sendDownloadMessage("Unable to calculate word count", gin.H{"Error": true})
|
sendDownloadMessage("Unable to calculate word count", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1083,7 +1052,7 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
Filepath: &fileName,
|
Filepath: &fileName,
|
||||||
Words: &wordCount,
|
Words: &wordCount,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[appSaveNewDocument] UpsertDocument DB Error: ", err)
|
log.Error("UpsertDocument DB Error: ", err)
|
||||||
sendDownloadMessage("Unable to save to database", gin.H{"Error": true})
|
sendDownloadMessage("Unable to save to database", gin.H{"Error": true})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1099,14 +1068,14 @@ func (api *API) appSaveNewDocument(c *gin.Context) {
|
|||||||
func (api *API) appEditSettings(c *gin.Context) {
|
func (api *API) appEditSettings(c *gin.Context) {
|
||||||
var rUserSettings requestSettingsEdit
|
var rUserSettings requestSettingsEdit
|
||||||
if err := c.ShouldBind(&rUserSettings); err != nil {
|
if err := c.ShouldBind(&rUserSettings); err != nil {
|
||||||
log.Error("[appEditSettings] Invalid Form Bind")
|
log.Error("Invalid Form Bind")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate Something Exists
|
// Validate Something Exists
|
||||||
if rUserSettings.Password == nil && rUserSettings.NewPassword == nil && rUserSettings.TimeOffset == nil {
|
if rUserSettings.Password == nil && rUserSettings.NewPassword == nil && rUserSettings.TimeOffset == nil {
|
||||||
log.Error("[appEditSettings] Missing Form Values")
|
log.Error("Missing Form Values")
|
||||||
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
errorPage(c, http.StatusBadRequest, "Invalid or missing form values.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1144,7 +1113,7 @@ func (api *API) appEditSettings(c *gin.Context) {
|
|||||||
// Update User
|
// Update User
|
||||||
_, err := api.DB.Queries.UpdateUser(api.DB.Ctx, newUserSettings)
|
_, err := api.DB.Queries.UpdateUser(api.DB.Ctx, newUserSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditSettings] UpdateUser DB Error: ", err)
|
log.Error("UpdateUser DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpdateUser DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpdateUser DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1152,7 +1121,7 @@ func (api *API) appEditSettings(c *gin.Context) {
|
|||||||
// Get User
|
// Get User
|
||||||
user, err := api.DB.Queries.GetUser(api.DB.Ctx, auth.UserName)
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditSettings] GetUser DB Error: ", err)
|
log.Error("GetUser DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetUser DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1160,7 +1129,7 @@ func (api *API) appEditSettings(c *gin.Context) {
|
|||||||
// Get Devices
|
// Get Devices
|
||||||
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, auth.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appEditSettings] GetDevices DB Error: ", err)
|
log.Error("GetDevices DB Error: ", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDevices DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1181,7 +1150,7 @@ func (api *API) getDocumentsWordCount(documents []database.GetDocumentsWithStats
|
|||||||
// Do Transaction
|
// Do Transaction
|
||||||
tx, err := api.DB.DB.Begin()
|
tx, err := api.DB.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[getDocumentsWordCount] Transaction Begin DB Error: ", err)
|
log.Error("Transaction Begin DB Error: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1194,13 +1163,13 @@ func (api *API) getDocumentsWordCount(documents []database.GetDocumentsWithStats
|
|||||||
filePath := filepath.Join(api.Config.DataPath, "documents", *item.Filepath)
|
filePath := filepath.Join(api.Config.DataPath, "documents", *item.Filepath)
|
||||||
wordCount, err := metadata.GetWordCount(filePath)
|
wordCount, err := metadata.GetWordCount(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("[getDocumentsWordCount] Word Count Error: ", err)
|
log.Warn("Word Count Error: ", err)
|
||||||
} else {
|
} else {
|
||||||
if _, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
if _, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
||||||
ID: item.ID,
|
ID: item.ID,
|
||||||
Words: &wordCount,
|
Words: &wordCount,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[getDocumentsWordCount] UpsertDocument DB Error: ", err)
|
log.Error("UpsertDocument DB Error: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1209,7 +1178,7 @@ func (api *API) getDocumentsWordCount(documents []database.GetDocumentsWithStats
|
|||||||
|
|
||||||
// Commit Transaction
|
// Commit Transaction
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
log.Error("[getDocumentsWordCount] Transaction Commit DB Error: ", err)
|
log.Error("Transaction Commit DB Error: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1349,3 +1318,175 @@ func arrangeUserStatistics(userStatistics []database.GetUserStatisticsRow) gin.H
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *API) processRestoreFile(rAdminAction requestAdminAction, c *gin.Context) {
|
||||||
|
// Validate Type & Derive Extension on MIME
|
||||||
|
uploadedFile, err := rAdminAction.RestoreFile.Open()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("File Error: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to open file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileMime, err := mimetype.DetectReader(uploadedFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("MIME Error")
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to detect filetype.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fileExtension := fileMime.Extension()
|
||||||
|
|
||||||
|
// Validate Extension
|
||||||
|
if !slices.Contains([]string{".zip"}, fileExtension) {
|
||||||
|
log.Error("Invalid FileType: ", fileExtension)
|
||||||
|
errorPage(c, http.StatusBadRequest, "Invalid filetype.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Temp File
|
||||||
|
tempFile, err := os.CreateTemp("", "restore")
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("Temp File Create Error: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to create temp file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.Remove(tempFile.Name())
|
||||||
|
defer tempFile.Close()
|
||||||
|
|
||||||
|
// Save Temp
|
||||||
|
err = c.SaveUploadedFile(rAdminAction.RestoreFile, tempFile.Name())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("File Error: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to save file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZIP Info
|
||||||
|
fileInfo, err := tempFile.Stat()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("File Error: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to read file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create ZIP Reader
|
||||||
|
r, err := zip.NewReader(tempFile, fileInfo.Size())
|
||||||
|
if err != nil {
|
||||||
|
log.Error("ZIP Error: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to read zip.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate ZIP Contents
|
||||||
|
hasDBFile := false
|
||||||
|
hasUnknownFile := false
|
||||||
|
for _, f := range r.File {
|
||||||
|
fileName := strings.TrimPrefix(f.Name, "/")
|
||||||
|
if fileName == "antholume.db" {
|
||||||
|
hasDBFile = true
|
||||||
|
break
|
||||||
|
} else if !strings.HasPrefix(fileName, "covers/") && !strings.HasPrefix(fileName, "documents/") {
|
||||||
|
hasUnknownFile = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalid ZIP
|
||||||
|
if !hasDBFile {
|
||||||
|
log.Error("Invalid ZIP File - Missing DB")
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Invalid Restore ZIP - Missing DB")
|
||||||
|
return
|
||||||
|
} else if hasUnknownFile {
|
||||||
|
log.Error("Invalid ZIP File - Invalid File(s)")
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Invalid Restore ZIP - Invalid File(s)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Backup File
|
||||||
|
backupFilePath := path.Join(api.Config.ConfigPath, fmt.Sprintf("backup/AnthoLumeBackup_%s.zip", time.Now().Format("20060102")))
|
||||||
|
backupFile, err := os.Create(backupFilePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Unable to create backup file: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to create backup file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer backupFile.Close()
|
||||||
|
|
||||||
|
// Save Backup File
|
||||||
|
w := bufio.NewWriter(backupFile)
|
||||||
|
err = api.createBackup(w, []string{"covers", "documents"})
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Unable to save backup file: ", err)
|
||||||
|
errorPage(c, http.StatusInternalServerError, "Unable to save backup file.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// - Extract from temp directory
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *API) createBackup(w io.Writer, directories []string) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get DB Path
|
||||||
|
fileName := fmt.Sprintf("%s.db", api.Config.DBName)
|
||||||
|
dbLocation := path.Join(api.Config.ConfigPath, fileName)
|
||||||
|
|
||||||
|
// Copy Database File
|
||||||
|
dbFile, err := os.Open(dbLocation)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dbFile.Close()
|
||||||
|
|
||||||
|
newDbFile, err := ar.Create(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
io.Copy(newDbFile, dbFile)
|
||||||
|
|
||||||
|
// Backup Covers & Documents
|
||||||
|
for _, dir := range directories {
|
||||||
|
err = filepath.WalkDir(path.Join(api.Config.DataPath, dir), exportWalker)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ar.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -206,7 +206,7 @@ func (api *API) appAuthFormRegister(c *gin.Context) {
|
|||||||
|
|
||||||
// SQL Error
|
// SQL Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appAuthFormRegister] CreateUser DB Error:", err)
|
log.Error("CreateUser DB Error:", err)
|
||||||
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
||||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||||
return
|
return
|
||||||
@ -214,7 +214,7 @@ func (api *API) appAuthFormRegister(c *gin.Context) {
|
|||||||
|
|
||||||
// User Already Exists
|
// User Already Exists
|
||||||
if rows == 0 {
|
if rows == 0 {
|
||||||
log.Warn("[appAuthFormRegister] User Already Exists:", username)
|
log.Warn("User Already Exists:", username)
|
||||||
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
||||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||||
return
|
return
|
||||||
@ -223,7 +223,7 @@ func (api *API) appAuthFormRegister(c *gin.Context) {
|
|||||||
// Get User
|
// Get User
|
||||||
user, err := api.DB.Queries.GetUser(api.DB.Ctx, username)
|
user, err := api.DB.Queries.GetUser(api.DB.Ctx, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[appAuthFormRegister] GetUser DB Error:", err)
|
log.Error("GetUser DB Error:", err)
|
||||||
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
templateVars["Error"] = "Registration Disabled or User Already Exists"
|
||||||
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
c.HTML(http.StatusBadRequest, "page/login", templateVars)
|
||||||
return
|
return
|
||||||
@ -268,7 +268,7 @@ func getSession(session sessions.Session) (auth authData, ok bool) {
|
|||||||
|
|
||||||
// Refresh
|
// Refresh
|
||||||
if expiresAt.(int64)-time.Now().Unix() < 60*60*24 {
|
if expiresAt.(int64)-time.Now().Unix() < 60*60*24 {
|
||||||
log.Info("[getSession] Refreshing Session")
|
log.Info("Refreshing Session")
|
||||||
setSession(session, auth)
|
setSession(session, auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
func (api *API) downloadDocument(c *gin.Context) {
|
func (api *API) downloadDocument(c *gin.Context) {
|
||||||
var rDoc requestDocumentID
|
var rDoc requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDoc); err != nil {
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
||||||
log.Error("[downloadDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -22,13 +22,13 @@ func (api *API) downloadDocument(c *gin.Context) {
|
|||||||
// Get Document
|
// Get Document
|
||||||
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[downloadDocument] GetDocument DB Error:", err)
|
log.Error("GetDocument DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if document.Filepath == nil {
|
if document.Filepath == nil {
|
||||||
log.Error("[downloadDocument] Document Doesn't Have File:", rDoc.DocumentID)
|
log.Error("Document Doesn't Have File:", rDoc.DocumentID)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exist"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exist"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func (api *API) downloadDocument(c *gin.Context) {
|
|||||||
// Validate File Exists
|
// Validate File Exists
|
||||||
_, err = os.Stat(filePath)
|
_, err = os.Stat(filePath)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
log.Error("[downloadDocument] File Doesn't Exist:", rDoc.DocumentID)
|
log.Error("File Doesn't Exist:", rDoc.DocumentID)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exists"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exists"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ func (api *API) downloadDocument(c *gin.Context) {
|
|||||||
func (api *API) getDocumentCover(c *gin.Context) {
|
func (api *API) getDocumentCover(c *gin.Context) {
|
||||||
var rDoc requestDocumentID
|
var rDoc requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDoc); err != nil {
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
||||||
log.Error("[getDocumentCover] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
errorPage(c, http.StatusNotFound, "Invalid cover.")
|
errorPage(c, http.StatusNotFound, "Invalid cover.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
// Validate Document Exists in DB
|
// Validate Document Exists in DB
|
||||||
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[getDocumentCover] GetDocument DB Error:", err)
|
log.Error("GetDocument DB Error:", err)
|
||||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocument DB Error: %v", err))
|
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocument DB Error: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
// Validate File Exists
|
// Validate File Exists
|
||||||
_, err = os.Stat(safePath)
|
_, err = os.Stat(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[getDocumentCover] File Should But Doesn't Exist:", err)
|
log.Error("File Should But Doesn't Exist:", err)
|
||||||
c.FileFromFS("assets/images/no-cover.jpg", http.FS(api.Assets))
|
c.FileFromFS("assets/images/no-cover.jpg", http.FS(api.Assets))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -117,7 +117,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
Isbn10: firstResult.ISBN10,
|
Isbn10: firstResult.ISBN10,
|
||||||
Isbn13: firstResult.ISBN13,
|
Isbn13: firstResult.ISBN13,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[getDocumentCover] AddMetadata DB Error:", err)
|
log.Error("AddMetadata DB Error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +126,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
|
|||||||
ID: document.ID,
|
ID: document.ID,
|
||||||
Coverfile: &coverFile,
|
Coverfile: &coverFile,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Warn("[getDocumentCover] UpsertDocument DB Error:", err)
|
log.Warn("UpsertDocument DB Error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return Unknown Cover
|
// Return Unknown Cover
|
||||||
|
@ -89,20 +89,20 @@ func (api *API) koCreateUser(c *gin.Context) {
|
|||||||
|
|
||||||
var rUser requestUser
|
var rUser requestUser
|
||||||
if err := c.ShouldBindJSON(&rUser); err != nil {
|
if err := c.ShouldBindJSON(&rUser); err != nil {
|
||||||
log.Error("[koCreateUser] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if rUser.Username == "" || rUser.Password == "" {
|
if rUser.Username == "" || rUser.Password == "" {
|
||||||
log.Error("[koCreateUser] Invalid User - Empty Username or Password")
|
log.Error("Invalid User - Empty Username or Password")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := argon2.CreateHash(rUser.Password, argon2.DefaultParams)
|
hashedPassword, err := argon2.CreateHash(rUser.Password, argon2.DefaultParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCreateUser] Argon2 Hash Failure:", err)
|
log.Error("Argon2 Hash Failure:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -112,7 +112,7 @@ func (api *API) koCreateUser(c *gin.Context) {
|
|||||||
Pass: &hashedPassword,
|
Pass: &hashedPassword,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCreateUser] CreateUser DB Error:", err)
|
log.Error("CreateUser DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -136,7 +136,7 @@ func (api *API) koSetProgress(c *gin.Context) {
|
|||||||
|
|
||||||
var rPosition requestPosition
|
var rPosition requestPosition
|
||||||
if err := c.ShouldBindJSON(&rPosition); err != nil {
|
if err := c.ShouldBindJSON(&rPosition); err != nil {
|
||||||
log.Error("[koSetProgress] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Progress Data"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Progress Data"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -148,14 +148,14 @@ func (api *API) koSetProgress(c *gin.Context) {
|
|||||||
DeviceName: rPosition.Device,
|
DeviceName: rPosition.Device,
|
||||||
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koSetProgress] UpsertDevice DB Error:", err)
|
log.Error("UpsertDevice DB Error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upsert Document
|
// Upsert Document
|
||||||
if _, err := api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
if _, err := api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
||||||
ID: rPosition.DocumentID,
|
ID: rPosition.DocumentID,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koSetProgress] UpsertDocument DB Error:", err)
|
log.Error("UpsertDocument DB Error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create or Replace Progress
|
// Create or Replace Progress
|
||||||
@ -167,7 +167,7 @@ func (api *API) koSetProgress(c *gin.Context) {
|
|||||||
Progress: rPosition.Progress,
|
Progress: rPosition.Progress,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koSetProgress] UpdateProgress DB Error:", err)
|
log.Error("UpdateProgress DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -186,7 +186,7 @@ func (api *API) koGetProgress(c *gin.Context) {
|
|||||||
|
|
||||||
var rDocID requestDocumentID
|
var rDocID requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDocID); err != nil {
|
if err := c.ShouldBindUri(&rDocID); err != nil {
|
||||||
log.Error("[koGetProgress] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -201,7 +201,7 @@ func (api *API) koGetProgress(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{})
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Error("[koGetProgress] GetDocumentProgress DB Error:", err)
|
log.Error("GetDocumentProgress DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -223,7 +223,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
|
|
||||||
var rActivity requestActivity
|
var rActivity requestActivity
|
||||||
if err := c.ShouldBindJSON(&rActivity); err != nil {
|
if err := c.ShouldBindJSON(&rActivity); err != nil {
|
||||||
log.Error("[koAddActivities] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -231,7 +231,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
// Do Transaction
|
// Do Transaction
|
||||||
tx, err := api.DB.DB.Begin()
|
tx, err := api.DB.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koAddActivities] Transaction Begin DB Error:", err)
|
log.Error("Transaction Begin DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -252,7 +252,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
if _, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
if _, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
|
||||||
ID: doc,
|
ID: doc,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koAddActivities] UpsertDocument DB Error:", err)
|
log.Error("UpsertDocument DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -265,7 +265,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
DeviceName: rActivity.Device,
|
DeviceName: rActivity.Device,
|
||||||
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koAddActivities] UpsertDevice DB Error:", err)
|
log.Error("UpsertDevice DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -281,7 +281,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
StartPercentage: float64(item.Page) / float64(item.Pages),
|
StartPercentage: float64(item.Page) / float64(item.Pages),
|
||||||
EndPercentage: float64(item.Page+1) / float64(item.Pages),
|
EndPercentage: float64(item.Page+1) / float64(item.Pages),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koAddActivities] AddActivity DB Error:", err)
|
log.Error("AddActivity DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -289,7 +289,7 @@ func (api *API) koAddActivities(c *gin.Context) {
|
|||||||
|
|
||||||
// Commit Transaction
|
// Commit Transaction
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
log.Error("[koAddActivities] Transaction Commit DB Error:", err)
|
log.Error("Transaction Commit DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -307,7 +307,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
|
|||||||
|
|
||||||
var rCheckActivity requestCheckActivitySync
|
var rCheckActivity requestCheckActivitySync
|
||||||
if err := c.ShouldBindJSON(&rCheckActivity); err != nil {
|
if err := c.ShouldBindJSON(&rCheckActivity); err != nil {
|
||||||
log.Error("[koCheckActivitySync] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -319,7 +319,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
|
|||||||
DeviceName: rCheckActivity.Device,
|
DeviceName: rCheckActivity.Device,
|
||||||
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koCheckActivitySync] UpsertDevice DB Error", err)
|
log.Error("UpsertDevice DB Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -332,7 +332,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
|
|||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
lastActivity = time.UnixMilli(0).Format(time.RFC3339)
|
lastActivity = time.UnixMilli(0).Format(time.RFC3339)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Error("[koCheckActivitySync] GetLastActivity DB Error:", err)
|
log.Error("GetLastActivity DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -340,7 +340,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
|
|||||||
// Parse Time
|
// Parse Time
|
||||||
parsedTime, err := time.Parse(time.RFC3339, lastActivity)
|
parsedTime, err := time.Parse(time.RFC3339, lastActivity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckActivitySync] Time Parse Error:", err)
|
log.Error("Time Parse Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -353,7 +353,7 @@ func (api *API) koCheckActivitySync(c *gin.Context) {
|
|||||||
func (api *API) koAddDocuments(c *gin.Context) {
|
func (api *API) koAddDocuments(c *gin.Context) {
|
||||||
var rNewDocs requestDocument
|
var rNewDocs requestDocument
|
||||||
if err := c.ShouldBindJSON(&rNewDocs); err != nil {
|
if err := c.ShouldBindJSON(&rNewDocs); err != nil {
|
||||||
log.Error("[koAddDocuments] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document(s)"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document(s)"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -361,7 +361,7 @@ func (api *API) koAddDocuments(c *gin.Context) {
|
|||||||
// Do Transaction
|
// Do Transaction
|
||||||
tx, err := api.DB.DB.Begin()
|
tx, err := api.DB.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koAddDocuments] Transaction Begin DB Error:", err)
|
log.Error("Transaction Begin DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -382,7 +382,7 @@ func (api *API) koAddDocuments(c *gin.Context) {
|
|||||||
Description: api.sanitizeInput(doc.Description),
|
Description: api.sanitizeInput(doc.Description),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koAddDocuments] UpsertDocument DB Error:", err)
|
log.Error("UpsertDocument DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -390,7 +390,7 @@ func (api *API) koAddDocuments(c *gin.Context) {
|
|||||||
|
|
||||||
// Commit Transaction
|
// Commit Transaction
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
log.Error("[koAddDocuments] Transaction Commit DB Error:", err)
|
log.Error("Transaction Commit DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -408,7 +408,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
|
|
||||||
var rCheckDocs requestCheckDocumentSync
|
var rCheckDocs requestCheckDocumentSync
|
||||||
if err := c.ShouldBindJSON(&rCheckDocs); err != nil {
|
if err := c.ShouldBindJSON(&rCheckDocs); err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] Invalid JSON Bind")
|
log.Error("Invalid JSON Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -421,7 +421,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
LastSynced: time.Now().UTC().Format(time.RFC3339),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] UpsertDevice DB Error", err)
|
log.Error("UpsertDevice DB Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -432,7 +432,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
// Get Missing Documents
|
// Get Missing Documents
|
||||||
missingDocs, err = api.DB.Queries.GetMissingDocuments(api.DB.Ctx, rCheckDocs.Have)
|
missingDocs, err = api.DB.Queries.GetMissingDocuments(api.DB.Ctx, rCheckDocs.Have)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] GetMissingDocuments DB Error", err)
|
log.Error("GetMissingDocuments DB Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -440,7 +440,7 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
// Get Deleted Documents
|
// Get Deleted Documents
|
||||||
deletedDocIDs, err = api.DB.Queries.GetDeletedDocuments(api.DB.Ctx, rCheckDocs.Have)
|
deletedDocIDs, err = api.DB.Queries.GetDeletedDocuments(api.DB.Ctx, rCheckDocs.Have)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] GetDeletedDocuments DB Error", err)
|
log.Error("GetDeletedDocuments DB Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -448,14 +448,14 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
// Get Wanted Documents
|
// Get Wanted Documents
|
||||||
jsonHaves, err := json.Marshal(rCheckDocs.Have)
|
jsonHaves, err := json.Marshal(rCheckDocs.Have)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] JSON Marshal Error", err)
|
log.Error("JSON Marshal Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wantedDocs, err := api.DB.Queries.GetWantedDocuments(api.DB.Ctx, string(jsonHaves))
|
wantedDocs, err := api.DB.Queries.GetWantedDocuments(api.DB.Ctx, string(jsonHaves))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koCheckDocumentsSync] GetWantedDocuments DB Error", err)
|
log.Error("GetWantedDocuments DB Error", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -499,14 +499,14 @@ func (api *API) koCheckDocumentsSync(c *gin.Context) {
|
|||||||
func (api *API) koUploadExistingDocument(c *gin.Context) {
|
func (api *API) koUploadExistingDocument(c *gin.Context) {
|
||||||
var rDoc requestDocumentID
|
var rDoc requestDocumentID
|
||||||
if err := c.ShouldBindUri(&rDoc); err != nil {
|
if err := c.ShouldBindUri(&rDoc); err != nil {
|
||||||
log.Error("[koUploadExistingDocument] Invalid URI Bind")
|
log.Error("Invalid URI Bind")
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileData, err := c.FormFile("file")
|
fileData, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koUploadExistingDocument] File Error:", err)
|
log.Error("File Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -517,7 +517,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
fileExtension := fileMime.Extension()
|
fileExtension := fileMime.Extension()
|
||||||
|
|
||||||
if !slices.Contains([]string{".epub", ".html"}, fileExtension) {
|
if !slices.Contains([]string{".epub", ".html"}, fileExtension) {
|
||||||
log.Error("[koUploadExistingDocument] Invalid FileType:", fileExtension)
|
log.Error("Invalid FileType:", fileExtension)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Filetype"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Filetype"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -525,7 +525,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
// Validate Document Exists in DB
|
// Validate Document Exists in DB
|
||||||
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koUploadExistingDocument] GetDocument DB Error:", err)
|
log.Error("GetDocument DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -558,7 +558,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = c.SaveUploadedFile(fileData, safePath)
|
err = c.SaveUploadedFile(fileData, safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koUploadExistingDocument] Save Failure:", err)
|
log.Error("Save Failure:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -567,7 +567,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
// Get MD5 Hash
|
// Get MD5 Hash
|
||||||
fileHash, err := getFileMD5(safePath)
|
fileHash, err := getFileMD5(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koUploadExistingDocument] Hash Failure:", err)
|
log.Error("Hash Failure:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -575,7 +575,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
// Get Word Count
|
// Get Word Count
|
||||||
wordCount, err := metadata.GetWordCount(safePath)
|
wordCount, err := metadata.GetWordCount(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[koUploadExistingDocument] Word Count Failure:", err)
|
log.Error("Word Count Failure:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -587,7 +587,7 @@ func (api *API) koUploadExistingDocument(c *gin.Context) {
|
|||||||
Filepath: &fileName,
|
Filepath: &fileName,
|
||||||
Words: &wordCount,
|
Words: &wordCount,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Error("[koUploadExistingDocument] UpsertDocument DB Error:", err)
|
log.Error("UpsertDocument DB Error:", err)
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Error"})
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ func (api *API) opdsDocuments(c *gin.Context) {
|
|||||||
Limit: *qParams.Limit,
|
Limit: *qParams.Limit,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[opdsDocuments] GetDocumentsWithStats DB Error:", err)
|
log.Error("GetDocumentsWithStats DB Error:", err)
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1,12 +1,13 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/snowzach/rotatefilehook"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -29,18 +30,23 @@ type Config struct {
|
|||||||
LogLevel string
|
LogLevel string
|
||||||
|
|
||||||
// Cookie Settings
|
// Cookie Settings
|
||||||
CookieSessionKey string
|
CookieAuthKey string
|
||||||
CookieSecure bool
|
CookieEncKey string
|
||||||
CookieHTTPOnly bool
|
CookieSecure bool
|
||||||
|
CookieHTTPOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type UTCFormatter struct {
|
type customFormatter struct {
|
||||||
log.Formatter
|
log.Formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
|
// Force UTC & Set Type (app)
|
||||||
|
func (cf customFormatter) Format(e *log.Entry) ([]byte, error) {
|
||||||
|
if e.Data["type"] == nil {
|
||||||
|
e.Data["type"] = "app"
|
||||||
|
}
|
||||||
e.Time = e.Time.UTC()
|
e.Time = e.Time.UTC()
|
||||||
return u.Formatter.Format(e)
|
return cf.Formatter.Format(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set at runtime
|
// Set at runtime
|
||||||
@ -49,15 +55,16 @@ var version string = "develop"
|
|||||||
func Load() *Config {
|
func Load() *Config {
|
||||||
c := &Config{
|
c := &Config{
|
||||||
Version: version,
|
Version: version,
|
||||||
DBType: trimLowerString(getEnv("DATABASE_TYPE", "SQLite")),
|
|
||||||
DBName: trimLowerString(getEnv("DATABASE_NAME", "antholume")),
|
|
||||||
ConfigPath: getEnv("CONFIG_PATH", "/config"),
|
ConfigPath: getEnv("CONFIG_PATH", "/config"),
|
||||||
DataPath: getEnv("DATA_PATH", "/data"),
|
DataPath: getEnv("DATA_PATH", "/data"),
|
||||||
ListenPort: getEnv("LISTEN_PORT", "8585"),
|
ListenPort: getEnv("LISTEN_PORT", "8585"),
|
||||||
|
DBType: trimLowerString(getEnv("DATABASE_TYPE", "SQLite")),
|
||||||
|
DBName: trimLowerString(getEnv("DATABASE_NAME", "antholume")),
|
||||||
RegistrationEnabled: trimLowerString(getEnv("REGISTRATION_ENABLED", "false")) == "true",
|
RegistrationEnabled: trimLowerString(getEnv("REGISTRATION_ENABLED", "false")) == "true",
|
||||||
DemoMode: trimLowerString(getEnv("DEMO_MODE", "false")) == "true",
|
DemoMode: trimLowerString(getEnv("DEMO_MODE", "false")) == "true",
|
||||||
SearchEnabled: trimLowerString(getEnv("SEARCH_ENABLED", "false")) == "true",
|
SearchEnabled: trimLowerString(getEnv("SEARCH_ENABLED", "false")) == "true",
|
||||||
CookieSessionKey: trimLowerString(getEnv("COOKIE_SESSION_KEY", "")),
|
CookieAuthKey: trimLowerString(getEnv("COOKIE_AUTH_KEY", "")),
|
||||||
|
CookieEncKey: trimLowerString(getEnv("COOKIE_ENC_KEY", "")),
|
||||||
LogLevel: trimLowerString(getEnv("LOG_LEVEL", "info")),
|
LogLevel: trimLowerString(getEnv("LOG_LEVEL", "info")),
|
||||||
CookieSecure: trimLowerString(getEnv("COOKIE_SECURE", "true")) == "true",
|
CookieSecure: trimLowerString(getEnv("COOKIE_SECURE", "true")) == "true",
|
||||||
CookieHTTPOnly: trimLowerString(getEnv("COOKIE_HTTP_ONLY", "true")) == "true",
|
CookieHTTPOnly: trimLowerString(getEnv("COOKIE_HTTP_ONLY", "true")) == "true",
|
||||||
@ -70,24 +77,29 @@ func Load() *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Log Formatter
|
// Log Formatter
|
||||||
ttyLogFormatter := &UTCFormatter{&log.TextFormatter{FullTimestamp: true}}
|
logFormatter := &customFormatter{&log.JSONFormatter{
|
||||||
fileLogFormatter := &UTCFormatter{&log.TextFormatter{FullTimestamp: true, DisableColors: true}}
|
CallerPrettyfier: prettyCaller,
|
||||||
|
}}
|
||||||
|
|
||||||
// Log Rotater
|
// Log Rotater
|
||||||
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
|
rotateFileHook, err := NewRotateFileHook(RotateFileConfig{
|
||||||
Filename: path.Join(c.ConfigPath, "logs/antholume.log"),
|
Filename: path.Join(c.ConfigPath, "logs/antholume.log"),
|
||||||
MaxSize: 50,
|
MaxSize: 50,
|
||||||
MaxBackups: 3,
|
MaxBackups: 3,
|
||||||
MaxAge: 30,
|
MaxAge: 30,
|
||||||
Level: logLevel,
|
Level: logLevel,
|
||||||
Formatter: fileLogFormatter,
|
Formatter: logFormatter,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("[config.Load] Unable to initialize file rotate hook")
|
log.Fatal("Unable to initialize file rotate hook")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rotate Now
|
||||||
|
rotateFileHook.Rotate()
|
||||||
|
|
||||||
log.SetLevel(logLevel)
|
log.SetLevel(logLevel)
|
||||||
log.SetFormatter(ttyLogFormatter)
|
log.SetFormatter(logFormatter)
|
||||||
|
log.SetReportCaller(true)
|
||||||
log.AddHook(rotateFileHook)
|
log.AddHook(rotateFileHook)
|
||||||
|
|
||||||
return c
|
return c
|
||||||
@ -103,3 +115,24 @@ func getEnv(key, fallback string) string {
|
|||||||
func trimLowerString(val string) string {
|
func trimLowerString(val string) string {
|
||||||
return strings.ToLower(strings.TrimSpace(val))
|
return strings.ToLower(strings.TrimSpace(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func prettyCaller(f *runtime.Frame) (function string, file string) {
|
||||||
|
purgePrefix := "reichard.io/antholume/"
|
||||||
|
|
||||||
|
pathName := strings.Replace(f.Func.Name(), purgePrefix, "", 1)
|
||||||
|
parts := strings.Split(pathName, ".")
|
||||||
|
|
||||||
|
filepath, line := f.Func.FileLine(f.PC)
|
||||||
|
splitFilePath := strings.Split(filepath, "/")
|
||||||
|
|
||||||
|
fileName := fmt.Sprintf("%s/%s@%d", parts[0], splitFilePath[len(splitFilePath)-1], line)
|
||||||
|
functionName := strings.Replace(pathName, parts[0]+".", "", 1)
|
||||||
|
|
||||||
|
// Exclude GIN Logger
|
||||||
|
if functionName == "NewApi.apiLogger.func1" {
|
||||||
|
fileName = ""
|
||||||
|
functionName = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return functionName, fileName
|
||||||
|
}
|
||||||
|
54
config/logger.go
Normal file
54
config/logger.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Modified "snowzach/rotatefilehook" to support manual rotation
|
||||||
|
|
||||||
|
type RotateFileConfig struct {
|
||||||
|
Filename string
|
||||||
|
MaxSize int
|
||||||
|
MaxBackups int
|
||||||
|
MaxAge int
|
||||||
|
Compress bool
|
||||||
|
Level logrus.Level
|
||||||
|
Formatter logrus.Formatter
|
||||||
|
}
|
||||||
|
|
||||||
|
type RotateFileHook struct {
|
||||||
|
Config RotateFileConfig
|
||||||
|
logWriter *lumberjack.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRotateFileHook(config RotateFileConfig) (*RotateFileHook, error) {
|
||||||
|
hook := RotateFileHook{
|
||||||
|
Config: config,
|
||||||
|
}
|
||||||
|
hook.logWriter = &lumberjack.Logger{
|
||||||
|
Filename: config.Filename,
|
||||||
|
MaxSize: config.MaxSize,
|
||||||
|
MaxBackups: config.MaxBackups,
|
||||||
|
MaxAge: config.MaxAge,
|
||||||
|
Compress: config.Compress,
|
||||||
|
}
|
||||||
|
return &hook, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *RotateFileHook) Rotate() error {
|
||||||
|
return hook.logWriter.Rotate()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *RotateFileHook) Levels() []logrus.Level {
|
||||||
|
return logrus.AllLevels[:hook.Config.Level+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *RotateFileHook) Fire(entry *logrus.Entry) (err error) {
|
||||||
|
b, err := hook.Config.Formatter.Format(entry)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hook.logWriter.Write(b)
|
||||||
|
return nil
|
||||||
|
}
|
@ -43,7 +43,7 @@ func NewMgr(c *config.Config) *DBManager {
|
|||||||
var err error
|
var err error
|
||||||
dbm.DB, err = sql.Open("sqlite", dbLocation)
|
dbm.DB, err = sql.Open("sqlite", dbLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("[NewMgr] Unable to open DB: %v", err)
|
log.Fatalf("Unable to open DB: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single Open Connection
|
// Single Open Connection
|
||||||
@ -51,19 +51,19 @@ func NewMgr(c *config.Config) *DBManager {
|
|||||||
|
|
||||||
// Execute DDL
|
// Execute DDL
|
||||||
if _, err := dbm.DB.Exec(ddl, nil); err != nil {
|
if _, err := dbm.DB.Exec(ddl, nil); err != nil {
|
||||||
log.Fatalf("[NewMgr] Error executing schema: %v", err)
|
log.Fatalf("Error executing schema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform Migrations
|
// Perform Migrations
|
||||||
err = dbm.performMigrations()
|
err = dbm.performMigrations()
|
||||||
if err != nil && err != goose.ErrNoMigrationFiles {
|
if err != nil && err != goose.ErrNoMigrationFiles {
|
||||||
log.Fatalf("[NewMgr] Error running DB migrations: %v", err)
|
log.Fatalf("Error running DB migrations: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache Tables
|
// Cache Tables
|
||||||
dbm.CacheTempTables()
|
dbm.CacheTempTables()
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("[NewMgr] Unsupported Database")
|
log.Fatal("Unsupported Database")
|
||||||
}
|
}
|
||||||
|
|
||||||
dbm.Queries = New(dbm.DB)
|
dbm.Queries = New(dbm.DB)
|
||||||
@ -84,7 +84,7 @@ func (dbm *DBManager) CacheTempTables() error {
|
|||||||
if _, err := dbm.DB.ExecContext(dbm.Ctx, user_streaks_sql); err != nil {
|
if _, err := dbm.DB.ExecContext(dbm.Ctx, user_streaks_sql); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Debug("[CacheTempTables] Cached 'user_streaks' in: ", time.Since(start))
|
log.Debug("Cached 'user_streaks' in: ", time.Since(start))
|
||||||
|
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
document_statistics_sql := `
|
document_statistics_sql := `
|
||||||
@ -94,7 +94,7 @@ func (dbm *DBManager) CacheTempTables() error {
|
|||||||
if _, err := dbm.DB.ExecContext(dbm.Ctx, document_statistics_sql); err != nil {
|
if _, err := dbm.DB.ExecContext(dbm.Ctx, document_statistics_sql); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Debug("[CacheTempTables] Cached 'document_user_statistics' in: ", time.Since(start))
|
log.Debug("Cached 'document_user_statistics' in: ", time.Since(start))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
3
go.mod
3
go.mod
@ -12,10 +12,10 @@ require (
|
|||||||
github.com/microcosm-cc/bluemonday v1.0.26
|
github.com/microcosm-cc/bluemonday v1.0.26
|
||||||
github.com/pressly/goose/v3 v3.17.0
|
github.com/pressly/goose/v3 v3.17.0
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d
|
|
||||||
github.com/taylorskalyo/goreader v0.0.0-20230626212555-e7f5644f8115
|
github.com/taylorskalyo/goreader v0.0.0-20230626212555-e7f5644f8115
|
||||||
github.com/urfave/cli/v2 v2.27.1
|
github.com/urfave/cli/v2 v2.27.1
|
||||||
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
|
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||||
modernc.org/sqlite v1.28.0
|
modernc.org/sqlite v1.28.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,7 +61,6 @@ require (
|
|||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
golang.org/x/tools v0.17.0 // indirect
|
golang.org/x/tools v0.17.0 // indirect
|
||||||
google.golang.org/protobuf v1.32.0 // indirect
|
google.golang.org/protobuf v1.32.0 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
lukechampine.com/uint128 v1.3.0 // indirect
|
lukechampine.com/uint128 v1.3.0 // indirect
|
||||||
modernc.org/cc/v3 v3.41.0 // indirect
|
modernc.org/cc/v3 v3.41.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -203,8 +203,6 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
|
|||||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d h1:4660u5vJtsyrn3QwJNfESwCws+TM1CMhRn123xjVyQ8=
|
|
||||||
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d/go.mod h1:ZLVe3VfhAuMYLYWliGEydMBoRnfib8EFSqkBYu1ck9E=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
@ -122,33 +122,33 @@ func saveGBooksCover(gbid string, coverFilePath string, overwrite bool) error {
|
|||||||
// Validate File Doesn't Exists
|
// Validate File Doesn't Exists
|
||||||
_, err := os.Stat(coverFilePath)
|
_, err := os.Stat(coverFilePath)
|
||||||
if err == nil && overwrite == false {
|
if err == nil && overwrite == false {
|
||||||
log.Warn("[saveGBooksCover] File Alreads Exists")
|
log.Warn("File Alreads Exists")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create File
|
// Create File
|
||||||
out, err := os.Create(coverFilePath)
|
out, err := os.Create(coverFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[saveGBooksCover] File Create Error")
|
log.Error("File Create Error")
|
||||||
return errors.New("File Failure")
|
return errors.New("File Failure")
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
// Download File
|
// Download File
|
||||||
log.Info("[saveGBooksCover] Downloading Cover")
|
log.Info("Downloading Cover")
|
||||||
coverURL := fmt.Sprintf(GBOOKS_GBID_COVER_URL, gbid)
|
coverURL := fmt.Sprintf(GBOOKS_GBID_COVER_URL, gbid)
|
||||||
resp, err := http.Get(coverURL)
|
resp, err := http.Get(coverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[saveGBooksCover] Cover URL API Failure")
|
log.Error("Cover URL API Failure")
|
||||||
return errors.New("API Failure")
|
return errors.New("API Failure")
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Copy File to Disk
|
// Copy File to Disk
|
||||||
log.Info("[saveGBooksCover] Saving Cover")
|
log.Info("Saving Cover")
|
||||||
_, err = io.Copy(out, resp.Body)
|
_, err = io.Copy(out, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[saveGBooksCover] File Copy Error")
|
log.Error("File Copy Error")
|
||||||
return errors.New("File Failure")
|
return errors.New("File Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,22 +157,22 @@ func saveGBooksCover(gbid string, coverFilePath string, overwrite bool) error {
|
|||||||
|
|
||||||
func performSearchRequest(searchQuery string) (*gBooksQueryResponse, error) {
|
func performSearchRequest(searchQuery string) (*gBooksQueryResponse, error) {
|
||||||
apiQuery := fmt.Sprintf(GBOOKS_QUERY_URL, searchQuery)
|
apiQuery := fmt.Sprintf(GBOOKS_QUERY_URL, searchQuery)
|
||||||
log.Info("[performSearchRequest] Acquiring Metadata: ", apiQuery)
|
log.Info("Acquiring Metadata: ", apiQuery)
|
||||||
resp, err := http.Get(apiQuery)
|
resp, err := http.Get(apiQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[performSearchRequest] Google Books Query URL API Failure")
|
log.Error("Google Books Query URL API Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedResp := gBooksQueryResponse{}
|
parsedResp := gBooksQueryResponse{}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[performSearchRequest] Google Books Query API Decode Failure")
|
log.Error("Google Books Query API Decode Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(parsedResp.Items) == 0 {
|
if len(parsedResp.Items) == 0 {
|
||||||
log.Warn("[performSearchRequest] No Results")
|
log.Warn("No Results")
|
||||||
return nil, errors.New("No Results")
|
return nil, errors.New("No Results")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,17 +182,17 @@ func performSearchRequest(searchQuery string) (*gBooksQueryResponse, error) {
|
|||||||
func performGBIDRequest(id string) (*gBooksQueryItem, error) {
|
func performGBIDRequest(id string) (*gBooksQueryItem, error) {
|
||||||
apiQuery := fmt.Sprintf(GBOOKS_GBID_INFO_URL, id)
|
apiQuery := fmt.Sprintf(GBOOKS_GBID_INFO_URL, id)
|
||||||
|
|
||||||
log.Info("[performGBIDRequest] Acquiring CoverID")
|
log.Info("Acquiring CoverID")
|
||||||
resp, err := http.Get(apiQuery)
|
resp, err := http.Get(apiQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[performGBIDRequest] Cover URL API Failure")
|
log.Error("Cover URL API Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedResp := gBooksQueryItem{}
|
parsedResp := gBooksQueryItem{}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
err = json.NewDecoder(resp.Body).Decode(&parsedResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[performGBIDRequest] Google Books ID API Decode Failure")
|
log.Error("Google Books ID API Decode Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,24 +32,24 @@ const OLIB_ISBN_LINK_URL string = "https://openlibrary.org/isbn/%s"
|
|||||||
|
|
||||||
func GetCoverOLIDs(title *string, author *string) ([]string, error) {
|
func GetCoverOLIDs(title *string, author *string) ([]string, error) {
|
||||||
if title == nil || author == nil {
|
if title == nil || author == nil {
|
||||||
log.Error("[metadata] Invalid Search Query")
|
log.Error("Invalid Search Query")
|
||||||
return nil, errors.New("Invalid Query")
|
return nil, errors.New("Invalid Query")
|
||||||
}
|
}
|
||||||
|
|
||||||
searchQuery := url.QueryEscape(fmt.Sprintf("%s %s", *title, *author))
|
searchQuery := url.QueryEscape(fmt.Sprintf("%s %s", *title, *author))
|
||||||
apiQuery := fmt.Sprintf(OLIB_QUERY_URL, searchQuery)
|
apiQuery := fmt.Sprintf(OLIB_QUERY_URL, searchQuery)
|
||||||
|
|
||||||
log.Info("[metadata] Acquiring CoverID")
|
log.Info("Acquiring CoverID")
|
||||||
resp, err := http.Get(apiQuery)
|
resp, err := http.Get(apiQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[metadata] Cover URL API Failure")
|
log.Error("Cover URL API Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
target := oLibQueryResponse{}
|
target := oLibQueryResponse{}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&target)
|
err = json.NewDecoder(resp.Body).Decode(&target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[metadata] Cover URL API Decode Failure")
|
log.Error("Cover URL API Decode Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,24 +73,24 @@ func DownloadAndSaveCover(coverID string, dirPath string) (*string, error) {
|
|||||||
// Validate File Doesn't Exists
|
// Validate File Doesn't Exists
|
||||||
_, err := os.Stat(safePath)
|
_, err := os.Stat(safePath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Warn("[metadata] File Alreads Exists")
|
log.Warn("File Alreads Exists")
|
||||||
return &safePath, nil
|
return &safePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create File
|
// Create File
|
||||||
out, err := os.Create(safePath)
|
out, err := os.Create(safePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[metadata] File Create Error")
|
log.Error("File Create Error")
|
||||||
return nil, errors.New("File Failure")
|
return nil, errors.New("File Failure")
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
// Download File
|
// Download File
|
||||||
log.Info("[metadata] Downloading Cover")
|
log.Info("Downloading Cover")
|
||||||
coverURL := fmt.Sprintf(OLIB_OLID_COVER_URL, coverID)
|
coverURL := fmt.Sprintf(OLIB_OLID_COVER_URL, coverID)
|
||||||
resp, err := http.Get(coverURL)
|
resp, err := http.Get(coverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[metadata] Cover URL API Failure")
|
log.Error("Cover URL API Failure")
|
||||||
return nil, errors.New("API Failure")
|
return nil, errors.New("API Failure")
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@ -98,7 +98,7 @@ func DownloadAndSaveCover(coverID string, dirPath string) (*string, error) {
|
|||||||
// Copy File to Disk
|
// Copy File to Disk
|
||||||
_, err = io.Copy(out, resp.Body)
|
_, err = io.Copy(out, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[metadata] File Copy Error")
|
log.Error("File Copy Error")
|
||||||
return nil, errors.New("File Failure")
|
return nil, errors.New("File Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ var sourceDefs = map[Source]sourceDef{
|
|||||||
|
|
||||||
func SearchBook(query string, source Source) ([]SearchItem, error) {
|
func SearchBook(query string, source Source) ([]SearchItem, error) {
|
||||||
def := sourceDefs[source]
|
def := sourceDefs[source]
|
||||||
log.Debug("[SearchBook] Source: ", def)
|
log.Debug("Source: ", def)
|
||||||
url := fmt.Sprintf(def.searchURL, url.QueryEscape(query))
|
url := fmt.Sprintf(def.searchURL, url.QueryEscape(query))
|
||||||
body, err := getPage(url)
|
body, err := getPage(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -91,7 +91,7 @@ func SearchBook(query string, source Source) ([]SearchItem, error) {
|
|||||||
|
|
||||||
func SaveBook(id string, source Source) (string, error) {
|
func SaveBook(id string, source Source) (string, error) {
|
||||||
def := sourceDefs[source]
|
def := sourceDefs[source]
|
||||||
log.Debug("[SaveBook] Source: ", def)
|
log.Debug("Source: ", def)
|
||||||
url := fmt.Sprintf(def.downloadURL, id)
|
url := fmt.Sprintf(def.downloadURL, id)
|
||||||
|
|
||||||
body, err := getPage(url)
|
body, err := getPage(url)
|
||||||
@ -101,34 +101,34 @@ func SaveBook(id string, source Source) (string, error) {
|
|||||||
|
|
||||||
bookURL, err := def.parseDownloadFunc(body)
|
bookURL, err := def.parseDownloadFunc(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[SaveBook] Parse Download URL Error: ", err)
|
log.Error("Parse Download URL Error: ", err)
|
||||||
return "", errors.New("Download Failure")
|
return "", errors.New("Download Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create File
|
// Create File
|
||||||
tempFile, err := os.CreateTemp("", "book")
|
tempFile, err := os.CreateTemp("", "book")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("[SaveBook] File Create Error: ", err)
|
log.Error("File Create Error: ", err)
|
||||||
return "", errors.New("File Failure")
|
return "", errors.New("File Failure")
|
||||||
}
|
}
|
||||||
defer tempFile.Close()
|
defer tempFile.Close()
|
||||||
|
|
||||||
// Download File
|
// Download File
|
||||||
log.Info("[SaveBook] Downloading Book: ", bookURL)
|
log.Info("Downloading Book: ", bookURL)
|
||||||
resp, err := downloadBook(bookURL)
|
resp, err := downloadBook(bookURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(tempFile.Name())
|
os.Remove(tempFile.Name())
|
||||||
log.Error("[SaveBook] Book URL API Failure: ", err)
|
log.Error("Book URL API Failure: ", err)
|
||||||
return "", errors.New("API Failure")
|
return "", errors.New("API Failure")
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Copy File to Disk
|
// Copy File to Disk
|
||||||
log.Info("[SaveBook] Saving Book")
|
log.Info("Saving Book")
|
||||||
_, err = io.Copy(tempFile, resp.Body)
|
_, err = io.Copy(tempFile, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(tempFile.Name())
|
os.Remove(tempFile.Name())
|
||||||
log.Error("[SaveBook] File Copy Error: ", err)
|
log.Error("File Copy Error: ", err)
|
||||||
return "", errors.New("File Failure")
|
return "", errors.New("File Failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ func GetBookURL(id string, bookType BookType) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPage(page string) (io.ReadCloser, error) {
|
func getPage(page string) (io.ReadCloser, error) {
|
||||||
log.Debug("[getPage] ", page)
|
log.Debug("URL: ", page)
|
||||||
|
|
||||||
// Set 10s Timeout
|
// Set 10s Timeout
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
|
@ -29,10 +29,16 @@ func NewServer(assets *embed.FS) *Server {
|
|||||||
api := api.NewApi(db, c, assets)
|
api := api.NewApi(db, c, assets)
|
||||||
|
|
||||||
// Create Paths
|
// Create Paths
|
||||||
|
os.Mkdir(c.ConfigPath, 0755)
|
||||||
|
os.Mkdir(c.DataPath, 0755)
|
||||||
|
|
||||||
|
// Create Subpaths
|
||||||
docDir := filepath.Join(c.DataPath, "documents")
|
docDir := filepath.Join(c.DataPath, "documents")
|
||||||
coversDir := filepath.Join(c.DataPath, "covers")
|
coversDir := filepath.Join(c.DataPath, "covers")
|
||||||
os.Mkdir(docDir, os.ModePerm)
|
backupDir := filepath.Join(c.DataPath, "backup")
|
||||||
os.Mkdir(coversDir, os.ModePerm)
|
os.Mkdir(docDir, 0755)
|
||||||
|
os.Mkdir(coversDir, 0755)
|
||||||
|
os.Mkdir(backupDir, 0755)
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
API: api,
|
API: api,
|
||||||
@ -55,7 +61,7 @@ func (s *Server) StartServer(wg *sync.WaitGroup, done <-chan struct{}) {
|
|||||||
|
|
||||||
err := s.httpServer.ListenAndServe()
|
err := s.httpServer.ListenAndServe()
|
||||||
if err != nil && err != http.ErrServerClosed {
|
if err != nil && err != http.ErrServerClosed {
|
||||||
log.Error("Error Starting Server:", err)
|
log.Error("Error starting server:", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -68,7 +74,7 @@ func (s *Server) StartServer(wg *sync.WaitGroup, done <-chan struct{}) {
|
|||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
s.RunScheduledTasks()
|
s.RunScheduledTasks()
|
||||||
case <-done:
|
case <-done:
|
||||||
log.Info("Stopping Task Runner...")
|
log.Info("Stopping task runner...")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,23 +84,23 @@ func (s *Server) StartServer(wg *sync.WaitGroup, done <-chan struct{}) {
|
|||||||
func (s *Server) RunScheduledTasks() {
|
func (s *Server) RunScheduledTasks() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := s.API.DB.CacheTempTables(); err != nil {
|
if err := s.API.DB.CacheTempTables(); err != nil {
|
||||||
log.Warn("[RunScheduledTasks] Refreshing Temp Table Cache Failure:", err)
|
log.Warn("Refreshing temp table cache failure:", err)
|
||||||
}
|
}
|
||||||
log.Debug("[RunScheduledTasks] Completed in: ", time.Since(start))
|
log.Debug("Completed in: ", time.Since(start))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) StopServer(wg *sync.WaitGroup, done chan<- struct{}) {
|
func (s *Server) StopServer(wg *sync.WaitGroup, done chan<- struct{}) {
|
||||||
log.Info("Stopping HTTP Server...")
|
log.Info("Stopping HTTP server...")
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := s.httpServer.Shutdown(ctx); err != nil {
|
if err := s.httpServer.Shutdown(ctx); err != nil {
|
||||||
log.Info("Shutting Error")
|
log.Info("HTTP server shutdown error: ", err)
|
||||||
}
|
}
|
||||||
s.API.DB.Shutdown()
|
s.API.DB.Shutdown()
|
||||||
|
|
||||||
close(done)
|
close(done)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
log.Info("Server Stopped")
|
log.Info("Server stopped")
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<div class="flex flex-col-reverse text-black dark:text-white"
|
<div class="flex flex-col-reverse text-black dark:text-white"
|
||||||
style="font-family: monospace">
|
style="font-family: monospace">
|
||||||
{{ range $log := .Data }}
|
{{ range $log := .Data }}
|
||||||
<span class="whitespace-pre">{{ $log }}</span>
|
<span class="whitespace-nowrap hover:whitespace-pre">{{ $log }}</span>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
Loading…
Reference in New Issue
Block a user