[add] service worker & offline reader
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2023-10-28 20:07:24 -04:00
parent 1b8b5060f1
commit f74c81dc9b
29 changed files with 1041 additions and 205 deletions

View File

@@ -80,7 +80,6 @@ func (api *API) registerWebAppRoutes() {
render.AddFromFiles("error", "templates/error.html")
render.AddFromFilesFuncs("login", helperFuncs, "templates/login.html")
render.AddFromFilesFuncs("reader", helperFuncs, "templates/reader-base.html", "templates/reader.html")
render.AddFromFilesFuncs("home", helperFuncs, "templates/base.html", "templates/home.html")
render.AddFromFilesFuncs("search", helperFuncs, "templates/base.html", "templates/search.html")
render.AddFromFilesFuncs("settings", helperFuncs, "templates/base.html", "templates/settings.html")
@@ -90,7 +89,15 @@ func (api *API) registerWebAppRoutes() {
api.Router.HTMLRender = render
// Static Assets (Require @ Root)
api.Router.GET("/manifest.json", api.webManifest)
api.Router.GET("/sw.js", api.serviceWorker)
// Offline Static Pages (No Template)
api.Router.GET("/offline", api.offlineDocuments)
api.Router.GET("/reader", api.documentReader)
// Template App
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)
@@ -104,12 +111,12 @@ func (api *API) registerWebAppRoutes() {
api.Router.GET("/documents", api.authWebAppMiddleware, api.createAppResourcesRoute("documents"))
api.Router.POST("/documents", api.authWebAppMiddleware, api.uploadNewDocument)
api.Router.GET("/documents/:document", api.authWebAppMiddleware, api.createAppResourcesRoute("document"))
api.Router.GET("/documents/:document/reader", api.authWebAppMiddleware, api.documentReader)
api.Router.GET("/documents/:document/file", api.authWebAppMiddleware, api.downloadDocument)
api.Router.GET("/documents/:document/cover", api.authWebAppMiddleware, api.getDocumentCover)
api.Router.POST("/documents/:document/edit", api.authWebAppMiddleware, api.editDocument)
api.Router.POST("/documents/:document/identify", api.authWebAppMiddleware, api.identifyDocument)
api.Router.POST("/documents/:document/delete", api.authWebAppMiddleware, api.deleteDocument)
api.Router.GET("/documents/:document/progress", api.authWebAppMiddleware, api.getDocumentProgress)
// Behind Configuration Flag
if api.Config.SearchEnabled {

View File

@@ -72,6 +72,18 @@ func (api *API) webManifest(c *gin.Context) {
c.File("./assets/manifest.json")
}
func (api *API) serviceWorker(c *gin.Context) {
c.File("./assets/sw.js")
}
func (api *API) offlineDocuments(c *gin.Context) {
c.File("./assets/offline/index.html")
}
func (api *API) documentReader(c *gin.Context) {
c.File("./assets/reader/index.html")
}
func (api *API) createAppResourcesRoute(routeName string, args ...map[string]any) func(*gin.Context) {
// Merge Optional Template Data
var templateVarsBase = gin.H{}
@@ -245,7 +257,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
// Handle Identified Document
if document.Coverfile != nil {
if *document.Coverfile == "UNKNOWN" {
c.File("./assets/no-cover.jpg")
c.File("./assets/images/no-cover.jpg")
return
}
@@ -256,7 +268,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
_, err = os.Stat(safePath)
if err != nil {
log.Error("[getDocumentCover] File Should But Doesn't Exist:", err)
c.File("./assets/no-cover.jpg")
c.File("./assets/images/no-cover.jpg")
return
}
@@ -309,7 +321,7 @@ func (api *API) getDocumentCover(c *gin.Context) {
// Return Unknown Cover
if coverFile == "UNKNOWN" {
c.File("./assets/no-cover.jpg")
c.File("./assets/images/no-cover.jpg")
return
}
@@ -317,12 +329,12 @@ func (api *API) getDocumentCover(c *gin.Context) {
c.File(coverFilePath)
}
func (api *API) documentReader(c *gin.Context) {
func (api *API) getDocumentProgress(c *gin.Context) {
rUser, _ := c.Get("AuthorizedUser")
var rDoc requestDocumentID
if err := c.ShouldBindUri(&rDoc); err != nil {
log.Error("[documentReader] Invalid URI Bind")
log.Error("[getDocumentProgress] Invalid URI Bind")
errorPage(c, http.StatusNotFound, "Invalid document.")
return
}
@@ -333,7 +345,7 @@ func (api *API) documentReader(c *gin.Context) {
})
if err != nil && err != sql.ErrNoRows {
log.Error("[documentReader] UpsertDocument DB Error:", err)
log.Error("[getDocumentProgress] UpsertDocument DB Error:", err)
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("UpsertDocument DB Error: %v", err))
return
}
@@ -343,15 +355,18 @@ func (api *API) documentReader(c *gin.Context) {
DocumentID: rDoc.DocumentID,
})
if err != nil {
log.Error("[documentReader] GetDocumentWithStats DB Error:", err)
log.Error("[getDocumentProgress] GetDocumentWithStats DB Error:", err)
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentWithStats DB Error: %v", err))
return
}
c.HTML(http.StatusOK, "reader", gin.H{
"SearchEnabled": api.Config.SearchEnabled,
"Progress": progress.Progress,
"Data": document,
c.JSON(http.StatusOK, gin.H{
"id": document.ID,
"title": document.Title,
"author": document.Author,
"words": document.Words,
"progress": progress.Progress,
"percentage": document.Percentage,
})
}

View File

@@ -636,7 +636,7 @@ func (api *API) downloadDocument(c *gin.Context) {
}
// Force Download (Security)
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filepath.Base(*document.Filepath)))
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filepath.Base(*document.Filepath)))
c.File(filePath)
}