2019-04-12 23:29:42 +05:00
|
|
|
// 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"
|
2019-10-12 14:18:35 +05:00
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/pagination"
|
|
|
|
"go.dev.pztrn.name/fastpastebin/internal/templater"
|
2019-04-12 23:29:42 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET for "/pastes/", a list of publicly available pastes.
|
2019-12-22 00:56:43 +05:00
|
|
|
// Web interface version.
|
2021-11-20 22:19:58 +05:00
|
|
|
func pastesGET(ectx echo.Context) error {
|
2019-04-12 23:29:42 +05:00
|
|
|
// We should check if database connection available.
|
2022-06-26 22:26:39 +05:00
|
|
|
dbConn := ctx.Database.GetDatabaseConnection()
|
|
|
|
if ctx.Config.Database.Type != flatfiles.FlatFileDialect && dbConn == nil {
|
2021-06-15 00:11:58 +05:00
|
|
|
// nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.Redirect(http.StatusFound, "/database_not_available")
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
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
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
if pageFromParamRaw != "" {
|
|
|
|
pageRaw := regexInts.FindAllString(pageFromParamRaw, 1)[0]
|
|
|
|
page, _ = strconv.Atoi(pageRaw)
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:26:39 +05:00
|
|
|
ctx.Logger.Debug().Int("page", page).Msg("Requested page")
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// Get pastes IDs.
|
2022-06-26 22:26:39 +05:00
|
|
|
pastes, err3 := ctx.Database.GetPagedPastes(page)
|
|
|
|
ctx.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
|
2019-04-12 23:29:42 +05:00
|
|
|
|
2021-06-14 23:48:34 +05:00
|
|
|
pastesString := "No pastes to show."
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// Show "No pastes to show" on any error for now.
|
|
|
|
if err3 != nil {
|
2022-06-26 22:26:39 +05:00
|
|
|
ctx.Logger.Error().Err(err3).Msg("Failed to get pastes list from database")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +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
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusOK, noPastesToShowTpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(pastes) > 0 {
|
|
|
|
pastesString = ""
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
for _, paste := range pastes {
|
2019-04-12 23:29:42 +05:00
|
|
|
pasteDataMap := make(map[string]string)
|
2021-11-20 22:19:58 +05:00
|
|
|
pasteDataMap["pasteID"] = strconv.Itoa(paste.ID)
|
|
|
|
pasteDataMap["pasteTitle"] = paste.Title
|
|
|
|
pasteDataMap["pasteDate"] = paste.CreatedAt.Format("2006-01-02 @ 15:04:05") + " UTC"
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// Get max 4 lines of each paste.
|
2021-11-20 22:19:58 +05:00
|
|
|
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
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
if len(pasteDataSplitted) < 4 {
|
2021-11-20 22:19:58 +05:00
|
|
|
pasteData = paste.Data
|
2019-04-12 23:29:42 +05:00
|
|
|
} else {
|
|
|
|
pasteData = strings.Join(pasteDataSplitted[0:4], "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
pasteDataMap["pasteData"] = pasteData
|
2021-11-20 22:19:58 +05:00
|
|
|
pasteTpl := templater.GetRawTemplate(ectx, "pastelist_paste.html", pasteDataMap)
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
pastesString += pasteTpl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pagination.
|
2022-06-26 22:26:39 +05:00
|
|
|
pages := ctx.Database.GetPastesPages()
|
|
|
|
ctx.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
|
2019-04-12 23:29:42 +05:00
|
|
|
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
|
|
|
|
|
2021-11-20 22:19:58 +05:00
|
|
|
pasteListTpl := templater.GetTemplate(ectx, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})
|
2019-04-12 23:29:42 +05:00
|
|
|
|
2021-06-15 00:11:58 +05:00
|
|
|
// nolint:wrapcheck
|
2021-11-20 22:19:58 +05:00
|
|
|
return ectx.HTML(http.StatusOK, pasteListTpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|