Initial Commit

This commit is contained in:
Evan Reichard 2023-09-18 19:57:18 -04:00
commit 3b82423f10
51 changed files with 6543 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use nix

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.DS_Store
data/
.direnv/

2
.sqlfluff Normal file
View File

@ -0,0 +1,2 @@
[sqlfluff]
dialect = sqlite

13
API.md Normal file
View File

@ -0,0 +1,13 @@
# API
## Original Endpoints
POST /users/create
GET /users/auth
GET /syncs/progress/:document
PUT /syncs/progress
## New Endpoints
GET /syncs/activity
POST /syncs/activity

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
# FROM golang:1.20-alpine AS build
FROM alpine:edge AS build
RUN apk add --no-cache --update go gcc g++
WORKDIR /app
COPY . /app
RUN go mod download
RUN CGO_ENABLED=1 CGO_CFLAGS="-D_LARGEFILE64_SOURCE" go build -o /sync-ninja cmd/main.go
FROM alpine:3.18
COPY --from=build /sync-ninja /sync-ninja
EXPOSE 8585
ENTRYPOINT ["/sync-ninja", "serve"]

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
docker_build_local:
docker build -t sync-ninja:latest .
docker_build_release_beta:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t gitea.va.reichard.io/reichard/sync-ninja:beta --push .
docker_build_release_latest:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t gitea.va.reichard.io/reichard/sync-ninja:latest --push .

46
README.md Normal file
View File

