fastpastebin/domains/pastes/pastes_get.go

112 lines
3.7 KiB
Go
Raw Normal View History

// Fast Paste Bin - uberfast and easy-to-use pastebin.
//
// Copyright (c) 2018, Stanislav N. aka pztrn and Fast Paste Bin
// developers.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package pastes
import (
"net/http"
"strconv"
"strings"
2021-06-14 23:48:34 +05:00
"github.com/labstack/echo"
"go.dev.pztrn.name/fastpastebin/internal/database/dialects/flatfiles"
"go.dev.pztrn.name/fastpastebin/internal/pagination"
"go.dev.pztrn.name/fastpastebin/internal/templater"
)
// GET for "/pastes/", a list of publicly available pastes.
// Web interface version.
func pastesGET(ectx echo.Context) error {
// We should check if database connection available.
dbConn := c.Database.GetDatabaseConnection()
2021-06-14 23:48:34 +05:00
if c.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
2021-06-15 00:11:58 +05:00
// nolint:wrapcheck
return ectx.Redirect(http.StatusFound, "/database_not_available")
}
pageFromParamRaw := ectx.Param("page")
2019-12-22 01:17:18 +05:00
2021-06-14 23:48:34 +05:00
page := 1
2019-12-22 01:17:18 +05:00
if pageFromParamRaw != "" {
pageRaw := regexInts.FindAllString(pageFromParamRaw, 1)[0]
page, _ = strconv.Atoi(pageRaw)
}
2019-04-13 00:05:22 +05:00
c.Logger.Debug().Int("page", page).Msg("Requested page")
// Get pastes IDs.
pastes, err3 := c.Database.GetPagedPastes(page)
2020-02-29 23:30:44 +05:00
c.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
2021-06-14 23:48:34 +05:00
pastesString := "No pastes to show."
// Show "No pastes to show" on any error for now.
if err3 != nil {
2019-04-13 00:05:22 +05:00
c.Logger.Error().Err(err3).Msg("Failed to get pastes list from database")
2019-12-22 01:17:18 +05:00
noPastesToShowTpl := templater.GetErrorTemplate(ectx, "No pastes to show.")
2019-12-22 01:17:18 +05:00
2021-06-15 00:11:58 +05:00
// nolint:wrapcheck
return ectx.HTML(http.StatusOK, noPastesToShowTpl)
}
if len(pastes) > 0 {
pastesString = ""
2019-12-22 01:17:18 +05:00
for _, paste := range pastes {
pasteDataMap := make(map[string]string)
pasteDataMap["pasteID"] = strconv.Itoa(paste.ID)
pasteDataMap["pasteTitle"] = paste.Title
pasteDataMap["pasteDate"] = paste.CreatedAt.Format("2006-01-02 @ 15:04:05") + " UTC"
// Get max 4 lines of each paste.
pasteDataSplitted := strings.Split(paste.Data, "\n")
2019-12-22 01:17:18 +05:00
2019-10-13 16:26:52 +05:00
var pasteData string
2019-12-22 01:17:18 +05:00
if len(pasteDataSplitted) < 4 {
pasteData = paste.Data
} else {
pasteData = strings.Join(pasteDataSplitted[0:4], "\n")
}
pasteDataMap["pasteData"] = pasteData
pasteTpl := templater.GetRawTemplate(ectx, "pastelist_paste.html", pasteDataMap)
pastesString += pasteTpl
}
}
// Pagination.
pages := c.Database.GetPastesPages()
2020-02-29 23:30:44 +05:00
c.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
pasteListTpl := templater.GetTemplate(ectx, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})
2021-06-15 00:11:58 +05:00
// nolint:wrapcheck
return ectx.HTML(http.StatusOK, pasteListTpl)
}