diff --git a/pkg/html/html.go b/pkg/html/html.go index 7177902..2a0a45a 100644 --- a/pkg/html/html.go +++ b/pkg/html/html.go @@ -2,6 +2,7 @@ package html import ( "fmt" + "log" "math/rand" "strings" @@ -79,69 +80,31 @@ textarea { } ` -func FullHTML(h HTMLData) string { - var sb strings.Builder - var errorBuilder strings.Builder +const ( + // errorListItem is shown after adding a feed. It shows if error or success. + errorListItem = `
  • %s - %s
  • ` - FeedCount := 0 - DatabaseSize := stats.GetDBSize() - - // This is the error message that will be displayed if there are any errors - if len(h.ParseResult) > 0 { - errorBuilder.WriteString("") - } - StatusMsg := errorBuilder.String() - - sb.WriteString(` - - - - - - - `) - - if h.Description != "" { - sb.WriteString(``) - } - - if h.Keywords != "" { - sb.WriteString(``) - } - - if h.Author != "" { - sb.WriteString(``) - } - - if h.CanonicalURL != "" { - sb.WriteString(``) - } - - sb.WriteString(` - ` + h.Title + ` - - - - ` + StatusMsg + ` + // htmlTemplate is the HTML template for the entire page. + htmlTemplate = ` + + + + + + + + + %s + + + + %s

    FeedVault

    - Archive of web feeds. ` + fmt.Sprintf("%d", FeedCount) + ` feeds. ~` + DatabaseSize + `. + Archive of web feeds. %d feeds. ~%s.
    -
    @@ -152,7 +115,7 @@ func FullHTML(h HTMLData) string {
    - Home | Feeds | API + Home | Feeds | API
    GitHub | Donate @@ -161,10 +124,10 @@ func FullHTML(h HTMLData) string {
    -
    - ` + h.Content + ` -
    -
    +
    + %s +
    +
    @@ -178,14 +141,41 @@ func FullHTML(h HTMLData) string { hello@feedvault.se
    - ` + quotes.FunMsg[rand.Intn(len(quotes.FunMsg))] + ` + %s
    - - `) - - return sb.String() + + ` +) +func buildErrorList(parseResults []models.ParseResult) string { + var errorBuilder strings.Builder + if len(parseResults) > 0 { + errorBuilder.WriteString("
      ") + for _, result := range parseResults { + // CSS class for the list item. Green for success, red for error. + listItemClass := "success" + if result.IsError { + listItemClass = "error" + } + errorBuilder.WriteString(fmt.Sprintf(errorListItem, listItemClass, result.FeedURL, result.FeedURL, result.Msg)) + } + errorBuilder.WriteString("
    ") + } + return errorBuilder.String() +} + +func FullHTML(h HTMLData) string { + statusMsg := buildErrorList(h.ParseResult) + feedCount := 0 + databaseSize, err := stats.GetDBSize() + if err != nil { + databaseSize = "0 KiB" + log.Println("Error getting database size:", err) + } + + funMsg := quotes.FunMsg[rand.Intn(len(quotes.FunMsg))] + return fmt.Sprintf(htmlTemplate, h.Description, h.Keywords, h.Author, h.CanonicalURL, h.Title, style, statusMsg, feedCount, databaseSize, h.Content, funMsg) } diff --git a/pkg/models/models.go b/pkg/models/models.go index 730a030..0172475 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -1,32 +1,7 @@ package models -import ( - "net/http" - - "github.com/TheLovinator1/FeedVault/pkg/stats" -) - -type TemplateData struct { - Title string - Description string - Keywords string - Author string - CanonicalURL string - FeedCount int - DatabaseSize string - Request *http.Request - ParseErrors []ParseResult -} - type ParseResult struct { FeedURL string Msg string IsError bool } - -func (d *TemplateData) GetDatabaseSizeAndFeedCount() { - // TODO: Get the feed count from the database - // TODO: Add amount of entries - // TODO: Add amount of users - d.DatabaseSize = stats.GetDBSize() -} diff --git a/pkg/stats/stats.go b/pkg/stats/stats.go index 98fbc8f..045b977 100644 --- a/pkg/stats/stats.go +++ b/pkg/stats/stats.go @@ -2,7 +2,6 @@ package stats import ( "fmt" - "log" "os" "time" ) @@ -14,20 +13,16 @@ type Cache struct { var cache Cache -// Get the size of the database and return as nearest human readable size. -// -// e.g. 1.23 KiB, 4.56 MiB, 7.89 GiB, 0.12 TiB -// The size is cached for 10 minutes -func GetDBSize() string { +func GetDBSize() (string, error) { // If cache is less than 10 minutes old, return cached data if time.Since(cache.timestamp).Minutes() < 10 { - return cache.data + return cache.data, nil } + // TODO: This should be read from the environment fileInfo, err := os.Stat("feedvault.db") if err != nil { - log.Println("Error getting file info:", err) - return "0 B" + return "", err } // Get the file size in bytes @@ -35,16 +30,23 @@ func GetDBSize() string { // Convert to human readable size and append the unit (KiB, MiB, GiB, TiB) var size float64 + if fileSize < 1024*1024 { + // If the file size is less than 1 KiB, return the size in bytes size = float64(fileSize) / 1024 cache.data = fmt.Sprintf("%.2f KiB", size) + } else if fileSize < 1024*1024*1024 { + // If the file size is less than 1 MiB, return the size in KiB size = float64(fileSize) / (1024 * 1024) cache.data = fmt.Sprintf("%.2f MiB", size) + } else if fileSize < 1024*1024*1024*1024 { + // If the file size is less than 1 GiB, return the size in MiB size = float64(fileSize) / (1024 * 1024 * 1024) cache.data = fmt.Sprintf("%.2f GiB", size) } else { + // If the file size is 1 GiB or more, return the size in TiB size = float64(fileSize) / (1024 * 1024 * 1024 * 1024) cache.data = fmt.Sprintf("%.2f TiB", size) } @@ -52,7 +54,6 @@ func GetDBSize() string { // Update cache timestamp cache.timestamp = time.Now() - log.Println("Returning database size, it is", cache.data) - - return cache.data + // Return the human readable size + return cache.data, nil }