@ -0,0 +1,46 @@
# Book Manager
<p align="center">
<a href="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/login.png">
<img src="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/login.png" width="33%">
</a>
<a href="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/home.png">
<img src="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/home.png" width="33%">
</a>
<a href="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/documents.png">
<img src="https://gitea.va.reichard.io/evan/BookManager/raw/branch/master/screenshots/documents.png" width="33%">
</a>
</p>
---
This is BookManager! Will probably be renamed at some point. This repository contains:
- [KOReader KOSync](https://github.com/koreader/koreader-sync-server) Compatible API
- KOReader Plugin (See `client` subfolder)
- WebApp
In additional to the compatible KOSync API's, we add:
- Additional APIs to automatically upload reading statistics
- Automatically upload documents to the server (can download in the "Documents" view)
- Automatic book cover metadata scraping (Thanks [OpenLibrary](https://openlibrary.org/))
# Development
SQLC Generation:
```
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
~/go/bin/sqlc generate
```
Run Development:
```
CONFIG_PATH=./data DATA_PATH=./data go run cmd/main.go serve
```
## Notes
- Icons: https://www.svgrepo.com/collection/solar-bold-icons

137
api/api.go Normal file
View File

@ -0,0 +1,137 @@
package api
import (
"crypto/rand"
"html/template"
"net/http"
"github.com/gin-contrib/multitemplate"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"reichard.io/bbank/config"
"reichard.io/bbank/database"
"reichard.io/bbank/graph"
)
type API struct {
Router *gin.Engine
Config *config.Config
DB *database.DBManager
}
func NewApi(db *database.DBManager, c *config.Config) *API {
api := &API{
Router: gin.Default(),
Config: c,
DB: db,
}
// Assets & Web App Templates
api.Router.Static("/assets", "./assets")
// Generate Secure Token
newToken, err := generateToken(64)
if err != nil {
panic("Unable to generate secure token")
}
// Configure Cookie Session Store
store := cookie.NewStore(newToken)
store.Options(sessions.Options{
MaxAge: 60 * 60 * 24,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})
api.Router.Use(sessions.Sessions("token", store))
// Register Web App Route
api.registerWebAppRoutes()
// Register API Routes
apiGroup := api.Router.Group("/api")
api.registerKOAPIRoutes(apiGroup)
api.registerWebAPIRoutes(apiGroup)
return api
}
func (api *API) registerWebAppRoutes() {
// Define Templates & Helper Functions
render := multitemplate.NewRenderer()
helperFuncs := template.FuncMap{
"GetSVGGraphData": graph.GetSVGGraphData,
}
render.AddFromFilesFuncs("login", helperFuncs, "templates/login.html")
render.AddFromFilesFuncs("home", helperFuncs, "templates/base.html", "templates/home.html")
render.AddFromFilesFuncs("graphs", helperFuncs, "templates/base.html", "templates/graphs.html")
render.AddFromFilesFuncs("activity", helperFuncs, "templates/base.html", "templates/activity.html")
render.AddFromFilesFuncs("documents", helperFuncs, "templates/base.html", "templates/documents.html")
api.Router.HTMLRender = render
api.Router.GET("/login", api.createAppResourcesRoute("login"))
api.Router.GET("/register", api.createAppResourcesRoute("login", gin.H{"Register": true}))
api.Router.GET("/logout", api.authWebAppMiddleware, api.authLogout)
api.Router.POST("/login", api.authFormLogin)
api.Router.POST("/register", api.authFormRegister)
api.Router.GET("/", api.authWebAppMiddleware, api.createAppResourcesRoute("home"))
api.Router.GET("/documents", api.authWebAppMiddleware, api.createAppResourcesRoute("documents"))
api.Router.GET("/documents/:document/file", api.authWebAppMiddleware, api.downloadDocumentFile)
api.Router.GET("/documents/:document/cover", api.authWebAppMiddleware, api.getDocumentCover)
// TODO
api.Router.GET("/activity", api.authWebAppMiddleware, baseResourceRoute("activity"))
api.Router.GET("/graphs", api.authWebAppMiddleware, baseResourceRoute("graphs"))
}
func (api *API) registerKOAPIRoutes(apiGroup *gin.RouterGroup) {
koGroup := apiGroup.Group("/ko")
koGroup.GET("/info", api.serverInfo)
koGroup.POST("/users/create", api.createUser)
koGroup.GET("/users/auth", api.authAPIMiddleware, api.authorizeUser)
koGroup.PUT("/syncs/progress", api.authAPIMiddleware, api.setProgress)
koGroup.GET("/syncs/progress/:document", api.authAPIMiddleware, api.getProgress)
koGroup.POST("/documents", api.authAPIMiddleware, api.addDocuments)
koGroup.POST("/syncs/documents", api.authAPIMiddleware, api.checkDocumentsSync)
koGroup.PUT("/documents/:document/file", api.authAPIMiddleware, api.uploadDocumentFile)
koGroup.GET("/documents/:document/file", api.authAPIMiddleware, api.downloadDocumentFile)
koGroup.POST("/activity", api.authAPIMiddleware, api.addActivities)
koGroup.POST("/syncs/activity", api.authAPIMiddleware, api.checkActivitySync)
}
func (api *API) registerWebAPIRoutes(apiGroup *gin.RouterGroup) {
v1Group := apiGroup.Group("/v1")
v1Group.GET("/info", api.serverInfo)
v1Group.POST("/users", api.createUser)
v1Group.GET("/users", api.authAPIMiddleware, api.getUsers)
v1Group.POST("/documents", api.authAPIMiddleware, api.checkDocumentsSync)
v1Group.GET("/documents", api.authAPIMiddleware, api.getDocuments)
v1Group.GET("/documents/:document/file", api.authAPIMiddleware, api.downloadDocumentFile)
v1Group.PUT("/documents/:document/file", api.authAPIMiddleware, api.uploadDocumentFile)
v1Group.GET("/activity", api.authAPIMiddleware, api.getActivity)
v1Group.GET("/devices", api.authAPIMiddleware, api.getDevices)
}
func generateToken(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}

169
api/app-routes.go Normal file
View File

@ -0,0 +1,169 @@
package api
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"reichard.io/bbank/database"
"reichard.io/bbank/metadata"
)
func baseResourceRoute(template string, args ...map[string]any) func(c *gin.Context) {
variables := gin.H{"RouteName": template}
if len(args) > 0 {
variables = args[0]
}
return func(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
variables["User"] = rUser
c.HTML(http.StatusOK, template, variables)
}
}
func (api *API) createAppResourcesRoute(routeName string, args ...map[string]any) func(*gin.Context) {
// Merge Optional Template Data
var templateVars = gin.H{}
if len(args) > 0 {
templateVars = args[0]
}
templateVars["RouteName"] = routeName
return func(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
qParams := bindQueryParams(c)
templateVars["User"] = rUser
if routeName == "documents" {
documents, err := api.DB.Queries.GetDocumentsWithStats(api.DB.Ctx, database.GetDocumentsWithStatsParams{
UserID: rUser.(string),
Offset: (*qParams.Page - 1) * *qParams.Limit,
Limit: *qParams.Limit,
})
if err != nil {
log.Info(err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
templateVars["Data"] = documents
} else if routeName == "home" {
weekly_streak, _ := api.DB.Queries.GetUserWindowStreaks(api.DB.Ctx, database.GetUserWindowStreaksParams{
UserID: rUser.(string),
Window: "WEEK",
})
daily_streak, _ := api.DB.Queries.GetUserWindowStreaks(api.DB.Ctx, database.GetUserWindowStreaksParams{
UserID: rUser.(string),
Window: "DAY",
})
database_info, _ := api.DB.Queries.GetDatabaseInfo(api.DB.Ctx, rUser.(string))
read_graph_data, err := api.DB.Queries.GetDailyReadStats(api.DB.Ctx, rUser.(string))
if err != nil {
log.Info("HMMMM:", err)
}
templateVars["Data"] = gin.H{
"DailyStreak": daily_streak,
"WeeklyStreak": weekly_streak,
"DatabaseInfo": database_info,
"GraphData": read_graph_data,
}
}
c.HTML(http.StatusOK, routeName, templateVars)
}
}
func (api *API) getDocumentCover(c *gin.Context) {
var rDoc requestDocumentID
if err := c.ShouldBindUri(&rDoc); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// Validate Document Exists in DB
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// Handle Identified Document
if document.Olid != nil {
if *document.Olid == "UNKNOWN" {
c.Redirect(http.StatusFound, "/assets/no-cover.jpg")
return
}
// Derive Path
fileName := "." + filepath.Clean(fmt.Sprintf("/%s.jpg", *document.Olid))
safePath := filepath.Join(api.Config.DataPath, "covers", fileName)
// Validate File Exists
_, err = os.Stat(safePath)
if err != nil {
c.Redirect(http.StatusFound, "/assets/no-cover.jpg")
return
}
c.File(safePath)
return
}
/*
This is a bit convoluted because we want to ensure we set the OLID to
UNKNOWN if there are any errors. This will ideally prevent us from
hitting the OpenLibrary API multiple times in the future.
*/
var coverID string = "UNKNOWN"
var coverFilePath *string
// Identify Documents & Save Covers
coverIDs, err := metadata.GetCoverIDs(document.Title, document.Author)
if err == nil && len(coverIDs) > 0 {
coverFilePath, err = metadata.DownloadAndSaveCover(coverIDs[0], api.Config.DataPath)
if err == nil {
coverID = coverIDs[0]
}
}
// Upsert Document
if _, err = api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
ID: document.ID,
Olid: &coverID,
}); err != nil {
log.Error("Document Upsert Error")
}
// Return Unknown Cover
if coverID == "UNKNOWN" {
c.Redirect(http.StatusFound, "/assets/no-cover.jpg")
return
}
c.File(*coverFilePath)
}
/*
METADATA:
- Metadata Match
- Update Metadata
*/
/*
GRAPHS:
- Streaks (Daily, Weekly, Monthly)
- Last Week Activity (Daily - Pages & Time)
- Pages Read (Daily, Weekly, Monthly)
- Reading Progress
- Average Reading Time (Daily, Weekly, Monthly)
*/

167
api/auth.go Normal file
View File

@ -0,0 +1,167 @@
package api
import (
"crypto/md5"
"fmt"
"net/http"
"strings"
argon2 "github.com/alexedwards/argon2id"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"reichard.io/bbank/database"
)
type authHeader struct {
AuthUser string `header:"x-auth-user"`
AuthKey string `header:"x-auth-key"`
}
func (api *API) authorizeCredentials(username string, password string) (authorized bool) {
user, err := api.DB.Queries.GetUser(api.DB.Ctx, username)
if err != nil {
return false
}
if match, err := argon2.ComparePasswordAndHash(password, user.Pass); err != nil || match != true {
return false
}
return true
}
func (api *API) authAPIMiddleware(c *gin.Context) {
session := sessions.Default(c)
// Utilize Session Token
if authorizedUser := session.Get("authorizedUser"); authorizedUser != nil {
c.Set("AuthorizedUser", authorizedUser)
c.Next()
return
}
var rHeader authHeader
if err := c.ShouldBindHeader(&rHeader); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Incorrect Headers"})
return
}
if rHeader.AuthUser == "" || rHeader.AuthKey == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid Authorization Headers"})
return
}
if authorized := api.authorizeCredentials(rHeader.AuthUser, rHeader.AuthKey); authorized != true {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
// Set Session Cookie
session.Set("authorizedUser", rHeader.AuthUser)
session.Save()
c.Set("AuthorizedUser", rHeader.AuthUser)
c.Next()
}
func (api *API) authWebAppMiddleware(c *gin.Context) {
session := sessions.Default(c)
// Utilize Session Token
if authorizedUser := session.Get("authorizedUser"); authorizedUser != nil {
c.Set("AuthorizedUser", authorizedUser)
c.Next()
return
}
c.Redirect(http.StatusFound, "/login")
c.Abort()
}
func (api *API) authFormLogin(c *gin.Context) {
username := strings.TrimSpace(c.PostForm("username"))
rawPassword := strings.TrimSpace(c.PostForm("password"))
if username == "" || rawPassword == "" {
c.HTML(http.StatusUnauthorized, "login", gin.H{
"Error": "Invalid Credentials",
})
return
}
password := fmt.Sprintf("%x", md5.Sum([]byte(rawPassword)))
if authorized := api.authorizeCredentials(username, password); authorized != true {
c.HTML(http.StatusUnauthorized, "login", gin.H{
"Error": "Invalid Credentials",
})
return
}
session := sessions.Default(c)
// Set Session Cookie
session.Set("authorizedUser", username)
session.Save()
c.Redirect(http.StatusFound, "/")
}
func (api *API) authLogout(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
c.Redirect(http.StatusFound, "/login")
}
func (api *API) authFormRegister(c *gin.Context) {
username := strings.TrimSpace(c.PostForm("username"))
rawPassword := strings.TrimSpace(c.PostForm("password"))
if username == "" || rawPassword == "" {
c.HTML(http.StatusBadRequest, "login", gin.H{
"Register": true,
"Error": "Registration Disabled or User Already Exists",
})
return
}
password := fmt.Sprintf("%x", md5.Sum([]byte(rawPassword)))
hashedPassword, err := argon2.CreateHash(password, argon2.DefaultParams)
if err != nil {
c.HTML(http.StatusBadRequest, "login", gin.H{
"Register": true,
"Error": "Registration Disabled or User Already Exists",
})
return
}
rows, err := api.DB.Queries.CreateUser(api.DB.Ctx, database.CreateUserParams{
ID: username,
Pass: hashedPassword,
})
// SQL Error
if err != nil {
c.HTML(http.StatusBadRequest, "login", gin.H{
"Register": true,
"Error": "Registration Disabled or User Already Exists",
})
return
}
// User Already Exists
if rows == 0 {
c.HTML(http.StatusBadRequest, "login", gin.H{
"Register": true,
"Error": "Registration Disabled or User Already Exists",
})
return
}
session := sessions.Default(c)
// Set Session Cookie
session.Set("authorizedUser", username)
session.Save()
c.Redirect(http.StatusFound, "/")
}

593
api/ko-routes.go Normal file
View File

@ -0,0 +1,593 @@
package api
import (
"crypto/md5"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
argon2 "github.com/alexedwards/argon2id"
"github.com/gabriel-vasile/mimetype"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"reichard.io/bbank/database"
)
type activityItem struct {
DocumentID string `json:"document"`
StartTime int64 `json:"start_time"`
Duration int64 `json:"duration"`
CurrentPage int64 `json:"current_page"`
TotalPages int64 `json:"total_pages"`
}
type requestActivity struct {
DeviceID string `json:"device_id"`
Device string `json:"device"`
Activity []activityItem `json:"activity"`
}
type requestCheckActivitySync struct {
DeviceID string `json:"device_id"`
}
type requestDocument struct {
Documents []database.Document `json:"documents"`
}
type requestPosition struct {
DocumentID string `json:"document"`
Percentage float64 `json:"percentage"`
Progress string `json:"progress"`
Device string `json:"device"`
DeviceID string `json:"device_id"`
}
type requestUser struct {
Username string `json:"username"`
Password string `json:"password"`
}
type requestCheckDocumentSync struct {
DeviceID string `json:"device_id"`
Device string `json:"device"`
Have []string `json:"have"`
}
type responseCheckDocumentSync struct {
Want []string `json:"want"`
Give []database.Document `json:"give"`
Delete []string `json:"deleted"`
}
type requestDocumentID struct {
DocumentID string `uri:"document" binding:"required"`
}
var allowedExtensions []string = []string{".epub", ".html"}
func (api *API) authorizeUser(c *gin.Context) {
c.JSON(200, gin.H{
"authorized": "OK",
})
}
func (api *API) createUser(c *gin.Context) {
var rUser requestUser
if err := c.ShouldBindJSON(&rUser); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
return
}
if rUser.Username == "" || rUser.Password == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
return
}
hashedPassword, err := argon2.CreateHash(rUser.Password, argon2.DefaultParams)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
return
}
// TODO - Initial User is Admin & Enable / Disable Registration
rows, err := api.DB.Queries.CreateUser(api.DB.Ctx, database.CreateUserParams{
ID: rUser.Username,
Pass: hashedPassword,
})
// SQL Error
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid User Data"})
return
}
// User Exists (ON CONFLICT DO NOTHING)
if rows == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "User Already Exists"})
return
}
// TODO: Struct -> JSON
c.JSON(http.StatusCreated, gin.H{
"username": rUser.Username,
})
}
func (api *API) setProgress(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rPosition requestPosition
if err := c.ShouldBindJSON(&rPosition); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Progress Data"})
return
}
// Upsert Device
device, err := api.DB.Queries.UpsertDevice(api.DB.Ctx, database.UpsertDeviceParams{
ID: rPosition.DeviceID,
UserID: rUser.(string),
DeviceName: rPosition.Device,
})
if err != nil {
log.Error("Device Upsert Error:", device, err)
}
// Upsert Document
document, err := api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
ID: rPosition.DocumentID,
})
if err != nil {
log.Error("Document Upsert Error:", document, err)
}
// Create or Replace Progress
progress, err := api.DB.Queries.UpdateProgress(api.DB.Ctx, database.UpdateProgressParams{
Percentage: rPosition.Percentage,
DocumentID: rPosition.DocumentID,
DeviceID: rPosition.DeviceID,
UserID: rUser.(string),
Progress: rPosition.Progress,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
// TODO: Struct -> JSON
c.JSON(http.StatusOK, gin.H{
"document": progress.DocumentID,
"timestamp": progress.CreatedAt,
})
}
func (api *API) getProgress(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rDocID requestDocumentID
if err := c.ShouldBindUri(&rDocID); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
progress, err := api.DB.Queries.GetProgress(api.DB.Ctx, database.GetProgressParams{
DocumentID: rDocID.DocumentID,
UserID: rUser.(string),
})
if err != nil {
log.Error("Invalid Progress:", progress, err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
// TODO: Struct -> JSON
c.JSON(http.StatusOK, gin.H{
"document": progress.DocumentID,
"percentage": progress.Percentage,
"progress": progress.Progress,
"device": progress.DeviceName,
"device_id": progress.DeviceID,
})
}
func (api *API) addActivities(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rActivity requestActivity
if err := c.ShouldBindJSON(&rActivity); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
return
}
// Do Transaction
tx, err := api.DB.DB.Begin()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
return
}
// Derive Unique Documents
allDocumentsMap := make(map[string]bool)
for _, item := range rActivity.Activity {
allDocumentsMap[item.DocumentID] = true
}
allDocuments := getKeys(allDocumentsMap)
// Defer & Start Transaction
defer tx.Rollback()
qtx := api.DB.Queries.WithTx(tx)
// Upsert Documents
for _, doc := range allDocuments {
_, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
ID: doc,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
}
// Upsert Device
_, err = qtx.UpsertDevice(api.DB.Ctx, database.UpsertDeviceParams{
ID: rActivity.DeviceID,
UserID: rUser.(string),
DeviceName: rActivity.Device,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
return
}
// Add All Activity
for _, item := range rActivity.Activity {
_, err := qtx.AddActivity(api.DB.Ctx, database.AddActivityParams{
UserID: rUser.(string),
DocumentID: item.DocumentID,
DeviceID: rActivity.DeviceID,
StartTime: time.Unix(int64(item.StartTime), 0).UTC(),
Duration: int64(item.Duration),
CurrentPage: int64(item.CurrentPage),
TotalPages: int64(item.TotalPages),
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Activity"})
return
}
}
// Commit Transaction
tx.Commit()
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
c.JSON(http.StatusOK, gin.H{
"added": len(rActivity.Activity),
})
}
func (api *API) checkActivitySync(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rCheckActivity requestCheckActivitySync
if err := c.ShouldBindJSON(&rCheckActivity); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
// Get Last Device Activity
lastActivity, err := api.DB.Queries.GetLastActivity(api.DB.Ctx, database.GetLastActivityParams{
UserID: rUser.(string),
DeviceID: rCheckActivity.DeviceID,
})
if err == sql.ErrNoRows {
lastActivity = time.UnixMilli(0)
} else if err != nil {
log.Error("GetLastActivity Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
return
}
c.JSON(http.StatusOK, gin.H{
"last_sync": lastActivity.Unix(),
})
}
func (api *API) addDocuments(c *gin.Context) {
var rNewDocs requestDocument
if err := c.ShouldBindJSON(&rNewDocs); err != nil {
log.Error("[addDocuments] Invalid JSON Bind")
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document(s)"})
return
}
// Do Transaction
tx, err := api.DB.DB.Begin()
if err != nil {
log.Error("[addDocuments] Unknown Transaction Error")
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Error"})
return
}
// Defer & Start Transaction
defer tx.Rollback()
qtx := api.DB.Queries.WithTx(tx)
// Upsert Documents
for _, doc := range rNewDocs.Documents {
doc, err := qtx.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
ID: doc.ID,
Title: doc.Title,
Author: doc.Author,
Series: doc.Series,
SeriesIndex: doc.SeriesIndex,
Lang: doc.Lang,
Description: doc.Description,
})
if err != nil {
log.Error("[addDocuments] UpsertDocument Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
_, err = qtx.UpdateDocumentSync(api.DB.Ctx, database.UpdateDocumentSyncParams{
ID: doc.ID,
Synced: true,
})
if err != nil {
log.Error("[addDocuments] UpsertDocumentSync Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
}
// Commit Transaction
tx.Commit()
c.JSON(http.StatusOK, gin.H{
"changed": len(rNewDocs.Documents),
})
}
func (api *API) checkDocumentsSync(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rCheckDocs requestCheckDocumentSync
if err := c.ShouldBindJSON(&rCheckDocs); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
// Upsert Device
device, err := api.DB.Queries.UpsertDevice(api.DB.Ctx, database.UpsertDeviceParams{
ID: rCheckDocs.DeviceID,
UserID: rUser.(string),
DeviceName: rCheckDocs.Device,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Device"})
return
}
missingDocs := []database.Document{}
deletedDocIDs := []string{}
if device.Sync == true {
// Get Missing Documents
missingDocs, err = api.DB.Queries.GetMissingDocuments(api.DB.Ctx, rCheckDocs.Have)
if err != nil {
log.Error("GetMissingDocuments Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
// Get Deleted Documents
deletedDocIDs, err = api.DB.Queries.GetDeletedDocuments(api.DB.Ctx, rCheckDocs.Have)
if err != nil {
log.Error("GetDeletedDocuements Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
}
// Get Wanted Documents
jsonHaves, err := json.Marshal(rCheckDocs.Have)
if err != nil {
log.Error("JSON Marshal Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
wantedDocIDs, err := api.DB.Queries.GetWantedDocuments(api.DB.Ctx, string(jsonHaves))
if err != nil {
log.Error("GetWantedDocuments Error:", err)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
rCheckDocSync := responseCheckDocumentSync{
Delete: []string{},
Want: []string{},
Give: []database.Document{},
}
// Ensure Empty Array
if wantedDocIDs != nil {
rCheckDocSync.Want = wantedDocIDs
}
if missingDocs != nil {
rCheckDocSync.Give = missingDocs
}
if deletedDocIDs != nil {
rCheckDocSync.Delete = deletedDocIDs
}
c.JSON(http.StatusOK, rCheckDocSync)
}
func (api *API) uploadDocumentFile(c *gin.Context) {
var rDoc requestDocumentID
if err := c.ShouldBindUri(&rDoc); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
fileData, err := c.FormFile("file")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
return
}
// Validate Type & Derive Extension on MIME
uploadedFile, err := fileData.Open()
fileMime, err := mimetype.DetectReader(uploadedFile)
fileExtension := fileMime.Extension()
if !slices.Contains(allowedExtensions, fileExtension) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Filetype"})
return
}
// Validate Document Exists in DB
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
return
}
// Derive Filename
var fileName string
if document.Author != nil {
fileName = fileName + *document.Author
} else {
fileName = fileName + "Unknown"
}
if document.Title != nil {
fileName = fileName + " - " + *document.Title
} else {
fileName = fileName + " - Unknown"
}
// Derive & Sanitize File Name
fileName = "." + filepath.Clean(fmt.Sprintf("/%s [%s]%s", fileName, document.ID, fileExtension))
// Generate Storage Path
safePath := filepath.Join(api.Config.DataPath, "documents", fileName)
// Save & Prevent Overwrites
_, err = os.Stat(safePath)
if os.IsNotExist(err) {
err = c.SaveUploadedFile(fileData, safePath)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
return
}
}
// Get MD5 Hash
fileHash, err := getFileMD5(safePath)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "File Error"})
return
}
// Upsert Document
_, err = api.DB.Queries.UpsertDocument(api.DB.Ctx, database.UpsertDocumentParams{
ID: document.ID,
Md5: fileHash,
Filepath: &fileName,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Error"})
return
}
// Update Document Sync Attribute
_, err = api.DB.Queries.UpdateDocumentSync(api.DB.Ctx, database.UpdateDocumentSyncParams{
ID: document.ID,
Synced: true,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Document"})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
func (api *API) downloadDocumentFile(c *gin.Context) {
var rDoc requestDocumentID
if err := c.ShouldBindUri(&rDoc); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
// Get Document
document, err := api.DB.Queries.GetDocument(api.DB.Ctx, rDoc.DocumentID)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unknown Document"})
return
}
if document.Filepath == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exist"})
return
}
// Derive Storage Location
filePath := filepath.Join(api.Config.DataPath, "documents", *document.Filepath)
// Validate File Exists
_, err = os.Stat(filePath)
if os.IsNotExist(err) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Document Doesn't Exists"})
return
}
// Force Download (Security)
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filepath.Base(*document.Filepath)))
c.File(filePath)
}
func getKeys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func getFileMD5(filePath string) (*string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return nil, err
}
fileHash := fmt.Sprintf("%x", hash.Sum(nil))
return &fileHash, nil
}

163
api/web-routes.go Normal file
View File

@ -0,0 +1,163 @@
package api
import (
"net/http"
argon2 "github.com/alexedwards/argon2id"
"github.com/gin-gonic/gin"
"reichard.io/bbank/database"
)
type infoResponse struct {
Authorized bool `json:"authorized"`
Version string `json:"version"`
}
type queryParams struct {
Page *int64 `form:"page"`
Limit *int64 `form:"limit"`
Document *string `form:"document"`
}
func bindQueryParams(c *gin.Context) queryParams {
var qParams queryParams
c.BindQuery(&qParams)
if qParams.Limit == nil {
var defaultValue int64 = 50
qParams.Limit = &defaultValue
} else if *qParams.Limit < 0 {
var zeroValue int64 = 0
qParams.Limit = &zeroValue
}
if qParams.Page == nil || *qParams.Page < 1 {
var oneValue int64 = 0
qParams.Page = &oneValue
}
return qParams
}
func (api *API) serverInfo(c *gin.Context) {
respData := infoResponse{
Authorized: false,
Version: api.Config.Version,
}
var rHeader authHeader
if err := c.ShouldBindHeader(&rHeader); err != nil {
c.JSON(200, respData)
return
}
if rHeader.AuthUser == "" || rHeader.AuthKey == "" {
c.JSON(200, respData)
return
}
user, err := api.DB.Queries.GetUser(api.DB.Ctx, rHeader.AuthUser)
if err != nil {
c.JSON(200, respData)
return
}
match, err := argon2.ComparePasswordAndHash(rHeader.AuthKey, user.Pass)
if err != nil || match != true {
c.JSON(200, respData)
return
}
respData.Authorized = true
c.JSON(200, respData)
}
func (api *API) getDocuments(c *gin.Context) {
qParams := bindQueryParams(c)
documents, err := api.DB.Queries.GetDocuments(api.DB.Ctx, database.GetDocumentsParams{
Offset: (*qParams.Page - 1) * *qParams.Limit,
Limit: *qParams.Limit,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
if documents == nil {
documents = []database.Document{}
}
c.JSON(http.StatusOK, documents)
}
func (api *API) getUsers(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
qParams := bindQueryParams(c)
users, err := api.DB.Queries.GetUsers(api.DB.Ctx, database.GetUsersParams{
User: rUser.(string),
Offset: (*qParams.Page - 1) * *qParams.Limit,
Limit: *qParams.Limit,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
if users == nil {
users = []database.User{}
}
c.JSON(http.StatusOK, users)
}
func (api *API) getActivity(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
qParams := bindQueryParams(c)
dbActivityParams := database.GetActivityParams{
UserID: rUser.(string),
DocFilter: false,
DocumentID: "",
Offset: (*qParams.Page - 1) * *qParams.Limit,
Limit: *qParams.Limit,
}
if qParams.Document != nil {
dbActivityParams.DocFilter = true
dbActivityParams.DocumentID = *qParams.Document
}
activity, err := api.DB.Queries.GetActivity(api.DB.Ctx, dbActivityParams)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
if activity == nil {
activity = []database.Activity{}
}
c.JSON(http.StatusOK, activity)
}
func (api *API) getDevices(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
qParams := bindQueryParams(c)
devices, err := api.DB.Queries.GetDevices(api.DB.Ctx, database.GetDevicesParams{
UserID: rUser.(string),
Offset: (*qParams.Page - 1) * *qParams.Limit,
Limit: *qParams.Limit,
})
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Request"})
return
}
if devices == nil {
devices = []database.Device{}
}
c.JSON(http.StatusOK, devices)
}

BIN
assets/book1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 511 KiB

BIN
assets/book2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 699 KiB

BIN
assets/book3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
assets/book4.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

BIN
assets/book55.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 473 KiB

BIN
assets/no-cover.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

View File

@ -0,0 +1,316 @@
local UIManager = require("ui/uimanager")
local socketutil = require("socketutil")
local logger = require("logger")
-- Push/Pull
local SYNC_TIMEOUTS = {2, 5}
-- Login/Register
local AUTH_TIMEOUTS = {5, 10}
local SyncNinjaClient = {service_spec = nil, custom_url = nil}
function SyncNinjaClient:new(o)
if o == nil then o = {} end
setmetatable(o, self)
self.__index = self
if o.init then o:init() end
return o
end
function SyncNinjaClient:init()
local Spore = require("Spore")
self.client = Spore.new_from_spec(self.service_spec,
{base_url = self.custom_url})
package.loaded["Spore.Middleware.GinClient"] = {}
require("Spore.Middleware.GinClient").call = function(_, req)
req.headers["accept"] = "application/vnd.koreader.v1+json"
end
package.loaded["Spore.Middleware.SyncNinjaAuth"] = {}
require("Spore.Middleware.SyncNinjaAuth").call = function(args, req)
req.headers["x-auth-user"] = args.username
req.headers["x-auth-key"] = args.userkey
end
package.loaded["Spore.Middleware.AsyncHTTP"] = {}
require("Spore.Middleware.AsyncHTTP").call = function(args, req)
-- disable async http if Turbo looper is missing
if not UIManager.looper then return end
req:finalize()
local result
local turbo = require("turbo")
turbo.log.categories.success = false
turbo.log.categories.warning = false
local client = turbo.async.HTTPClient({verify_ca = false})
local res = coroutine.yield(client:fetch(request.url, {
url = req.url,
method = req.method,
body = req.env.spore.payload,
connect_timeout = 10,
request_timeout = 20,
on_headers = function(headers)
for header, value in pairs(req.headers) do
if type(header) == "string" then
headers:add(header, value)
end
end
end
}))
return res
-- return coroutine.create(function() coroutine.yield(result) end)
end
end
------------------------------------------
-------------- New Functions -------------
------------------------------------------
function SyncNinjaClient:check_activity(username, password, device_id, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:check_activity({device_id = device_id})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:check_activity failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
function SyncNinjaClient:add_activity(username, password, device_id, device,
activity, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:add_activity({
device_id = device_id,
device = device,
activity = activity
})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:add_activity failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
function SyncNinjaClient:add_documents(username, password, documents, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:add_documents({documents = documents})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:add_documents failure:", res)(
"SyncNinjaClient:add_documents failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
function SyncNinjaClient:check_documents(username, password, device_id, device,
have, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:check_documents({
device_id = device_id,
device = device,
have = have
})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:check_documents failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
function SyncNinjaClient:download_document(username, password, document,
callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
local ok, res = pcall(function()
return self.client:download_document({document = document})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:download_document failure:", res)
callback(false, res.body)
end
end
function SyncNinjaClient:upload_document(username, password, document, file,
callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
local ok, res = pcall(function()
return self.client:upload_document({document = document, file = file})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:upload_document failure:", res)
callback(false, res.body)
end
end
------------------------------------------
----------- Existing Functions -----------
------------------------------------------
function SyncNinjaClient:register(username, password)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
local ok, res = pcall(function()
return self.client:register({username = username, password = password})
end)
socketutil:reset_timeout()
if ok then
return res.status == 201, res.body
else
logger.dbg("SyncNinjaClient:register failure:", res)
return false, res.body
end
end
function SyncNinjaClient:authorize(username, password)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
local ok, res = pcall(function() return self.client:authorize() end)
socketutil:reset_timeout()
if ok then
return res.status == 200, res.body
else
logger.dbg("SyncNinjaClient:authorize failure:", res)
return false, res.body
end
end
function SyncNinjaClient:update_progress(username, password, document, progress,
percentage, device, device_id, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:update_progress({
document = document,
progress = tostring(progress),
percentage = percentage,
device = device,
device_id = device_id
})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:update_progress failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
function SyncNinjaClient:get_progress(username, password, document, callback)
self.client:reset_middlewares()
self.client:enable("Format.JSON")
self.client:enable("GinClient")
self.client:enable("SyncNinjaAuth",
{username = username, userkey = password})
socketutil:set_timeout(SYNC_TIMEOUTS[1], SYNC_TIMEOUTS[2])
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:get_progress({document = document})
end)
if ok then
callback(res.status == 200, res.body)
else
logger.dbg("SyncNinjaClient:get_progress failure:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
socketutil:reset_timeout()
end
return SyncNinjaClient

View File

@ -0,0 +1,6 @@
local _ = require("gettext")
return {
name = "syncninja",
fullname = _("Additional sync capabilities"),
description = _([[Syncs your documents and activity to an altered server.]])
}

View File

@ -0,0 +1,61 @@
{
"base_url": "http://127.0.0.1:8585",
"name": "syncninja",
"methods": {
"add_activity": {
"path": "/api/ko/activity",
"method": "POST",
"required_params": ["device_id", "device", "activity"],
"payload": ["device_id", "device", "activity"],
"expected_status": [200, 401]
},
"add_documents": {
"path": "/api/ko/documents",
"method": "POST",
"required_params": ["documents"],
"payload": ["documents"],
"expected_status": [200, 401]
},
"check_documents": {
"path": "/api/ko/syncs/documents",
"method": "POST",
"required_params": ["device_id", "device", "have"],
"payload": ["device_id", "device", "have"],
"expected_status": [200, 401]
},
"check_activity": {
"path": "/api/ko/syncs/activity",
"method": "POST",
"required_params": ["device_id"],
"payload": ["device_id"],
"expected_status": [200, 401]
},
"download_document": {
"path": "/api/ko/documents/:document/file",
"method": "GET",
"required_params": ["document"],
"expected_status": [200, 401]
},
"upload_document": {
"path": "/api/ko/documents/:document/file",
"method": "PUT",
"required_params": ["document", "file"],
"form-data": {
"file": "@:file"
},
"expected_status": [200, 401]
},
"authorize": {
"path": "/api/ko/users/auth",
"method": "GET",
"expected_status": [200, 401]
},
"register": {
"path": "/api/ko/users/create",
"method": "POST",
"required_params": ["username", "password"],
"payload": ["username", "password"],
"expected_status": [201, 402]
}
}
}

View File

@ -0,0 +1,953 @@
local DataStorage = require("datastorage")
local Device = require("device")
local Dispatcher = require("dispatcher")
local DocSettings = require("docsettings")
local InfoMessage = require("ui/widget/infomessage")
local MultiInputDialog = require("ui/widget/multiinputdialog")
local NetworkMgr = require("ui/network/manager")
local ReadHistory = require("readhistory")
local SQ3 = require("lua-ljsqlite3/init")
local T = require("ffi/util").template
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local logger = require("logger")
local md5 = require("ffi/sha2").md5
-- TODO:
-- - Handle ReadHistory missing files (statistics.sqlite3, bookinfo_cache.sqlite3)
-- - Handle document uploads (Manual push only, warning saying this may take awhile)
-- - Configure activity bulk size? 1000, 5000, 10000? Separate manual settings to upload ALL?
------------------------------------------
------------ Helper Functions ------------
------------------------------------------
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then k = '"' .. k .. '"' end
s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
local function validate(entry)
if not entry then return false end
if type(entry) == "string" then
if entry == "" or not entry:match("%S") then return false end
end
return true
end
local function validateUser(user, pass)
local error_message = nil
local user_ok = validate(user)
local pass_ok = validate(pass)
if not user_ok and not pass_ok then
error_message = _("invalid username and password")
elseif not user_ok then
error_message = _("invalid username")
elseif not pass_ok then
error_message = _("invalid password")
end
if not error_message then
return user_ok and pass_ok
else
return user_ok and pass_ok, error_message
end
end
------------------------------------------
-------------- Plugin Start --------------
------------------------------------------
local MERGE_SETTINGS_IN = "IN"
local MERGE_SETTINGS_OUT = "OUT"
local STATISTICS_ACTIVITY_SINCE_QUERY = [[
SELECT
b.md5 AS document,
psd.start_time AS start_time,
psd.duration AS duration,
psd.page AS current_page,
psd.total_pages
FROM page_stat_data AS psd
JOIN book AS b
ON b.id = psd.id_book
WHERE start_time > %d
ORDER BY start_time ASC LIMIT 1000;
]]
local STATISTICS_BOOK_QUERY = [[
SELECT
md5,
title,
authors,
series,
language
FROM book;
]]
local BOOKINFO_BOOK_QUERY = [[
SELECT
(directory || filename) as filepath,
title,
authors,
series,
series_index,
language,
description
FROM bookinfo;
]]
-- Validate Device ID Exists
if G_reader_settings:hasNot("device_id") then
G_reader_settings:saveSetting("device_id", random.uuid())
end
-- Define DB Location
local statistics_db = DataStorage:getSettingsDir() .. "/statistics.sqlite3"
local bookinfo_db = DataStorage:getSettingsDir() .. "/bookinfo_cache.sqlite3"
local SyncNinja = WidgetContainer:extend{
name = "syncninja",
settings = nil,
is_doc_only = false
}
SyncNinja.default_settings = {
server = nil,
username = nil,
password = nil,
sync_frequency = 30,
sync_activity = true,
sync_documents = true,
sync_document_files = true
}
function SyncNinja:init()
logger.dbg("SyncNinja: init")
-- Instance Specific (Non Interactive)
self.periodic_push_task = function() self:performSync(false) end
-- Load Settings
self.device_id = G_reader_settings:readSetting("device_id")
self.settings = G_reader_settings:readSetting("syncninja",
self.default_settings)
-- Register Menu Items
self.ui.menu:registerToMainMenu(self)
-- Initial Periodic Push Schedule (5 Minutes)
self:schedulePeriodicPush(5)
end
------------------------------------------
-------------- UX Functions --------------
------------------------------------------
function SyncNinja:addToMainMenu(menu_items)
logger.dbg("SyncNinja: addToMainMenu")
menu_items.syncninja = {
text = _("Sync Ninja"),
sorting_hint = "tools",
sub_item_table = {
{
text = _("Sync Server"),
keep_menu_open = true,
tap_input_func = function(menu)
return {
title = _("Sync server address"),
input = self.settings.server or "https://",
type = "text",
callback = function(input)
self.settings.server = input ~= "" and input or nil
if menu then
menu:updateItems()
end
end
}
end
}, {
text_func = function()
return self.settings.password and (_("Logout")) or
_("Register") .. " / " .. _("Login")
end,
enabled_func = function()
return self.settings.server ~= nil
end,
keep_menu_open = true,
callback_func = function()
if self.settings.password then
return function(menu)
self:logoutUI(menu)
end
else
return function(menu)
self:loginUI(menu)
end
end
end
}, {
text = _("Manual Sync"),
keep_menu_open = true,
enabled_func = function()
return self.settings.password ~= nil and
self.settings.username ~= nil and
self.settings.server ~= nil
end,
callback = function()
UIManager:unschedule(self.performSync)
self:performSync(true) -- Interactive
end
}, {
text = _("KOSync Auth Merge"),
sub_item_table = {
{
text = _("KOSync Merge In"),
keep_menu_open = true,
callback = function()
self:mergeKOSync(MERGE_SETTINGS_IN)
end
}, {
text = _("KOSync Merge Out"),
keep_menu_open = true,
callback = function()
self:mergeKOSync(MERGE_SETTINGS_OUT)
end
}
},
separator = true
}, {
text_func = function()
return T(_("Sync Frequency (%1 Minutes)"),
self.settings.sync_frequency or 30)
end,
keep_menu_open = true,
callback = function(touchmenu_instance)
local SpinWidget = require("ui/widget/spinwidget")
local items = SpinWidget:new{
text = _(
[[This value determines the cadence at which the syncs will be performed.
If set to 0, periodic sync will be disabled.]]),
value = self.settings.sync_frequency or 30,
value_min = 0,
value_max = 1440,
value_step = 30,
value_hold_step = 60,
ok_text = _("Set"),
title_text = _("Minutes between syncs"),
default_value = 30,
callback = function(spin)
self.settings.sync_frequency = spin.value > 0 and
spin.value or 30
if touchmenu_instance then
touchmenu_instance:updateItems()
end
self:schedulePeriodicPush()
end
}
UIManager:show(items)
end
}, {
text_func = function()
return T(_("Sync Activity (%1)"), self.settings
.sync_activity == true and (_("Enabled")) or
(_("Disabled")))
end,
sub_item_table = {
{
text = _("Enabled"),
checked_func = function()
return self.settings.sync_activity == true
end,
callback = function()
self.settings.sync_activity = true
end
}, {
text = _("Disabled"),
checked_func = function()
return self.settings.sync_activity ~= true
end,
callback = function()
self.settings.sync_activity = false
end
}
}
}, {
text_func = function()
return T(_("Sync Documents (%1)"), self.settings
.sync_documents == true and (_("Enabled")) or
(_("Disabled")))
end,
sub_item_table = {
{
text = _("Enabled"),
checked_func = function()
return self.settings.sync_documents == true
end,
callback = function()
self.settings.sync_documents = true
end
}, {
text = _("Disabled"),
checked_func = function()
return self.settings.sync_documents ~= true
end,
callback = function()
self.settings.sync_documents = false
end
}
}
}, {
text_func = function()
return T(_("Sync Document Files (%1)"),
self.settings.sync_documents == true and
self.settings.sync_document_files == true and
(_("Enabled")) or (_("Disabled")))
end,
enabled_func = function()
return self.settings.sync_documents == true
end,
sub_item_table = {
{
text = _("Enabled"),
checked_func = function()
return self.settings.sync_document_files == true
end,
callback = function()
self.settings.sync_document_files = true
end
}, {
text = _("Disabled"),
checked_func = function()
return self.settings.sync_document_files ~= true
end,
callback = function()
self.settings.sync_document_files = false
end
}
}
}
}
}
end
function SyncNinja:loginUI(menu)
logger.dbg("SyncNinja: loginUI")
if NetworkMgr:willRerunWhenOnline(function() self:loginUI(menu) end) then
return
end
local dialog
dialog = MultiInputDialog:new{
title = _("Register/login to SyncNinja server"),
fields = {
{text = self.settings.username, hint = "username"},
{hint = "password", text_type = "password"}
},
buttons = {
{
{
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(dialog)
end
}, {
text = _("Login"),
callback = function()
local username, password = unpack(dialog:getFields())
local ok, err = validateUser(username, password)
if not ok then
UIManager:show(InfoMessage:new{
text = T(_("Cannot login: %1"), err),
timeout = 2
})
else
UIManager:close(dialog)
UIManager:scheduleIn(0.5, function()
self:userLogin(username, password, menu)
end)
UIManager:show(InfoMessage:new{
text = _("Logging in. Please wait…"),
timeout = 1
})
end
end
}, {
text = _("Register"),
callback = function()
local username, password = unpack(dialog:getFields())
local ok, err = validateUser(username, password)
if not ok then
UIManager:show(InfoMessage:new{
text = T(_("Cannot register: %1"), err),
timeout = 2
})
else
UIManager:close(dialog)
UIManager:scheduleIn(0.5, function()
self:userRegister(username, password, menu)
end)
UIManager:show(InfoMessage:new{
text = _("Registering. Please wait…"),
timeout = 1
})
end
end
}
}
}
}
UIManager:show(dialog)
dialog:onShowKeyboard()
end
function SyncNinja:logoutUI(menu)
logger.dbg("SyncNinja: logoutUI")
self.settings.username = nil
self.settings.password = nil
if menu then menu:updateItems() end
UIManager:unschedule(self.periodic_push_task)
end
function SyncNinja:mergeKOSync(direction)
logger.dbg("SyncNinja: mergeKOSync")
local kosync_settings = G_reader_settings:readSetting("kosync")
if kosync_settings == nil then return end
if direction == MERGE_SETTINGS_OUT then
-- Validate Configured
if not self.settings.server or not self.settings.username or
not self.settings.password then
return UIManager:show(InfoMessage:new{
text = _("Error: SyncNinja not configured")
})
end
kosync_settings.custom_server = self.settings.server ..
(self.settings.server:sub(-#"/") ==
"/" and "api/ko" or "/api/ko")
kosync_settings.username = self.settings.username
kosync_settings.userkey = self.settings.password
UIManager:show(InfoMessage:new{text = _("Synced to KOSync")})
elseif direction == MERGE_SETTINGS_IN then
-- Validate Configured
if not kosync_settings.custom_server or not kosync_settings.username or
not kosync_settings.userkey then
return UIManager:show(InfoMessage:new{
text = _("Error: KOSync not configured")
})
end
-- Validate Compatible Server
if kosync_settings.custom_server:sub(-#"/api/ko") ~= "/api/ko" and
kosync_settings.custom_server:sub(-#"/api/ko/") ~= "/api/ko/" then
return UIManager:show(InfoMessage:new{
text = _("Error: Configured KOSync server not compatible")
})
end
self.settings.server = string.gsub(kosync_settings.custom_server,
"/api/ko/?$", "")
self.settings.username = kosync_settings.username
self.settings.password = kosync_settings.userkey
UIManager:show(InfoMessage:new{text = _("Synced from KOSync")})
end
end
------------------------------------------
------------- Login Functions ------------
------------------------------------------
function SyncNinja:userLogin(username, password, menu)
logger.dbg("SyncNinja: userLogin")
if not self.settings.server then return end
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
Device:setIgnoreInput(true)
local userkey = md5(password)
local ok, status, body = pcall(client.authorize, client, username, userkey)
if not ok then
if status then
UIManager:show(InfoMessage:new{
text = _("An error occurred while logging in:") .. "\n" ..
status
})
else
UIManager:show(InfoMessage:new{
text = _("An unknown error occurred while logging in.")
})
end
Device:setIgnoreInput(false)
return
elseif status then
self.settings.username = username
self.settings.password = userkey
if menu then menu:updateItems() end
UIManager:show(InfoMessage:new{
text = _("Logged in to KOReader server.")
})
self:schedulePeriodicPush(0)
else
logger.dbg("SyncNinja: userLogin Error:", dump(body))
end
Device:setIgnoreInput(false)
end
function SyncNinja:userRegister(username, password, menu)
logger.dbg("SyncNinja: userRegister")
if not self.settings.server then return end
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
-- on Android to avoid ANR (no-op on other platforms)
Device:setIgnoreInput(true)
local userkey = md5(password)
local ok, status, body = pcall(client.register, client, username, userkey)
if not ok then
if status then
UIManager:show(InfoMessage:new{
text = _("An error occurred while registering:") .. "\n" ..
status
})
else
UIManager:show(InfoMessage:new{
text = _("An unknown error occurred while registering.")
})
end
elseif status then
self.settings.username = username
self.settings.password = userkey
if menu then menu:updateItems() end
UIManager:show(InfoMessage:new{
text = _("Registered to KOReader server.")
})
self:schedulePeriodicPush(0)
else
UIManager:show(InfoMessage:new{
text = body and body.message or _("Unknown server error")
})
end
Device:setIgnoreInput(false)
end
------------------------------------------
------------- Sync Functions -------------
------------------------------------------
function SyncNinja:schedulePeriodicPush(minutes)
logger.dbg("SyncNinja: schedulePeriodicPush")
-- Validate Configured
if not self.settings then return end
if not self.settings.username then return end
if not self.settings.password then return end
if not self.settings.server then return end
-- Unschedule & Schedule
local sync_frequency = minutes or self.settings.sync_frequency or 30
UIManager:unschedule(self.periodic_push_task)
UIManager:scheduleIn(60 * sync_frequency, self.periodic_push_task)
end
function SyncNinja:performSync(interactive)
logger.dbg("SyncNinja: performSync")
-- Upload Activity & Check Documents
self:checkActivity(interactive)
self:checkDocuments(interactive)
if interactive == true then
UIManager:show(InfoMessage:new{
text = _("SyncNinja: Manual Sync Success"),
timeout = 3
})
end
-- Schedule Push Again
self:schedulePeriodicPush()
end
function SyncNinja:checkActivity(interactive)
logger.dbg("SyncNinja: checkActivity")
-- Ensure Activity Sync Enabled
if self.settings.sync_activity ~= true then return end
-- API Callback Function
local callback_func = function(ok, body)
if not ok then
-- TODO: if interactive
UIManager:show(InfoMessage:new{
text = _("SyncNinja: checkActivity Error"),
timeout = 3
})
return logger.dbg("SyncNinja: checkActivity Error:", dump(body))
end
local last_sync = body.last_sync
local activity_data = self:getStatisticsActivity(last_sync)
-- Activity Data Exists
if not (next(activity_data) == nil) then
self:uploadActivity(activity_data, interactive)
end
end
-- API Call
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
local ok, err = pcall(client.check_activity, client, self.settings.username,
self.settings.password, self.device_id, callback_func)
end
function SyncNinja:uploadActivity(activity_data, interactive)
logger.dbg("SyncNinja: uploadActivity")
-- API Callback Function
local callback_func = function(ok, body)
if not ok then
-- TODO: if interactive
UIManager:show(InfoMessage:new{
text = _("SyncNinja: uploadActivity Error"),
timeout = 3
})
return logger.dbg("SyncNinja: uploadActivity Error:", dump(body))
end
end
-- API Call
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
local ok, err = pcall(client.add_activity, client, self.settings.username,
self.settings.password, self.device_id, Device.model,
activity_data, callback_func)
end
function SyncNinja:checkDocuments(interactive)
logger.dbg("SyncNinja: checkDocuments")
-- ensure document sync enabled
if self.settings.sync_documents ~= true then return end
-- API Request Data
local doc_metadata = self:getLocalDocumentMetadata()
local doc_ids = self:getLocalDocumentIDs(doc_metadata)
-- API Callback Function
local callback_func = function(ok, body)
if not ok then
-- TODO: if interactive
UIManager:show(InfoMessage:new{
text = _("SyncNinja: checkDocuments Error"),
timeout = 3
})
return logger.dbg("SyncNinja: checkDocuments Error:", dump(body))
end
-- Documents Wanted
if not (next(body.want) == nil) then
local hash_want = {}
for _, v in pairs(body.want) do hash_want[v] = true end
local upload_doc_metadata = {}
for _, v in pairs(doc_metadata) do
if hash_want[v.id] == true then
table.insert(upload_doc_metadata, v)
end
end
self:uploadDocuments(upload_doc_metadata, interactive)
end
-- Documents Provided
if not (next(body.give) == nil) then
self:downloadDocuments(body.give, interactive)
end
end
-- API Call
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
local ok, err = pcall(client.check_documents, client,
self.settings.username, self.settings.password,
self.device_id, Device.model, doc_ids, callback_func)
end
function SyncNinja:downloadDocuments(doc_metadata, interactive)
logger.dbg("SyncNinja: downloadDocuments")
-- TODO
end
function SyncNinja:uploadDocuments(doc_metadata, interactive)
logger.dbg("SyncNinja: uploadDocuments")
-- Ensure Document Sync Enabled
if self.settings.sync_documents ~= true then return end
-- API Callback Function
local callback_func = function(ok, body)
if not ok then
-- TODO: if interactive
UIManager:show(InfoMessage:new{
text = _("SyncNinja: uploadDocuments Error"),
timeout = 3
})
return logger.dbg("SyncNinja: uploadDocuments Error:", dump(body))
end
end
-- API Client
local SyncNinjaClient = require("SyncNinjaClient")
local client = SyncNinjaClient:new{
custom_url = self.settings.server,
service_spec = self.path .. "/api.json"
}
-- API Initial Metadata
local ok, err = pcall(client.add_documents, client, self.settings.username,
self.settings.password, doc_metadata, callback_func)
-- Ensure Document File Sync Enabled
if self.settings.sync_document_files ~= true then return end
if interactive ~= true then return end
-- API File Upload
local confirm_upload_callback = function()
for _, v in pairs(doc_metadata) do
if v.filepath ~= nil then
local ok, err = pcall(client.upload_document, client,
self.settings.username,
self.settings.password, v.id, v.filepath,
callback_func)
end
end
end
UIManager:show(ConfirmBox:new{
text = _("Upload documents? This can take awhile."),
ok_text = _("Yes"),
ok_callback = confirm_upload_callback
})
end
------------------------------------------
------------ Getter Functions ------------
------------------------------------------
function SyncNinja:getLocalDocumentIDs(doc_metadata)
logger.dbg("SyncNinja: getLocalDocumentIDs")
local document_ids = {}
if doc_metadata == nil then
doc_metadata = self:getLocalDocumentMetadata()
end
for _, v in pairs(doc_metadata) do table.insert(document_ids, v.id) end
return document_ids
end
function SyncNinja:getLocalDocumentMetadata()
logger.dbg("SyncNinja: getLocalDocumentMetadata")
local all_documents = {}
local documents_kv = self:getStatisticsBookKV()
local bookinfo_books = self:getBookInfoBookKV()
for _, v in pairs(ReadHistory.hist) do
if DocSettings:hasSidecarFile(v.file) then
local docsettings = DocSettings:open(v.file)
-- Ensure Partial MD5 Exists
local pmd5 = docsettings:readSetting("partial_md5_checksum")
if not pmd5 then
pmd5 = self:getPartialMd5(v.file)
docsettings:saveSetting("partial_md5_checksum", pmd5)
end
-- Get Document Props
local doc_props = docsettings:readSetting("doc_props")
local fdoc = bookinfo_books[v.file] or {}
-- Update or Create
if documents_kv[pmd5] ~= nil then
local doc = documents_kv[pmd5]
-- Merge Statistics, History, and BookInfo
doc.title = doc.title or doc_props.title or fdoc.title
doc.author = doc.author or doc_props.authors or fdoc.author
doc.series = doc.series or doc_props.series or fdoc.series
doc.lang = doc.lang or doc_props.language or fdoc.lang
-- Merge History and BookInfo
doc.series_index = doc_props.series_index or fdoc.series_index
doc.description = doc_props.description or fdoc.description
doc.filepath = v.file
else
-- Merge History and BookInfo
documents_kv[pmd5] = {
title = doc_props.title or fdoc.title,
author = doc_props.authors or fdoc.author,
series = doc_props.series or fdoc.series,
series_index = doc_props.series_index or fdoc.series_index,
lang = doc_props.language or fdoc.lang,
description = doc_props.description or fdoc.description,
filepath = v.file
}
end
end
end
-- Convert KV -> Array
for pmd5, v in pairs(documents_kv) do
table.insert(all_documents, {
id = pmd5,
title = v.title,
author = v.author,
series = v.series,
series_index = v.series_index,
lang = v.lang,
description = v.description,
filepath = v.filepath
})
end
return all_documents
end
function SyncNinja:getStatisticsActivity(timestamp)
logger.dbg("SyncNinja: getStatisticsActivity")
local all_data = {}
local conn = SQ3.open(statistics_db)
local stmt = conn:prepare(string.format(STATISTICS_ACTIVITY_SINCE_QUERY,
timestamp))
local rows = stmt:resultset("i", 1000)
conn:close()
-- No Results
if rows == nil then return all_data end
-- Normalize
for i, v in pairs(rows[1]) do
table.insert(all_data, {
document = rows[1][i],
start_time = tonumber(rows[2][i]),
duration = tonumber(rows[3][i]),
current_page = tonumber(rows[4][i]),
total_pages = tonumber(rows[5][i])
})
end
return all_data
end
-- Returns KEY:VAL (MD5:<TABLE>)
function SyncNinja:getStatisticsBookKV()
logger.dbg("SyncNinja: getStatisticsBookKV")
local all_data = {}
local conn = SQ3.open(statistics_db)
local stmt = conn:prepare(STATISTICS_BOOK_QUERY)
local rows = stmt:resultset("i", 1000)
conn:close()
-- No Results
if rows == nil then return all_data end
-- Normalize
for i, v in pairs(rows[1]) do
local pmd5 = rows[1][i]
all_data[pmd5] = {
title = rows[2][i],
author = rows[3][i],
series = rows[4][i],
lang = rows[5][i]
}
end
return all_data
end
-- Returns KEY:VAL (FILEPATH:<TABLE>)
function SyncNinja:getBookInfoBookKV()
logger.dbg("SyncNinja: getBookInfoBookKV")
local all_data = {}
local conn = SQ3.open(bookinfo_db)
local stmt = conn:prepare(BOOKINFO_BOOK_QUERY)
local rows = stmt:resultset("i", 1000)
conn:close()
-- No Results
if rows == nil then return all_data end
-- Normalize
for i, v in pairs(rows[1]) do
filepath = rows[1][i]
all_data[filepath] = {
title = rows[2][i],
author = rows[3][i],
series = rows[4][i],
series_index = tonumber(rows[5][i]),
lang = rows[6][i],
description = rows[7][i]
}
end
return all_data
end
function SyncNinja:getPartialMd5(file)
logger.dbg("SyncNinja: getPartialMd5")
if file == nil then return nil end
local bit = require("bit")
local lshift = bit.lshift
local step, size = 1024, 1024
local update = md5()
local file_handle = io.open(file, 'rb')
if file_handle == nil then return nil end
for i = -1, 10 do
file_handle:seek("set", lshift(step, 2 * i))
local sample = file_handle:read(size)
if sample then
update(sample)
else
break
end
end
file_handle:close()
return update()
end
return SyncNinja

55
cmd/main.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"os"
"os/signal"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"reichard.io/bbank/server"
)
type UTCFormatter struct {
log.Formatter
}
func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return u.Formatter.Format(e)
}
func main() {
log.SetFormatter(UTCFormatter{&log.TextFormatter{FullTimestamp: true}})
app := &cli.App{
Name: "Book Bank",
Usage: "A self hosted e-book progress tracker.",
Commands: []*cli.Command{
{
Name: "serve",
Aliases: []string{"s"},
Usage: "Start Book Bank web server.",
Action: cmdServer,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func cmdServer(ctx *cli.Context) error {
log.Info("Starting Book Bank Server")
server := server.NewServer()
server.StartServer()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
server.StopServer()
os.Exit(0)
return nil
}

34
config/config.go Normal file
View File

@ -0,0 +1,34 @@
package config
import (
"os"
)
type Config struct {
DBType string
DBName string
DBPassword string
ConfigPath string
DataPath string
ListenPort string
Version string
}
func Load() *Config {
return &Config{
DBType: getEnv("DATABASE_TYPE", "SQLite"),
DBName: getEnv("DATABASE_NAME", "bbank"),
DBPassword: getEnv("DATABASE_PASSWORD", ""),
ConfigPath: getEnv("CONFIG_PATH", "/config"),
DataPath: getEnv("DATA_PATH", "/data"),
ListenPort: getEnv("LISTEN_PORT", "8585"),
Version: "0.0.1",
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

31
database/db.go Normal file
View File

@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.21.0
package database
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

66
database/manager.go Normal file
View File

@ -0,0 +1,66 @@
package database
import (
"context"
"database/sql"
_ "embed"
"path"
sqlite "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"reichard.io/bbank/config"
)
type DBManager struct {
DB *sql.DB
Ctx context.Context
Queries *Queries
}
//go:embed schema.sql
var ddl string
func foobar() string {
log.Info("WTF")
return ""
}
func NewMgr(c *config.Config) *DBManager {
// Create Manager
dbm := &DBManager{
Ctx: context.Background(),
}
// Create Database
if c.DBType == "SQLite" {
sql.Register("sqlite3_custom", &sqlite.SQLiteDriver{
ConnectHook: func(conn *sqlite.SQLiteConn) error {
if err := conn.RegisterFunc("test_func", foobar, false); err != nil {
log.Info("Error Registering")
return err
}
return nil
},
})
dbLocation := path.Join(c.ConfigPath, "bbank.db")
var err error
dbm.DB, err = sql.Open("sqlite3_custom", dbLocation)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("Unsupported Database")
}
// Create Tables
if _, err := dbm.DB.ExecContext(dbm.Ctx, ddl); err != nil {
log.Fatal(err)
}
dbm.Queries = New(dbm.DB)
return dbm
}

79
database/models.go Normal file
View File

@ -0,0 +1,79 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.21.0
package database
import (
"time"
)
type Activity struct {
ID int64 `json:"id"`
UserID string `json:"user_id"`
DocumentID string `json:"document_id"`
DeviceID string `json:"device_id"`
StartTime time.Time `json:"start_time"`
Duration int64 `json:"duration"`
CurrentPage int64 `json:"current_page"`
TotalPages int64 `json:"total_pages"`
CreatedAt time.Time `json:"created_at"`
}
type Device struct {
ID string `json:"id"`
UserID string `json:"user_id"`
DeviceName string `json:"device_name"`
CreatedAt time.Time `json:"created_at"`
Sync bool `json:"sync"`
}
type Document struct {
ID string `json:"id"`
Md5 *string `json:"md5"`
Filepath *string `json:"filepath"`
Title *string `json:"title"`
Author *string `json:"author"`
Series *string `json:"series"`
SeriesIndex *int64 `json:"series_index"`
Lang *string `json:"lang"`
Description *string `json:"description"`
Olid *string `json:"-"`
Synced bool `json:"-"`
Deleted bool `json:"-"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}
type DocumentDeviceSync struct {
UserID string `json:"user_id"`
DocumentID string `json:"document_id"`
DeviceID string `json:"device_id"`
LastSynced time.Time `json:"last_synced"`
Sync bool `json:"sync"`
}
type DocumentProgress struct {
UserID string `json:"user_id"`
DocumentID string `json:"document_id"`
DeviceID string `json:"device_id"`
Percentage float64 `json:"percentage"`
Progress string `json:"progress"`
CreatedAt time.Time `json:"created_at"`
}
type RescaledActivity struct {
DocumentID string `json:"document_id"`
DeviceID string `json:"device_id"`
UserID string `json:"user_id"`
StartTime time.Time `json:"start_time"`
Page int64 `json:"page"`
Duration int64 `json:"duration"`
}
type User struct {
ID string `json:"id"`
Pass string `json:"-"`
Admin bool `json:"-"`
CreatedAt time.Time `json:"created_at"`
}

427
database/query.sql Normal file
View File

@ -0,0 +1,427 @@
-- name: CreateUser :execrows
INSERT INTO users (id, pass)
VALUES (?, ?)
ON CONFLICT DO NOTHING;
-- name: GetUser :one
SELECT * FROM users
WHERE id = $user_id LIMIT 1;
-- name: UpsertDocument :one
INSERT INTO documents (
id,
md5,
filepath,
title,
author,
series,
series_index,
lang,
description,
olid
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT DO UPDATE
SET
md5 = COALESCE(excluded.md5, md5),
filepath = COALESCE(excluded.filepath, filepath),
title = COALESCE(excluded.title, title),
author = COALESCE(excluded.author, author),
series = COALESCE(excluded.series, series),
series_index = COALESCE(excluded.series_index, series_index),
lang = COALESCE(excluded.lang, lang),
description = COALESCE(excluded.description, description),
olid = COALESCE(excluded.olid, olid)
RETURNING *;
-- name: DeleteDocument :execrows
UPDATE documents
SET
deleted = 1
WHERE id = $id;
-- name: UpdateDocumentSync :one
UPDATE documents
SET
synced = $synced
WHERE id = $id
RETURNING *;
-- name: UpdateDocumentDeleted :one
UPDATE documents
SET
deleted = $deleted
WHERE id = $id
RETURNING *;
-- name: GetDocument :one
SELECT * FROM documents
WHERE id = $document_id LIMIT 1;
-- name: UpsertDevice :one
INSERT INTO devices (id, user_id, device_name)
VALUES (?, ?, ?)
ON CONFLICT DO UPDATE
SET
device_name = COALESCE(excluded.device_name, device_name)
RETURNING *;
-- name: GetDevice :one
SELECT * FROM devices
WHERE id = $device_id LIMIT 1;
-- name: UpdateProgress :one
INSERT OR REPLACE INTO document_progress (
user_id,
document_id,
device_id,
percentage,
progress
)
VALUES (?, ?, ?, ?, ?)
RETURNING *;
-- name: GetProgress :one
SELECT
document_progress.*,
devices.device_name
FROM document_progress
JOIN devices ON document_progress.device_id = devices.id
WHERE
document_progress.user_id = $user_id
AND document_progress.document_id = $document_id
ORDER BY
document_progress.created_at
DESC
LIMIT 1;
-- name: GetLastActivity :one
SELECT start_time
FROM activity
WHERE device_id = $device_id
AND user_id = $user_id
ORDER BY start_time DESC LIMIT 1;
-- name: AddActivity :one
INSERT INTO activity (
user_id,
document_id,
device_id,
start_time,
duration,
current_page,
total_pages
)
VALUES (?, ?, ?, ?, ?, ?, ?)
RETURNING *;
-- name: GetMissingDocuments :many
SELECT documents.* FROM documents
WHERE
documents.filepath IS NOT NULL
AND documents.deleted = false
AND documents.id NOT IN (sqlc.slice('document_ids'));
-- name: GetWantedDocuments :many
SELECT CAST(value AS TEXT) AS id
FROM json_each(?1)
LEFT JOIN documents
ON value = documents.id
WHERE (
documents.id IS NOT NULL
AND documents.synced = false
)
OR (documents.id IS NULL)
OR CAST($document_ids AS TEXT) != CAST($document_ids AS TEXT);
-- name: GetDeletedDocuments :many
SELECT documents.id
FROM documents
WHERE
documents.deleted = true
AND documents.id IN (sqlc.slice('document_ids'));
-- name: GetDocuments :many
SELECT * FROM documents
ORDER BY created_at DESC
LIMIT $limit
OFFSET $offset;
-- name: GetDocumentsWithStats :many
WITH true_progress AS (
SELECT
start_time AS last_read,
SUM(duration) / 60 AS total_time_minutes,
document_id,
current_page,
total_pages,
ROUND(CAST(current_page AS REAL) / CAST(total_pages AS REAL) * 100, 2) AS percentage
FROM activity
WHERE user_id = $user_id
GROUP BY document_id
HAVING MAX(start_time)
)
SELECT
documents.*,
CAST(IFNULL(current_page, 0) AS INTEGER) AS current_page,
CAST(IFNULL(total_pages, 0) AS INTEGER) AS total_pages,
CAST(IFNULL(total_time_minutes, 0) AS INTEGER) AS total_time_minutes,
CAST(
STRFTIME('%Y-%m-%dT%H:%M:%SZ', IFNULL(last_read, "1970-01-01")
) AS TEXT) AS last_read,
CAST(CASE
WHEN percentage > 97.0 THEN 100.0
WHEN percentage IS NULL THEN 0.0
ELSE percentage
END AS REAL) AS percentage
FROM documents
LEFT JOIN true_progress ON document_id = id
ORDER BY last_read DESC, created_at DESC
LIMIT $limit
OFFSET $offset;
-- name: GetUsers :many
SELECT * FROM users
WHERE
users.id = $user
OR ?1 IN (
SELECT id
FROM users
WHERE id = $user
AND admin = 1
)
ORDER BY created_at DESC
LIMIT $limit
OFFSET $offset;
-- name: GetActivity :many
SELECT * FROM activity
WHERE
user_id = $user_id
AND (
($doc_filter = TRUE AND document_id = $document_id)
OR $doc_filter = FALSE
)
ORDER BY start_time DESC
LIMIT $limit
OFFSET $offset;
-- name: GetDevices :many
SELECT * FROM devices
WHERE user_id = $user_id
ORDER BY created_at DESC
LIMIT $limit
OFFSET $offset;
-- name: GetDocumentReadStats :one
SELECT
count(DISTINCT page) AS pages_read,
sum(duration) AS total_time
FROM rescaled_activity
WHERE document_id = $document_id
AND user_id = $user_id
AND start_time >= $start_time;
-- name: GetDocumentReadStatsCapped :one
WITH capped_stats AS (
SELECT min(sum(duration), CAST($page_duration_cap AS INTEGER)) AS durations
FROM rescaled_activity
WHERE document_id = $document_id
AND user_id = $user_id
AND start_time >= $start_time
GROUP BY page
)
SELECT
CAST(count(*) AS INTEGER) AS pages_read,
CAST(sum(durations) AS INTEGER) AS total_time
FROM capped_stats;
-- name: GetDocumentDaysRead :one
WITH document_days AS (
SELECT date(start_time, 'localtime') AS dates
FROM rescaled_activity
WHERE document_id = $document_id
AND user_id = $user_id
GROUP BY dates
)
SELECT CAST(count(*) AS INTEGER) AS days_read
FROM document_days;
-- name: GetUserDayStreaks :one
WITH document_days AS (
SELECT date(start_time, 'localtime') AS read_day
FROM activity
WHERE user_id = $user_id
GROUP BY read_day
ORDER BY read_day DESC
),
partitions AS (
SELECT
document_days.*,
row_number() OVER (
PARTITION BY 1 ORDER BY read_day DESC
) AS seqnum
FROM document_days
),
streaks AS (
SELECT
count(*) AS streak,
MIN(read_day) AS start_date,
MAX(read_day) AS end_date
FROM partitions
GROUP BY date(read_day, '+' || seqnum || ' day')
ORDER BY end_date DESC
),
max_streak AS (
SELECT
MAX(streak) AS max_streak,
start_date AS max_streak_start_date,
end_date AS max_streak_end_date
FROM streaks
)
SELECT
CAST(max_streak AS INTEGER),
CAST(max_streak_start_date AS TEXT),
CAST(max_streak_end_date AS TEXT),
streak AS current_streak,
CAST(start_date AS TEXT) AS current_streak_start_date,
CAST(end_date AS TEXT) AS current_streak_end_date
FROM max_streak, streaks LIMIT 1;
-- name: GetUserWeekStreaks :one
WITH document_weeks AS (
SELECT STRFTIME('%Y-%m-%d', start_time, 'localtime', 'weekday 0', '-7 day') AS read_week
FROM activity
WHERE user_id = $user_id
GROUP BY read_week
ORDER BY read_week DESC
),
partitions AS (
SELECT
document_weeks.*,
row_number() OVER (
PARTITION BY 1 ORDER BY read_week DESC
) AS seqnum
FROM document_weeks
),
streaks AS (
SELECT
count(*) AS streak,
MIN(read_week) AS start_date,
MAX(read_week) AS end_date
FROM partitions
GROUP BY date(read_week, '+' || (seqnum * 7) || ' day')
ORDER BY end_date DESC
),
max_streak AS (
SELECT
MAX(streak) AS max_streak,
start_date AS max_streak_start_date,
end_date AS max_streak_end_date
FROM streaks
)
SELECT
CAST(max_streak AS INTEGER),
CAST(max_streak_start_date AS TEXT),
CAST(max_streak_end_date AS TEXT),
streak AS current_streak,
CAST(start_date AS TEXT) AS current_streak_start_date,
CAST(end_date AS TEXT) AS current_streak_end_date
FROM max_streak, streaks LIMIT 1;
-- name: GetUserWindowStreaks :one
WITH document_windows AS (
SELECT CASE
WHEN ?2 = "WEEK" THEN STRFTIME('%Y-%m-%d', start_time, 'localtime', 'weekday 0', '-7 day')
WHEN ?2 = "DAY" THEN date(start_time, 'localtime')
END AS read_window
FROM activity
WHERE user_id = $user_id
AND CAST($window AS TEXT) = CAST($window AS TEXT)
GROUP BY read_window
ORDER BY read_window DESC
),
partitions AS (
SELECT
document_windows.*,
row_number() OVER (
PARTITION BY 1 ORDER BY read_window DESC
) AS seqnum
FROM document_windows
),
streaks AS (
SELECT
count(*) AS streak,
MIN(read_window) AS start_date,
MAX(read_window) AS end_date
FROM partitions
GROUP BY CASE
WHEN ?2 = "DAY" THEN date(read_window, '+' || seqnum || ' day')
WHEN ?2 = "WEEK" THEN date(read_window, '+' || (seqnum * 7) || ' day')
END
ORDER BY end_date DESC
),
max_streak AS (
SELECT
MAX(streak) AS max_streak,
start_date AS max_streak_start_date,
end_date AS max_streak_end_date
FROM streaks
)
SELECT
CAST(max_streak AS INTEGER),
CAST(max_streak_start_date AS TEXT),
CAST(max_streak_end_date AS TEXT),
streak AS current_streak,
CAST(start_date AS TEXT) AS current_streak_start_date,
CAST(end_date AS TEXT) AS current_streak_end_date
FROM max_streak, streaks LIMIT 1;
-- name: GetDatabaseInfo :one
SELECT
(SELECT count(rowid) FROM activity WHERE activity.user_id = $user_id) AS activity_size,
(SELECT count(rowid) FROM documents) AS documents_size,
(SELECT count(rowid) FROM document_progress WHERE document_progress.user_id = $user_id) AS progress_size,
(SELECT count(rowid) FROM devices WHERE devices.user_id = $user_id) AS devices_size
LIMIT 1;
-- name: GetDailyReadStats :many
WITH RECURSIVE last_30_days (date) AS (
SELECT date('now') AS date
UNION ALL
SELECT date(date, '-1 days')
FROM last_30_days
LIMIT 30
),
activity_records AS (
SELECT
sum(duration) AS seconds_read,
date(start_time, 'localtime') AS day
FROM activity
WHERE user_id = $user_id
GROUP BY day
ORDER BY day DESC
LIMIT 30
)
SELECT
CAST(date AS TEXT),
CAST(CASE
WHEN seconds_read IS NULL THEN 0
ELSE seconds_read / 60
END AS INTEGER) AS minutes_read
FROM last_30_days
LEFT JOIN activity_records ON activity_records.day == last_30_days.date
ORDER BY date DESC
LIMIT 30;
-- SELECT
-- sum(duration) / 60 AS minutes_read,
-- date(start_time, 'localtime') AS day
-- FROM activity
-- GROUP BY day
-- ORDER BY day DESC
-- LIMIT 10;

1263
database/query.sql.go Normal file

File diff suppressed because it is too large Load Diff

156
database/schema.sql Normal file
View File

@ -0,0 +1,156 @@
PRAGMA foreign_keys = ON;
-- Authentication
CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL PRIMARY KEY,
pass TEXT NOT NULL,
admin BOOLEAN NOT NULL DEFAULT 0 CHECK (admin IN (0, 1)),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Books / Documents
CREATE TABLE IF NOT EXISTS documents (
id TEXT NOT NULL PRIMARY KEY,
md5 TEXT,
filepath TEXT,
title TEXT,
author TEXT,
series TEXT,
series_index INTEGER,
lang TEXT,
description TEXT,
olid TEXT,
synced BOOLEAN NOT NULL DEFAULT 0 CHECK (synced IN (0, 1)),
deleted BOOLEAN NOT NULL DEFAULT 0 CHECK (deleted IN (0, 1)),
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Devices
CREATE TABLE IF NOT EXISTS devices (
id TEXT NOT NULL PRIMARY KEY,
user_id TEXT NOT NULL,
device_name TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
sync BOOLEAN NOT NULL DEFAULT 1 CHECK (sync IN (0, 1)),
FOREIGN KEY (user_id) REFERENCES users (id)
);
-- Document Device Sync
CREATE TABLE IF NOT EXISTS document_device_sync (
user_id TEXT NOT NULL,
document_id TEXT NOT NULL,
device_id TEXT NOT NULL,
last_synced DATETIME NOT NULL,
sync BOOLEAN NOT NULL DEFAULT 1 CHECK (sync IN (0, 1)),
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (document_id) REFERENCES documents (id),
FOREIGN KEY (device_id) REFERENCES devices (id),
PRIMARY KEY (user_id, document_id, device_id)
);
-- User Document Progress
CREATE TABLE IF NOT EXISTS document_progress (
user_id TEXT NOT NULL,
document_id TEXT NOT NULL,
device_id TEXT NOT NULL,
percentage REAL NOT NULL,
progress TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (document_id) REFERENCES documents (id),
FOREIGN KEY (device_id) REFERENCES devices (id),
PRIMARY KEY (user_id, document_id, device_id)
);
-- Read Activity
CREATE TABLE IF NOT EXISTS activity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
document_id TEXT NOT NULL,
device_id TEXT NOT NULL,
start_time DATETIME NOT NULL,
duration INTEGER NOT NULL,
current_page INTEGER NOT NULL,
total_pages INTEGER NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (document_id) REFERENCES documents (id),
FOREIGN KEY (device_id) REFERENCES devices (id)
);
-- Update Trigger
CREATE TRIGGER IF NOT EXISTS update_documents_updated_at
BEFORE UPDATE ON documents BEGIN
UPDATE documents
SET updated_at = CURRENT_TIMESTAMP
WHERE id = old.id;
END;
-- Rescaled Activity View (Adapted from KOReader)
CREATE VIEW IF NOT EXISTS rescaled_activity AS
WITH RECURSIVE numbers (idx) AS (
SELECT 1 AS idx
UNION ALL
SELECT idx + 1
FROM numbers
LIMIT 1000
),
total_pages AS (
SELECT
document_id,
total_pages AS pages
FROM activity
GROUP BY document_id
HAVING MAX(start_time)
ORDER BY start_time DESC
),
intermediate AS (
SELECT
activity.document_id,
activity.device_id,
activity.user_id,
activity.current_page,
activity.total_pages,
total_pages.pages,
activity.start_time,
activity.duration,
numbers.idx,
-- Derive First Page
((activity.current_page - 1) * total_pages.pages) / activity.total_pages
+ 1 AS first_page,
-- Derive Last Page
MAX(
((activity.current_page - 1) * total_pages.pages)
/ activity.total_pages
+ 1,
(activity.current_page * total_pages.pages) / activity.total_pages
) AS last_page
FROM activity
INNER JOIN total_pages ON total_pages.document_id = activity.document_id
INNER JOIN numbers ON numbers.idx <= (last_page - first_page + 1)
)
SELECT
document_id,
device_id,
user_id,
start_time,
first_page + idx - 1 AS page,
duration / (last_page - first_page + 1) AS duration
FROM intermediate;

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
---
services:
sync-ninja:
# working_dir: /app
environment:
- CONFIG_PATH=/data
- DATA_PATH=/data
ports:
- "8585:8585"
build: .
volumes:
- ./data:/data

48
go.mod Normal file
View File

@ -0,0 +1,48 @@
module reichard.io/bbank
go 1.19
require (
github.com/alexedwards/argon2id v0.0.0-20230305115115-4b3c3280a736
github.com/gabriel-vasile/mimetype v1.4.2
github.com/gin-contrib/multitemplate v0.0.0-20230212012517-45920c92c271
github.com/gin-contrib/sessions v0.0.4
github.com/gin-gonic/gin v1.9.1
github.com/mattn/go-sqlite3 v1.14.17
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)
require (
github.com/bytedance/sonic v1.10.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

217
go.sum Normal file
View File

@ -0,0 +1,217 @@
github.com/alexedwards/argon2id v0.0.0-20230305115115-4b3c3280a736 h1:qZaEtLxnqY5mJ0fVKbk31NVhlgi0yrKm51Pq/I5wcz4=
github.com/alexedwards/argon2id v0.0.0-20230305115115-4b3c3280a736/go.mod h1:mTeFRcTdnpzOlRjMoFYC/80HwVUreupyAiqPkCZQOXc=
github.com/antonlindstrom/pgstore v0.0.0-20200229204646-b08ebf1105e0/go.mod h1:2Ti6VUHVxpC0VSmTZzEvpzysnaGAfGBOoMIz5ykPyyw=
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff/go.mod h1:+RTT1BOk5P97fT2CiHkbFQwkK3mjsFAP6zCYV2aXtjw=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1/go.mod h1:dkChI7Tbtx7H1Tj7TqGSZMOeGpMP5gLHtjroHd4agiI=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk=
github.com/bytedance/sonic v1.10.0/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/multitemplate v0.0.0-20230212012517-45920c92c271 h1:s+boMV47gwTyff2PL+k6V33edJpp+K5y3QPzZlRhno8=
github.com/gin-contrib/multitemplate v0.0.0-20230212012517-45920c92c271/go.mod h1:XLLtIXoP9+9zGcEDc7gAGV3AksGPO+vzv4kXHMJSdU0=
github.com/gin-contrib/sessions v0.0.4 h1:gq4fNa1Zmp564iHP5G6EBuktilEos8VKhe2sza1KMgo=
github.com/gin-contrib/sessions v0.0.4/go.mod h1:pQ3sIyviBBGcxgyR8mkeJuXbeV3h3NYmhJADQTq5+Vo=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.15.3 h1:S+sSpunYjNPDuXkWbK+x+bA7iXiW296KG4dL3X7xUZo=
github.com/go-playground/validator/v10 v10.15.3/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b/go.mod h1:g2nVr8KZVXJSS97Jo8pJ0jgq29P6H7dG0oplUA86MQw=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b/go.mod h1:wTPjTepVu7uJBYgZ0SdWHQlIas582j6cn2jgk4DDdlg=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
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.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

173
graph/graph.go Normal file
View File

@ -0,0 +1,173 @@
package graph
import (
"fmt"
"math"
"reichard.io/bbank/database"
)
type SVGGraphPoint struct {
X int
Y int
Size int
}
type SVGGraphData struct {
Height int
Width int
Offset int
LinePoints []SVGGraphPoint
BarPoints []SVGGraphPoint
BezierPath string
BezierFill string
}
type SVGBezierOpposedLine struct {
Length int
Angle int
}
func GetSVGGraphData(inputData []database.GetDailyReadStatsRow, svgWidth int) SVGGraphData {
// Static Padding
var padding int = 5
// Derive Height
var maxHeight int = 0
for _, item := range inputData {
if int(item.MinutesRead) > maxHeight {
maxHeight = int(item.MinutesRead)
}
}
// Derive Block Offsets & Transformed Coordinates (Line & Bar)
var blockOffset int = int(math.Floor(float64(svgWidth) / float64(len(inputData))))
// Line & Bar Points
linePoints := []SVGGraphPoint{}
barPoints := []SVGGraphPoint{}
// Bezier Fill Coordinates (Max X, Min X, Max Y)
var maxBX int = 0
var maxBY int = 0
var minBX int = 0
for idx, item := range inputData {
itemSize := int(item.MinutesRead)
itemY := (maxHeight + padding) - itemSize
barPoints = append(barPoints, SVGGraphPoint{
X: (idx * blockOffset) + (blockOffset / 2),
Y: itemY,
Size: itemSize + padding,
})
lineX := (idx + 1) * blockOffset
linePoints = append(linePoints, SVGGraphPoint{
X: lineX,
Y: itemY,
Size: itemSize + padding,
})
if lineX > maxBX {
maxBX = lineX
}
if lineX < minBX {
minBX = lineX
}
if itemY > maxBY {
maxBY = itemY
}
}
// Return Data
return SVGGraphData{
Width: svgWidth + padding*2,
Height: maxHeight + padding*2,
Offset: blockOffset,
LinePoints: linePoints,
BarPoints: barPoints,
BezierPath: getSVGBezierPath(linePoints),
BezierFill: fmt.Sprintf("L %d,%d L %d,%d Z", maxBX, maxBY+padding, minBX, maxBY+padding),
}
}
func getSVGBezierOpposedLine(pointA SVGGraphPoint, pointB SVGGraphPoint) SVGBezierOpposedLine {
lengthX := float64(pointB.X - pointA.X)
lengthY := float64(pointB.Y - pointA.Y)
return SVGBezierOpposedLine{
Length: int(math.Sqrt(math.Pow(lengthX, 2) + math.Pow(lengthY, 2))),
Angle: int(math.Atan2(lengthY, lengthX)),
}
// length = Math.sqrt(Math.pow(lengthX, 2) + Math.pow(lengthY, 2)),
// angle = Math.atan2(lengthY, lengthX)
}
func getSVGBezierControlPoint(currentPoint *SVGGraphPoint, prevPoint *SVGGraphPoint, nextPoint *SVGGraphPoint, isReverse bool) SVGGraphPoint {
// First / Last Point
if prevPoint == nil {
prevPoint = currentPoint
}
if nextPoint == nil {
nextPoint = currentPoint
}
// Modifiers
var smoothingRatio float64 = 0.2
var directionModifier float64 = 0
if isReverse == true {
directionModifier = math.Pi
}
opposingLine := getSVGBezierOpposedLine(*prevPoint, *nextPoint)
var lineAngle float64 = float64(opposingLine.Angle) + directionModifier
var lineLength float64 = float64(opposingLine.Length) * smoothingRatio
// Calculate Control Point
return SVGGraphPoint{
X: currentPoint.X + int(math.Cos(float64(lineAngle))*lineLength),
Y: currentPoint.Y + int(math.Sin(float64(lineAngle))*lineLength),
}
}
func getSVGBezierCurve(point SVGGraphPoint, index int, allPoints []SVGGraphPoint) []SVGGraphPoint {
var pointMinusTwo *SVGGraphPoint
var pointMinusOne *SVGGraphPoint
var pointPlusOne *SVGGraphPoint
if index-2 >= 0 && index-2 < len(allPoints) {
pointMinusTwo = &allPoints[index-2]
}
if index-1 >= 0 && index-1 < len(allPoints) {
pointMinusOne = &allPoints[index-1]
}
if index+1 >= 0 && index+1 < len(allPoints) {
pointPlusOne = &allPoints[index+1]
}
startControlPoint := getSVGBezierControlPoint(pointMinusOne, pointMinusTwo, &point, false)
endControlPoint := getSVGBezierControlPoint(&point, pointMinusOne, pointPlusOne, true)
return []SVGGraphPoint{
startControlPoint,
endControlPoint,
point,
}
}
func getSVGBezierPath(allPoints []SVGGraphPoint) string {
var bezierSVGPath string = ""
for index, point := range allPoints {
if index == 0 {
bezierSVGPath += fmt.Sprintf("M %d,%d", point.X, point.Y)
} else {
newPoints := getSVGBezierCurve(point, index, allPoints)
bezierSVGPath += fmt.Sprintf(" C%d,%d %d,%d %d,%d", newPoints[0].X, newPoints[0].Y, newPoints[1].X, newPoints[1].Y, newPoints[2].X, newPoints[2].Y)
}
}
return bezierSVGPath
}

104
metadata/metadata.go Normal file
View File

@ -0,0 +1,104 @@
package metadata
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
)
type coverResult struct {
CoverEditionKey string `json:"cover_edition_key"`
}
type queryResponse struct {
ResultCount int `json:"numFound"`
Start int `json:"start"`
ResultCountExact bool `json:"numFoundExact"`
Results []coverResult `json:"docs"`
}
var BASE_QUERY_URL string = "https://openlibrary.org/search.json?q=%s&fields=cover_edition_key"
var BASE_COVER_URL string = "https://covers.openlibrary.org/b/olid/%s-L.jpg"
func GetCoverIDs(title *string, author *string) ([]string, error) {
if title == nil || author == nil {
log.Error("[metadata] Invalid Search Query")
return nil, errors.New("Invalid Query")
}
searchQuery := url.QueryEscape(fmt.Sprintf("%s %s", *title, *author))
apiQuery := fmt.Sprintf(BASE_QUERY_URL, searchQuery)
log.Info("[metadata] Acquiring CoverID")
resp, err := http.Get(apiQuery)
if err != nil {
log.Error("[metadata] Cover URL API Failure")
return nil, errors.New("API Failure")
}
target := queryResponse{}
err = json.NewDecoder(resp.Body).Decode(&target)
if err != nil {
log.Error("[metadata] Cover URL API Decode Failure")
return nil, errors.New("API Failure")
}
var coverIDs []string
for _, result := range target.Results {
if result.CoverEditionKey != "" {
coverIDs = append(coverIDs, result.CoverEditionKey)
}
}
return coverIDs, nil
}
func DownloadAndSaveCover(coverID string, dirPath string) (*string, error) {
// Derive & Sanitize File Name
fileName := "." + filepath.Clean(fmt.Sprintf("/%s.jpg", coverID))
// Generate Storage Path
safePath := filepath.Join(dirPath, "covers", fileName)
// Validate File Doesn't Exists
_, err := os.Stat(safePath)
if err == nil {
log.Warn("[metadata] File Alreads Exists")
return &safePath, nil
}
// Create File
out, err := os.Create(safePath)
if err != nil {
log.Error("[metadata] File Create Error")
return nil, errors.New("File Failure")
}
defer out.Close()
// Download File
log.Info("[metadata] Downloading Cover")
coverURL := fmt.Sprintf(BASE_COVER_URL, coverID)
resp, err := http.Get(coverURL)
if err != nil {
log.Error("[metadata] Cover URL API Failure")
return nil, errors.New("API Failure")
}
defer resp.Body.Close()
// Copy File to Disk
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Error("[metadata] File Copy Error")
return nil, errors.New("File Failure")
}
// Return FilePath
return &safePath, nil
}

BIN
screenshots/documents.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

BIN
screenshots/home.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 362 KiB

BIN
screenshots/login.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

62
server/server.go Normal file
View File

@ -0,0 +1,62 @@
package server
import (
"context"
"net/http"
"os"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
"reichard.io/bbank/api"
"reichard.io/bbank/config"
"reichard.io/bbank/database"
)
type Server struct {
API *api.API
Config *config.Config
Database *database.DBManager
httpServer *http.Server
}
func NewServer() *Server {
c := config.Load()
db := database.NewMgr(c)
api := api.NewApi(db, c)
// Create Paths
docDir := filepath.Join(c.DataPath, "documents")
coversDir := filepath.Join(c.DataPath, "covers")
_ = os.Mkdir(docDir, os.ModePerm)
_ = os.Mkdir(coversDir, os.ModePerm)
return &Server{
API: api,
Config: c,
Database: db,
}
}
func (s *Server) StartServer() {
listenAddr := (":" + s.Config.ListenPort)
s.httpServer = &http.Server{
Handler: s.API.Router,
Addr: listenAddr,
}
go func() {
err := s.httpServer.ListenAndServe()
if err != nil {
log.Error("Error starting server ", err)
}
}()
}
func (s *Server) StopServer() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}

8
shell.nix Normal file
View File

@ -0,0 +1,8 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
packages = with pkgs; [
go
nodejs_20
];
}

60
sqlc.yaml Normal file
View File

@ -0,0 +1,60 @@
version: 2
sql:
- engine: "sqlite"
schema: "./database/schema.sql"
queries: "./database/query.sql"
gen:
go:
package: "database"
out: "database"
emit_json_tags: true
overrides:
# Type pointers needed for JSON
- column: "documents.md5"
go_type:
type: "string"
pointer: true
- column: "documents.filepath"
go_type:
type: "string"
pointer: true
- column: "documents.title"
go_type:
type: "string"
pointer: true
- column: "documents.author"
go_type:
type: "string"
pointer: true
- column: "documents.series"
go_type:
type: "string"
pointer: true
- column: "documents.series_index"
go_type:
type: "int64"
pointer: true
- column: "documents.lang"
go_type:
type: "string"
pointer: true
- column: "documents.description"
go_type:
type: "string"
pointer: true
- column: "documents.olid"
go_type:
type: "string"
pointer: true
# Do not generate JSON
- column: "documents.synced"
go_struct_tag: 'json:"-"'
- column: "documents.olid"
go_struct_tag: 'json:"-"'
- column: "documents.deleted"
go_struct_tag: 'json:"-"'
- column: "users.pass"
go_struct_tag: 'json:"-"'
- column: "users.admin"
go_struct_tag: 'json:"-"'

4
templates/activity.html Normal file
View File

@ -0,0 +1,4 @@
{{template "base.html" .}} {{define "title"}}Activity{{end}} {{define
"content"}}
<h1>Activity</h1>
{{end}}

202
templates/base.html Normal file
View File

@ -0,0 +1,202 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<script src="https://cdn.tailwindcss.com"></script>
<title>{{block "title" .}}{{end}}</title>
</head>
<body>
<main
class="relative h-screen overflow-hidden bg-gray-100 dark:bg-gray-800"
>
<div class="flex items-start justify-between">
<div class="relative hidden h-screen shadow-lg lg:block w-64">
<div class="h-full bg-white dark:bg-gray-700">
<div class="flex items-center justify-start pt-4 ml-8">
<p class="text-xl font-bold dark:text-white">Book Manager</p>
</div>
<nav class="mt-6">
<div>
<a
class="flex items-center justify-start w-full p-2 pl-6 my-2 transition-colors duration-200 border-l-4 {{if eq .RouteName "home"}}border-purple-500 dark:text-white{{else}}border-transparent text-gray-400 hover:text-gray-800{{end}}"
href="/"
>
<span class="text-left">
<svg
width="20"
height="20"
fill="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path fill-rule="evenodd" clip-rule="evenodd" d="M2.5192 7.82274C2 8.77128 2 9.91549 2 12.2039V13.725C2 17.6258 2 19.5763 3.17157 20.7881C4.34315 22 6.22876 22 10 22H14C17.7712 22 19.6569 22 20.8284 20.7881C22 19.5763 22 17.6258 22 13.725V12.2039C22 9.91549 22 8.77128 21.4808 7.82274C20.9616 6.87421 20.0131 6.28551 18.116 5.10812L16.116 3.86687C14.1106 2.62229 13.1079 2 12 2C10.8921 2 9.88939 2.62229 7.88403 3.86687L5.88403 5.10813C3.98695 6.28551 3.0384 6.87421 2.5192 7.82274ZM11.25 18C11.25 18.4142 11.5858 18.75 12 18.75C12.4142 18.75 12.75 18.4142 12.75 18V15C12.75 14.5858 12.4142 14.25 12 14.25C11.5858 14.25 11.25 14.5858 11.25 15V18Z" />
</svg>
</span>
<span class="mx-4 text-sm font-normal"> Home </span>
</a>
<a
class="flex items-center justify-start w-full p-2 pl-6 my-2 transition-colors duration-200 border-l-4 {{if eq .RouteName "documents"}}border-purple-500 dark:text-white{{else}}border-transparent text-gray-400 hover:text-gray-800{{end}}"
href="/documents"
>
<span class="text-left">
<svg
width="20"
height="20"
fill="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path fill-rule="evenodd" clip-rule="evenodd" d="M6.27103 2.11151C5.46135 2.21816 5.03258 2.41324 4.72718 2.71244C4.42179 3.01165 4.22268 3.43172 4.11382 4.225C4.00176 5.04159 4 6.12387 4 7.67568V16.2442C4.38867 15.9781 4.82674 15.7756 5.29899 15.6517C5.82716 15.513 6.44305 15.5132 7.34563 15.5135L20 15.5135V7.67568C20 6.12387 19.9982 5.04159 19.8862 4.22499C19.7773 3.43172 19.5782 3.01165 19.2728 2.71244C18.9674 2.41324 18.5387 2.21816 17.729 2.11151C16.8955 2.00172 15.7908 2 14.2069 2H9.7931C8.2092 2 7.10452 2.00172 6.27103 2.11151ZM6.75862 6.59459C6.75862 6.1468 7.12914 5.78378 7.58621 5.78378H16.4138C16.8709 5.78378 17.2414 6.1468 17.2414 6.59459C17.2414 7.04239 16.8709 7.40541 16.4138 7.40541H7.58621C7.12914 7.40541 6.75862 7.04239 6.75862 6.59459ZM7.58621 9.56757C7.12914 9.56757 6.75862 9.93058 6.75862 10.3784C6.75862 10.8262 7.12914 11.1892 7.58621 11.1892H13.1034C13.5605 11.1892 13.931 10.8262 13.931 10.3784C13.931 9.93058 13.5605 9.56757 13.1034 9.56757H7.58621Z" />
<path d="M7.47341 17.1351H8.68965H13.1034H19.9991C19.9956 18.2657 19.9776 19.1088 19.8862 19.775C19.7773 20.5683 19.5782 20.9884 19.2728 21.2876C18.9674 21.5868 18.5387 21.7818 17.729 21.8885C16.8955 21.9983 15.7908 22 14.2069 22H9.7931C8.2092 22 7.10452 21.9983 6.27103 21.8885C5.46135 21.7818 5.03258 21.5868 4.72718 21.2876C4.42179 20.9884 4.22268 20.5683 4.11382 19.775C4.07259 19.4746 4.0463 19.1382 4.02952 18.7558C4.30088 18.0044 4.93365 17.4264 5.72738 17.218C6.01657 17.1421 6.39395 17.1351 7.47341 17.1351Z" />
</svg>
</span>
<span class="mx-4 text-sm font-normal"> Documents </span>
</a>
<a
class="flex items-center justify-start w-full p-2 pl-6 my-2 transition-colors duration-200 border-l-4 {{if eq .RouteName "activity"}}border-purple-500 dark:text-white{{else}}border-transparent text-gray-400 hover:text-gray-800{{end}}"
href="/activity"
>
<span class="text-left">
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M9.5 2C8.67157 2 8 2.67157 8 3.5V4.5C8 5.32843 8.67157 6 9.5 6H14.5C15.3284 6 16 5.32843 16 4.5V3.5C16 2.67157 15.3284 2 14.5 2H9.5Z"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M6.5 4.03662C5.24209 4.10719 4.44798 4.30764 3.87868 4.87694C3 5.75562 3 7.16983 3 9.99826V15.9983C3 18.8267 3 20.2409 3.87868 21.1196C4.75736 21.9983 6.17157 21.9983 9 21.9983H15C17.8284 21.9983 19.2426 21.9983 20.1213 21.1196C21 20.2409 21 18.8267 21 15.9983V9.99826C21 7.16983 21 5.75562 20.1213 4.87694C19.552 4.30764 18.7579 4.10719 17.5 4.03662V4.5C17.5 6.15685 16.1569 7.5 14.5 7.5H9.5C7.84315 7.5 6.5 6.15685 6.5 4.5V4.03662ZM7 9.75C6.58579 9.75 6.25 10.0858 6.25 10.5C6.25 10.9142 6.58579 11.25 7 11.25H7.5C7.91421 11.25 8.25 10.9142 8.25 10.5C8.25 10.0858 7.91421 9.75 7.5 9.75H7ZM10.5 9.75C10.0858 9.75 9.75 10.0858 9.75 10.5C9.75 10.9142 10.0858 11.25 10.5 11.25H17C17.4142 11.25 17.75 10.9142 17.75 10.5C17.75 10.0858 17.4142 9.75 17 9.75H10.5ZM7 13.25C6.58579 13.25 6.25 13.5858 6.25 14C6.25 14.4142 6.58579 14.75 7 14.75H7.5C7.91421 14.75 8.25 14.4142 8.25 14C8.25 13.5858 7.91421 13.25 7.5 13.25H7ZM10.5 13.25C10.0858 13.25 9.75 13.5858 9.75 14C9.75 14.4142 10.0858 14.75 10.5 14.75H17C17.4142 14.75 17.75 14.4142 17.75 14C17.75 13.5858 17.4142 13.25 17 13.25H10.5ZM7 16.75C6.58579 16.75 6.25 17.0858 6.25 17.5C6.25 17.9142 6.58579 18.25 7 18.25H7.5C7.91421 18.25 8.25 17.9142 8.25 17.5C8.25 17.0858 7.91421 16.75 7.5 16.75H7ZM10.5 16.75C10.0858 16.75 9.75 17.0858 9.75 17.5C9.75 17.9142 10.0858 18.25 10.5 18.25H17C17.4142 18.25 17.75 17.9142 17.75 17.5C17.75 17.0858 17.4142 16.75 17 16.75H10.5Z"/>
</svg>
</span>
<span class="mx-4 text-sm font-normal"> Activity </span>
</a>
<a
class="flex items-center justify-start w-full p-2 pl-6 my-2 transition-colors duration-200 border-l-4 {{if eq .RouteName "graphs"}}border-purple-500 dark:text-white{{else}}border-transparent text-gray-400 hover:text-gray-800{{end}}"
href="/graphs"
>
<span class="text-left">
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M3.46447 3.46447C2 4.92893 2 7.28595 2 12C2 16.714 2 19.0711 3.46447 20.5355C4.92893 22 7.28595 22 12 22C16.714 22 19.0711 22 20.5355 20.5355C22 19.0711 22 16.714 22 12C22 7.28595 22 4.92893 20.5355 3.46447C19.0711 2 16.714 2 12 2C7.28595 2 4.92893 2 3.46447 3.46447ZM17.5762 10.4801C17.8413 10.1619 17.7983 9.68901 17.4801 9.42383C17.1619 9.15866 16.689 9.20165 16.4238 9.51986L14.6269 11.6761C14.2562 12.1211 14.0284 12.3915 13.8409 12.5609C13.7539 12.6394 13.7023 12.6708 13.6775 12.6827C13.6725 12.6852 13.6689 12.6866 13.6667 12.6875C13.6667 12.6875 13.6624 12.6858 13.659 12.6842L13.6558 12.6827C13.6311 12.6708 13.5795 12.6394 13.4925 12.5609C13.3049 12.3915 13.0772 12.1211 12.7064 11.6761L12.414 11.3252C12.0855 10.931 11.7894 10.5756 11.5128 10.3258C11.2119 10.0541 10.8328 9.81205 10.3333 9.81205C9.83384 9.81205 9.45478 10.0541 9.15384 10.3258C8.87725 10.5756 8.58113 10.931 8.25267 11.3253L6.42383 13.5199C6.15866 13.8381 6.20165 14.311 6.51986 14.5762C6.83807 14.8413 7.31099 14.7983 7.57617 14.4801L9.37306 12.3239C9.74385 11.8789 9.97155 11.6085 10.1591 11.4391C10.2461 11.3606 10.2977 11.3292 10.3225 11.3173C10.3251 11.316 10.3274 11.315 10.3292 11.3142L10.3333 11.3125C10.3356 11.3134 10.3392 11.3148 10.3442 11.3173C10.3689 11.3292 10.4205 11.3606 10.5075 11.4391C10.6951 11.6085 10.9228 11.8789 11.2936 12.3239L11.586 12.6748C11.9145 13.069 12.2106 13.4244 12.4872 13.6742C12.7881 13.9459 13.1672 14.188 13.6667 14.188C14.1662 14.188 14.5452 13.9459 14.8462 13.6742C15.1228 13.4244 15.4189 13.069 15.7473 12.6748L17.5762 10.4801Z"
fill="currentColor"
/>
</svg>
</span>
<span class="mx-4 text-sm font-normal"> Graphs </span>
</a>
</div>
</nav>
</div>
</div>
<div class="flex flex-col w-full">
<header class="z-40 flex items-center justify-between w-full h-16">
<div class="block ml-6 lg:hidden">
<button
class="flex items-center p-2 text-gray-500 bg-white rounded-full shadow text-md"
>
<svg
width="20"
height="20"
class="text-gray-400"
fill="currentColor"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1664 1344v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45z"
></path>
</svg>
</button>
</div>
<h1 class="text-xl font-bold dark:text-white px-6">{{block "title" .}}{{end}}</h1>
<div
class="relative flex items-center justify-end w-full p-4 space-x-4"
>
<a href="#" class="relative block">
<svg
width="20"
fill="currentColor"
height="20"
class="text-gray-800 dark:text-gray-200"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1523 1339q-22-155-87.5-257.5t-184.5-118.5q-67 74-159.5 115.5t-195.5 41.5-195.5-41.5-159.5-115.5q-119 16-184.5 118.5t-87.5 257.5q106 150 271 237.5t356 87.5 356-87.5 271-237.5zm-243-699q0-159-112.5-271.5t-271.5-112.5-271.5 112.5-112.5 271.5 112.5 271.5 271.5 112.5 271.5-112.5 112.5-271.5zm512 256q0 182-71 347.5t-190.5 286-285.5 191.5-349 71q-182 0-348-71t-286-191-191-286-71-348 71-348 191-286 286-191 348-71 348 71 286 191 191 286 71 348z"
/>
</svg>
</a>
<button
class="custom-profile-button flex items-center text-gray-500 dark:text-white text-md py-4"
>
{{ .User }}
<svg
width="20"
height="20"
class="ml-2 text-gray-400"
fill="currentColor"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1408 704q0 26-19 45l-448 448q-19 19-45 19t-45-19l-448-448q-19-19-19-45t19-45 45-19h896q26 0 45 19t19 45z"
></path>
</svg>
</button>
<div
class="custom-profile-dropdown transition duration-200 absolute right-4 top-16 pt-4"
>
<div
class="w-56 origin-top-right bg-white rounded-md shadow-lg dark:shadow-gray-800 dark:bg-gray-700 ring-1 ring-black ring-opacity-5"
>
<div
class="py-1"
role="menu"
aria-orientation="vertical"
aria-labelledby="options-menu"
>
<a
href="/logout"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span>Logout</span>
</span>
</a>
</div>
</div>
</div>
</div>
</header>
<div class="h-screen px-4 pb-24 overflow-auto md:px-6">
{{block "content" .}}{{end}}
</div>
</div>
</div>
</main>
<!-- Custom Animation CSS -->
<style>
.custom-profile-dropdown {
visibility: hidden;
opacity: 0;
}
.custom-profile-button:hover + .custom-profile-dropdown,
.custom-profile-dropdown:hover {
visibility: visible;
opacity: 1;
}
</style>
</body>
</html>

182
templates/base.old.html Normal file
View File

@ -0,0 +1,182 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<script src="https://cdn.tailwindcss.com"></script>
<title>{{block "title" .}}{{end}}</title>
</head>
<body>
<nav class="bg-white dark:bg-gray-800 shadow">
<div class="px-8 mx-auto max-w-7xl">
<div class="flex items-center justify-between h-16">
<div class="flex items-center">
<div class="hidden md:block">
<div class="flex items-baseline space-x-4">
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/"
>
Home
</a>
<a
class="text-gray-800 dark:text-white hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/documents"
>
Documents
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/activity"
>
Activity
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/graphs"
>
Graphs
</a>
</div>
</div>
</div>
<div class="block">
<div class="flex items-center ml-4 md:ml-6">
<div class="relative ml-3">
<div
class="custom-profile-icon relative inline-block text-left"
>
<div>
<button
type="button"
class="flex items-center justify-center w-full rounded-md px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-50 hover:bg-gray-50 dark:hover:bg-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-gray-500"
id="options-menu"
>
<svg
width="20"
fill="currentColor"
height="20"
class="text-gray-800 dark:text-gray-200"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1523 1339q-22-155-87.5-257.5t-184.5-118.5q-67 74-159.5 115.5t-195.5 41.5-195.5-41.5-159.5-115.5q-119 16-184.5 118.5t-87.5 257.5q106 150 271 237.5t356 87.5 356-87.5 271-237.5zm-243-699q0-159-112.5-271.5t-271.5-112.5-271.5 112.5-112.5 271.5 112.5 271.5 271.5 112.5 271.5-112.5 112.5-271.5zm512 256q0 182-71 347.5t-190.5 286-285.5 191.5-349 71q-182 0-348-71t-286-191-191-286-71-348 71-348 191-286 286-191 348-71 348 71 286 191 191 286 71 348z"
/>
</svg>
</button>
</div>
</div>
<div
class="custom-profile-dropdown transition duration-200 absolute right-0 w-56 pt-2 origin-top-right bg-white rounded-md shadow-lg dark:bg-gray-800 ring-1 ring-black ring-opacity-5"
>
<div
class="py-1"
role="menu"
aria-orientation="vertical"
aria-labelledby="options-menu"
>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Settings </span>
</span>
</a>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Account </span>
</span>
</a>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Logout </span>
</span>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="flex -mr-2 md:hidden">
<button
class="text-gray-800 dark:text-white hover:text-gray-300 inline-flex items-center justify-center p-2 rounded-md focus:outline-none"
>
<svg
width="20"
height="20"
fill="currentColor"
class="w-8 h-8"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1664 1344v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45z"
/>
</svg>
</button>
</div>
</div>
</div>
<div class="md:hidden">
<div class="px-2 pt-2 pb-3 space-y-1 sm:px-3">
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Home
</a>
<a
class="text-gray-800 dark:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Gallery
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Content
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Contact
</a>
</div>
</div>
</nav>
<!-- Custom Animation CSS -->
<style>
.custom-profile-dropdown {
visibility: hidden;
opacity: 0;
}
.custom-profile-icon:hover + .custom-profile-dropdown,
.custom-profile-dropdown:hover {
visibility: visible;
opacity: 1;
}
</style>
<div
class="bg-white dark:text-gray-200 dark:bg-gray-800 shadow m-5 p-5 rounded"
>
<div class="flex items-center justify-between h-16">
{{block "content" .}}{{end}}
</div>
</div>
</body>
</html>

51
templates/documents.html Normal file
View File

@ -0,0 +1,51 @@
{{template "base.html" .}} {{define "title"}}Documents{{end}} {{define
"content"}}
<div class="grid grid-cols-1 gap-4 my-4 md:grid-cols-2 lg:grid-cols-3">
{{range $doc := .Data }}
<div class="w-full">
<div class="flex gap-4 w-full p-4 bg-white shadow-lg dark:bg-gray-700 rounded">
<div class="min-w-fit h-48 relative">
<a href="./documents/{{$doc.ID}}/file">
<img class="rounded object-cover h-full w-full" src="./documents/{{$doc.ID}}/cover"></img>
</a>
</div>
<div class="flex flex-col justify-around dark:text-white w-full text-xs">
<div class="inline-flex shrink-0 items-center">
<div>
<p class="text-gray-400">Title</p>
<p class="font-medium">
{{ or $doc.Title "Unknown" }}
</p>
</div>
</div>
<div class="inline-flex shrink-0 items-center">
<div>
<p class="text-gray-400">Author</p>
<p class="font-medium">
{{ or $doc.Author "Unknown" }}
</p>
</div>
</div>
<div class="inline-flex shrink-0 items-center">
<div>
<p class="text-gray-400">Progress</p>
<p class="font-medium">
{{ $doc.CurrentPage }} / {{ $doc.TotalPages }} ({{ $doc.Percentage }}%)
</p>
</div>
</div>
<div class="inline-flex shrink-0 items-center">
<div>
<p class="text-gray-400">Minutes Read</p>
<p class="font-medium">
{{ $doc.TotalTimeMinutes }} Minutes
</p>
</div>
</div>
</div>
</div>
</div>
{{end}}
</div>
{{end}}

35
templates/graph.svg Normal file
View File

@ -0,0 +1,35 @@
<svg viewBox="0 0 {{ $data.Width }} {{ $data.Height }}" class="chart">
<!-- Box Graph -->
{{ range $idx, $item := $data.BarPoints }}
<g class="bar" transform="translate({{ $item.X }}, 0)" fill="gray">
<rect y="{{ $item.Y }}" height="{{ $item.Size }}" width="33"></rect>
</g>
{{ end }}
<!-- Linear Line Graph -->
<polyline
fill="none"
stroke="black"
stroke-width="2"
points="
{{ range $item := $data.LinePoints }}
{{ $item.X }},{{ $item.Y }}
{{ end }}
"
/>
<!-- Bezier Curve Line Graph -->
<path
fill="#316BBE"
fill-opacity="0.5"
stroke="none"
d="{{ $data.BezierPath }} {{ $data.BezierFill }}"
/>
<path
fill="none"
fill-opacity="0.1"
stroke="none"
d="{{ $data.BezierPath }}"
/>
</svg>

After

Width:  |  Height:  |  Size: 789 B

3
templates/graphs.html Normal file
View File

@ -0,0 +1,3 @@
{{template "base.html" .}} {{define "title"}}Graphs{{end}} {{define "content"}}
<h1>Graphs</h1>
{{end}}

162
templates/header.html Normal file
View File

@ -0,0 +1,162 @@
<nav class="bg-white dark:bg-gray-800 shadow">
<div class="px-8 mx-auto max-w-7xl">
<div class="flex items-center justify-between h-16">
<div class="flex items-center">
<div class="hidden md:block">
<div class="flex items-baseline space-x-4">
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/"
>
Home
</a>
<a
class="text-gray-800 dark:text-white hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/documents"
>
Documents
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/activity"
>
Activity
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white px-3 py-2 rounded-md text-sm font-medium"
href="/graphs"
>
Graphs
</a>
</div>
</div>
</div>
<div class="block">
<div class="flex items-center ml-4 md:ml-6">
<div class="relative ml-3">
<div class="custom-profile-icon relative inline-block text-left">
<div>
<button
type="button"
class="flex items-center justify-center w-full rounded-md px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-50 hover:bg-gray-50 dark:hover:bg-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-gray-500"
id="options-menu"
>
<svg
width="20"
fill="currentColor"
height="20"
class="text-gray-800 dark:text-gray-200"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1523 1339q-22-155-87.5-257.5t-184.5-118.5q-67 74-159.5 115.5t-195.5 41.5-195.5-41.5-159.5-115.5q-119 16-184.5 118.5t-87.5 257.5q106 150 271 237.5t356 87.5 356-87.5 271-237.5zm-243-699q0-159-112.5-271.5t-271.5-112.5-271.5 112.5-112.5 271.5 112.5 271.5 271.5 112.5 271.5-112.5 112.5-271.5zm512 256q0 182-71 347.5t-190.5 286-285.5 191.5-349 71q-182 0-348-71t-286-191-191-286-71-348 71-348 191-286 286-191 348-71 348 71 286 191 191 286 71 348z"
/>
</svg>
</button>
</div>
</div>
<div
class="custom-profile-dropdown transition duration-200 absolute right-0 w-56 pt-2 origin-top-right bg-white rounded-md shadow-lg dark:bg-gray-800 ring-1 ring-black ring-opacity-5"
>
<div
class="py-1"
role="menu"
aria-orientation="vertical"
aria-labelledby="options-menu"
>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Settings </span>
</span>
</a>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Account </span>
</span>
</a>
<a
href="#"
class="block block px-4 py-2 text-md text-gray-700 hover:bg-gray-100 hover:text-gray-900 dark:text-gray-100 dark:hover:text-white dark:hover:bg-gray-600"
role="menuitem"
>
<span class="flex flex-col">
<span> Logout </span>
</span>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="flex -mr-2 md:hidden">
<button
class="text-gray-800 dark:text-white hover:text-gray-300 inline-flex items-center justify-center p-2 rounded-md focus:outline-none"
>
<svg
width="20"
height="20"
fill="currentColor"
class="w-8 h-8"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1664 1344v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45z"
/>
</svg>
</button>
</div>
</div>
</div>
<div class="md:hidden">
<div class="px-2 pt-2 pb-3 space-y-1 sm:px-3">
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Home
</a>
<a
class="text-gray-800 dark:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Gallery
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Content
</a>
<a
class="text-gray-300 hover:text-gray-800 dark:hover:text-white block px-3 py-2 rounded-md text-base font-medium"
href="/#"
>
Contact
</a>
</div>
</div>
</nav>
<!-- Custom Animation CSS -->
<style>
.custom-profile-dropdown {
visibility: hidden;
opacity: 0;
}
.custom-profile-icon:hover + .custom-profile-dropdown,
.custom-profile-dropdown:hover {
visibility: visible;
opacity: 1;
}
</style>

235
templates/home.html Normal file
View File

@ -0,0 +1,235 @@
{{template "base.html" .}} {{define "title"}}Home{{end}} {{define "content"}}
<div class="w-full">
<div class="relative w-full px-4 py-4 bg-white shadow-lg dark:bg-gray-700">
<p
class="text-sm font-semibold text-gray-700 border-b border-gray-200 w-max dark:text-white dark:border-gray-500"
>
Daily Read Totals
</p>
{{ $data := (GetSVGGraphData .Data.GraphData 800)}}
<svg viewBox="0 0 {{ $data.Width }} {{ $data.Height }}">
<path
fill="#316BBE"
fill-opacity="0.5"
stroke="none"
d="{{ $data.BezierPath }} {{ $data.BezierFill }}"
/>
<path fill="none" stroke="#316BBE" d="{{ $data.BezierPath }}" />
{{ range $index, $item := $data.LinePoints }}
<line
class="hover-trigger"
stroke="black"
stroke-opacity="0.0"
stroke-width="{{ $data.Offset }}"
x1="{{ $item.X }}"
x2="{{ $item.X }}"
y1="0"
y2="{{ $data.Height }}"
></line>
<g class="hover-item">
<line
class="text-black dark:text-white"
stroke-opacity="0.2"
x1="{{ $item.X }}"
x2="{{ $item.X }}"
y1="30"
y2="{{ $data.Height }}"
></line>
<text
class="text-black dark:text-white"
alignment-baseline="middle"
transform="translate({{ $item.X }}, 5) translate(-30, 8)"
font-size="10"
>
{{ (index $.Data.GraphData $index).Date }}
</text>
<text
class="text-black dark:text-white"
alignment-baseline="middle"
transform="translate({{ $item.X }}, 25) translate(-30, -2)"
font-size="10"
>
{{ (index $.Data.GraphData $index).MinutesRead }} minutes
</text>
</g>
{{ end }}
</svg>
<style>
/* Interactive Hover */
.hover-item {
visibility: hidden;
opacity: 0;
}
.hover-trigger:hover + .hover-item,
.hover-item:hover {
visibility: visible;
opacity: 1;
}
/* SVG Component Styling */
svg text.text-black {
fill: black;
}
svg line.text-black {
stroke: black;
}
@media (prefers-color-scheme: dark) {
svg text.dark\:text-white {
fill: white;
}
svg line.dark\:text-white {
stroke: white;
}
}
</style>
</div>
</div>
<div class="grid grid-cols-2 gap-4 my-4 md:grid-cols-4">
<div class="w-full">
<div
class="flex gap-4 w-full p-4 bg-white shadow-lg dark:bg-gray-700 rounded"
>
<div class="flex flex-col justify-around dark:text-white w-full text-xs">
<p class="text-2xl font-bold text-black dark:text-white">
{{ .Data.DatabaseInfo.DocumentsSize }}
</p>
<p class="text-sm text-gray-400">Documents</p>
</div>
</div>
</div>
<div class="w-full">
<div
class="flex gap-4 w-full p-4 bg-white shadow-lg dark:bg-gray-700 rounded"
>
<div class="flex flex-col justify-around dark:text-white w-full text-xs">
<p class="text-2xl font-bold text-black dark:text-white">
{{ .Data.DatabaseInfo.ActivitySize }}
</p>
<p class="text-sm text-gray-400">Activity Records</p>
</div>
</div>
</div>
<div class="w-full">
<div
class="flex gap-4 w-full p-4 bg-white shadow-lg dark:bg-gray-700 rounded"
>
<div class="flex flex-col justify-around dark:text-white w-full text-xs">
<p class="text-2xl font-bold text-black dark:text-white">
{{ .Data.DatabaseInfo.ProgressSize }}
</p>
<p class="text-sm text-gray-400">Progress Records</p>
</div>
</div>
</div>
<div class="w-full">
<div
class="flex gap-4 w-full p-4 bg-white shadow-lg dark:bg-gray-700 rounded"
>
<div class="flex flex-col justify-around dark:text-white w-full text-xs">
<p class="text-2xl font-bold text-black dark:text-white">
{{ .Data.DatabaseInfo.DevicesSize }}
</p>
<p class="text-sm text-gray-400">Devices</p>
</div>
</div>
</div>
</div>
<div class="grid grid-cols-1 gap-4 my-4 md:grid-cols-2 lg:grid-cols-3">
<div class="w-full">
<div class="relative w-full px-4 py-6 bg-white shadow-lg dark:bg-gray-700">
<p
class="text-sm font-semibold text-gray-700 border-b border-gray-200 w-max dark:text-white dark:border-gray-500"
>
Daily Read Streak
</p>
<div class="flex items-end my-6 space-x-2">
<p class="text-5xl font-bold text-black dark:text-white">
{{ .Data.DailyStreak.CurrentStreak }}
</p>
</div>
<div class="dark:text-white">
<div
class="flex items-center justify-between pb-2 mb-2 text-sm border-b border-gray-200"
>
<div>
<p>Current Daily Streak</p>
<div class="flex items-end text-xs text-gray-400">
{{ .Data.DailyStreak.CurrentStreakStartDate }} ➞ {{
.Data.DailyStreak.CurrentStreakEndDate }}
</div>
</div>
<div class="flex items-end font-bold">
{{ .Data.DailyStreak.CurrentStreak }}
</div>
</div>
<div class="flex items-center justify-between pb-2 mb-2 text-sm">
<div>
<p>Best Daily Streak</p>
<div class="flex items-end text-xs text-gray-400">
{{ .Data.DailyStreak.MaxStreakStartDate }} ➞ {{
.Data.DailyStreak.MaxStreakEndDate }}
</div>
</div>
<div class="flex items-end font-bold">
{{ .Data.DailyStreak.MaxStreak }}
</div>
</div>
</div>
</div>
</div>
<div class="w-full">
<div class="relative w-full px-4 py-6 bg-white shadow-lg dark:bg-gray-700">
<p
class="text-sm font-semibold text-gray-700 border-b border-gray-200 w-max dark:text-white dark:border-gray-500"
>
Weekly Read Streak
</p>
<div class="flex items-end my-6 space-x-2">
<p class="text-5xl font-bold text-black dark:text-white">
{{ .Data.WeeklyStreak.CurrentStreak }}
</p>
</div>
<div class="dark:text-white">
<div
class="flex items-center justify-between pb-2 mb-2 text-sm border-b border-gray-200"
>
<div>
<p>Current Weekly Streak</p>
<div class="flex items-end text-xs text-gray-400">
{{ .Data.WeeklyStreak.CurrentStreakStartDate }} ➞ {{
.Data.WeeklyStreak.CurrentStreakEndDate }}
</div>
</div>
<div class="flex items-end font-bold">
{{ .Data.WeeklyStreak.CurrentStreak }}
</div>
</div>
<div class="flex items-center justify-between pb-2 mb-2 text-sm">
<div>
<p>Best Weekly Streak</p>
<div class="flex items-end text-xs text-gray-400">
{{ .Data.WeeklyStreak.MaxStreakStartDate }} ➞ {{
.Data.WeeklyStreak.MaxStreakEndDate }}
</div>
</div>
<div class="flex items-end font-bold">
{{ .Data.WeeklyStreak.MaxStreak }}
</div>
</div>
</div>
</div>
</div>
</div>
{{end}}

171
templates/login.html Normal file
View File

@ -0,0 +1,171 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 dark:bg-gray-800 dark:text-white">
<div class="flex flex-wrap w-full">
<div class="flex flex-col w-full md:w-1/2">
<div
class="flex flex-col justify-center px-8 pt-8 my-auto md:justify-start md:pt-0 md:px-24 lg:px-32"
>
<p class="text-3xl text-center">Welcome.</p>
<form
class="flex flex-col pt-3 md:pt-8"
{{if
.Register}}action="/register"
{{else}}action="/login"
{{end}}
method="POST"
>
<div class="flex flex-col pt-4">
<div class="flex relative">
<span
class="inline-flex items-center px-3 border-t bg-white border-l border-b border-gray-300 text-gray-500 shadow-sm text-sm"
>
<svg
width="15"
height="15"
fill="currentColor"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1523 1339q-22-155-87.5-257.5t-184.5-118.5q-67 74-159.5 115.5t-195.5 41.5-195.5-41.5-159.5-115.5q-119 16-184.5 118.5t-87.5 257.5q106 150 271 237.5t356 87.5 356-87.5 271-237.5zm-243-699q0-159-112.5-271.5t-271.5-112.5-271.5 112.5-112.5 271.5 112.5 271.5 271.5 112.5 271.5-112.5 112.5-271.5zm512 256q0 182-71 347.5t-190.5 286-285.5 191.5-349 71q-182 0-348-71t-286-191-191-286-71-348 71-348 191-286 286-191 348-71 348 71 286 191 191 286 71 348z"
></path>
</svg>
</span>
<input
type="text"
id="username"
name="username"
class="flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent"
placeholder="Username"
/>
</div>
</div>
<div class="flex flex-col pt-4 mb-12">
<div class="flex relative">
<span
class="inline-flex items-center px-3 border-t bg-white border-l border-b border-gray-300 text-gray-500 shadow-sm text-sm"
>
<svg
width="15"
height="15"
fill="currentColor"
viewBox="0 0 1792 1792"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1376 768q40 0 68 28t28 68v576q0 40-28 68t-68 28h-960q-40 0-68-28t-28-68v-576q0-40 28-68t68-28h32v-320q0-185 131.5-316.5t316.5-131.5 316.5 131.5 131.5 316.5q0 26-19 45t-45 19h-64q-26 0-45-19t-19-45q0-106-75-181t-181-75-181 75-75 181v320h736z"
></path>
</svg>
</span>
<input
type="password"
id="password"
name="password"
class="flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent"
placeholder="Password"
/>
<span class="absolute -bottom-5 text-red-400 text-xs"
>{{ .Error }}</span
>
</div>
</div>
<button
type="submit"
class="w-full px-4 py-2 text-base font-semibold text-center text-white transition duration-200 ease-in bg-black shadow-md hover:text-black hover:bg-white focus:outline-none focus:ring-2"
>
{{if .Register}}
<span class="w-full"> Register </span>
{{else}}
<span class="w-full"> Submit </span>
{{end}}
</button>
</form>
<div class="pt-12 pb-12 text-center">
{{ if .Register }}
<p>
Trying to login?
<a href="./login" class="font-semibold underline">
Login here.
</a>
</p>
{{else}}
<p>
Don&#x27;t have an account?
<a href="./register" class="font-semibold underline">
Register here.
</a>
</p>
{{end}}
</div>
</div>
</div>
<div
class="hidden image-fader w-1/2 shadow-2xl h-screen relative md:block"
>
<img
class="w-full h-screen object-cover ease-in-out top-0 left-0"
src="/assets/book1.jpg"
/>
<img
class="w-full h-screen object-cover ease-in-out top-0 left-0"
src="/assets/book2.jpg"
/>
<img
class="w-full h-screen object-cover ease-in-out top-0 left-0"
src="/assets/book3.jpg"
/>
<img
class="w-full h-screen object-cover ease-in-out top-0 left-0"
src="/assets/book4.jpg"
/>
</div>
</div>
<style>
.image-fader img {
position: absolute;
animation-name: imagefade;
animation-iteration-count: infinite;
animation-duration: 60s;
}
@keyframes imagefade {
0% {
opacity: 1;
}
17% {
opacity: 1;
}
25% {
opacity: 0;
}
92% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.image-fader img:nth-of-type(1) {
animation-delay: 45s;
}
.image-fader img:nth-of-type(2) {
animation-delay: 30s;
}
.image-fader img:nth-of-type(3) {
animation-delay: 15s;
}
.image-fader img:nth-of-type(4) {
animation-delay: 0;
}
</style>
</body>
</html>
<!-- https://stackoverflow.com/questions/60748752/change-an-image-after-some-time -->

49
utils/utils.go Normal file
View File

@ -0,0 +1,49 @@
package utils
import (
"bytes"
"crypto/md5"
// "encoding/hex"
"fmt"
"io"
"os"
)
func CalculatePartialMD5(filePath string) string {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
var step int64 = 1024
var size int64 = 1024
var buf bytes.Buffer
for i := -1; i <= 10; i++ {
byteStep := make([]byte, size)
var newShift int64 = int64(i * 2)
var newOffset int64
if i == -1 {
newOffset = 0
} else {
newOffset = step << newShift
}
_, err := file.ReadAt(byteStep, newOffset)
if err == io.EOF {
break
}
buf.Write(byteStep)
}
allBytes := buf.Bytes()
return fmt.Sprintf("%x", md5.Sum(allBytes))
}
func main() {
fileHash := CalculatePartialMD5("test.epub")
fmt.Println("MD5: ", fileHash)
}