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.
|
2019-04-12 23:29:42 +05:00
|
|
|
func pastesGET(ec 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
|
2019-04-12 23:29:42 +05:00
|
|
|
return ec.Redirect(http.StatusFound, "/database_not_available")
|
|
|
|
}
|
|
|
|
|
|
|
|
pageFromParamRaw := ec.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)
|
|
|
|
}
|
|
|
|
|
2019-04-13 00:05:22 +05:00
|
|
|
c.Logger.Debug().Int("page", page).Msg("Requested page")
|
2019-04-12 23:29:42 +05:00
|
|
|
|
|
|
|
// 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")
|
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 {
|
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
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
noPastesToShowTpl := templater.GetErrorTemplate(ec, "No pastes to show.")
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2021-06-15 00:11:58 +05:00
|
|
|
// nolint:wrapcheck
|
2019-04-12 23:29:42 +05:00
|
|
|
return ec.HTML(http.StatusOK, noPastesToShowTpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pastes) > 0 {
|
|
|
|
pastesString = ""
|
2019-12-22 01:17:18 +05:00
|
|
|
|
2019-04-12 23:29:42 +05:00
|
|
|
for i := range pastes {
|
|
|
|
pasteDataMap := make(map[string]string)
|
|
|
|
pasteDataMap["pasteID"] = strconv.Itoa(pastes[i].ID)
|
|
|
|
pasteDataMap["pasteTitle"] = pastes[i].Title
|
|
|
|
pasteDataMap["pasteDate"] = pastes[i].CreatedAt.Format("2006-01-02 @ 15:04:05") + " UTC"
|
|
|
|
|
|
|
|
// Get max 4 lines of each paste.
|
|
|
|
pasteDataSplitted := strings.Split(pastes[i].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 {
|
|
|
|
pasteData = pastes[i].Data
|
|
|
|
} else {
|
|
|
|
pasteData = strings.Join(pasteDataSplitted[0:4], "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
pasteDataMap["pasteData"] = pasteData
|
|
|
|
pasteTpl := templater.GetRawTemplate(ec, "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")
|
2019-04-12 23:29:42 +05:00
|
|
|
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
|
|
|
|
|
|
|
|
pasteListTpl := templater.GetTemplate(ec, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})
|
|
|
|
|
2021-06-15 00:11:58 +05:00
|
|
|
// nolint:wrapcheck
|
2019-12-22 01:17:18 +05:00
|
|
|
return ec.HTML(http.StatusOK, pasteListTpl)
|
2019-04-12 23:29:42 +05:00
|
|
|
}
|