Private pastes, systemd unit file, design updates.

It is now possible to create private pastes, just check approriate
checkbox.

Added systemd unit file. Don't forget to fix it for your needs!

Updated design for pastes viewing, now paste information shows in
table. Improves mobile experience at least :).
This commit is contained in:
Stanislav Nikitin 2018-05-01 22:56:45 +05:00
parent 64c2d62a49
commit c926a58276
16 changed files with 183 additions and 26 deletions

View File

@ -48,7 +48,7 @@ fileb0x fileb0x.yml
```
Also if you're changed list of assets (by creating or deleting them) be sure
to fix files list if ``fileb0x.yml`` file!
to fix files list in ``fileb0x.yml`` file!
The rest is default - use linters, formatters, etc. VSCode with Go plugin is
recommended for developing as it will perform most of linting-formatting

View File

@ -1,5 +1,5 @@
// Code generated by fileb0x at "2018-05-01 18:42:52.800269995 +0500 +05 m=+0.035406927" from config file "fileb0x.yml" DO NOT EDIT.
// modification hash(b2d7b5e4ee752835a7e3a3ee5b9ac079.5c3b355fe5a9e5324e826569a62de332)
// Code generated by fileb0x at "2018-05-01 22:56:15.114143898 +0500 +05 m=+0.020887887" from config file "fileb0x.yml" DO NOT EDIT.
// modification hash(c89aff9b6b6a650f49dd49d615e3d3e0.e30269e2254cb92c4231bb47f2cedf3a)
package static

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fast Paste Bin</title>
<link rel="stylesheet" href="/static/css/bulma-0.7.0.min.css">
<link rel="stylesheet" href="/static/css/bulma-tooltip-1.0.4.min.css">
<script defer src="/static/js/fontawesome-5.0.7.js"></script>
<link rel="stylesheet" href="/static/css/style.css">
</head>
@ -72,6 +73,11 @@
</div>
</div>
</div>
<div class="field tooltip is-tooltip-bottom is-tooltip-multiline" data-tooltip="Should this paste be accessible only with special URL and not shown in pastes list?">
<label class="checkbox">
<input type="checkbox" name="paste-private" id="paste-private"> Private paste with unique URL?
</label>
</div>
<div class="field">
<label for="paste-language">Language:</label>
<div class="control">

View File

@ -32,16 +32,31 @@
</div>
</div>
</nav>
<section class="section paste-header">
<div class="columns content">
<div class="column is-10">
<h2>#{pasteID}, titled '{pasteTitle}'</h2>
<p>Pasted on
<strong>{pasteDate}</strong>, language: {pasteLanguage}</p>
</div>
<div class="column is-2">
<section class="section">
<div class="content">
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Language</th>
<th>Pasted on</th>
</tr>
</thead>
<tbody>
<tr>
<td>{pasteID}</td>
<td>{pasteTitle}</td>
<td>{pasteLanguage}</td>
<td>{pasteDate}</td>
</tr>
<tr>
<td colspan="4">
<a class="button" href="/paste/{pasteID}/raw">View raw</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<div class="paste-data">

View File

@ -0,0 +1,48 @@
// 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 migrations
import (
// stdlib
"database/sql"
)
func PrivatePastesUp(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` ADD `private` BOOL NOT NULL DEFAULT false COMMENT 'Private paste? If true - additional URL parameter (UNIX TIMESTAMP) of paste will be required to access.'")
if err != nil {
return err
}
return nil
}
func PrivatePastesDown(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE `pastes` DROP COLUMN `private`")
if err != nil {
return err
}
return nil
}

View File

@ -49,6 +49,7 @@ func Migrate() {
goose.SetDialect("mysql")
goose.AddNamedMigration("1_initial.go", InitialUp, nil)
goose.AddNamedMigration("2_paste_lang.go", PasteLangUp, PasteLangDown)
goose.AddNamedMigration("3_private_pastes.go", PrivatePastesUp, PrivatePastesDown)
// Add new migrations BEFORE this message.
dbConn := c.Database.GetDatabaseConnection()

View File

@ -80,6 +80,7 @@ custom:
# type: array of strings
- files:
- "assets/css/bulma-0.7.0.min.css"
- "assets/css/bulma-tooltip-1.0.4.min.css"
- "assets/css/bulma.css.map"
- "assets/css/style.css"
- "assets/js/fontawesome-5.0.7.js"

View File

@ -53,7 +53,7 @@ var (
regexInts = regexp.MustCompile("[0-9]+")
)
// GET for "/paste/PASTE_ID".
// GET for "/paste/PASTE_ID" and "/paste/PASTE_ID/TIMESTAMP" (private pastes).
func pasteGET(ec echo.Context) error {
errhtml, err := static.ReadFile("error.html")
if err != nil {
@ -74,6 +74,23 @@ func pasteGET(ec echo.Context) error {
return ec.HTML(http.StatusBadRequest, errhtmlAsString)
}
// Check if we have a private paste and it's parameters are correct.
if paste.Private {
tsProvidedStr := ec.Param("timestamp")
tsProvided, err2 := strconv.ParseInt(tsProvidedStr, 10, 64)
if err2 != nil {
c.Logger.Error().Msgf("Invalid timestamp '%s' provided for getting private paste #%d: %s", tsProvidedStr, pasteID, err2.Error())
errhtmlAsString := strings.Replace(string(errhtml), "{error}", "Paste #"+strconv.Itoa(pasteID)+" not found", 1)
return ec.HTML(http.StatusBadRequest, errhtmlAsString)
}
pasteTs := paste.CreatedAt.Unix()
if tsProvided != pasteTs {
c.Logger.Error().Msgf("Incorrect timestamp '%v' provided for private paste #%d, waiting for %v", tsProvidedStr, pasteID, strconv.FormatInt(pasteTs, 10))
errhtmlAsString := strings.Replace(string(errhtml), "{error}", "Paste #"+strconv.Itoa(pasteID)+" not found", 1)
return ec.HTML(http.StatusBadRequest, errhtmlAsString)
}
}
pasteHTML, err2 := static.ReadFile("paste.html")
if err2 != nil {
return ec.String(http.StatusNotFound, "parse.html wasn't found!")
@ -85,6 +102,12 @@ func pasteGET(ec echo.Context) error {
pasteHTMLAsString = strings.Replace(pasteHTMLAsString, "{pasteDate}", paste.CreatedAt.Format("2006-01-02 @ 15:04:05"), 1)
pasteHTMLAsString = strings.Replace(pasteHTMLAsString, "{pasteLanguage}", paste.Language, 1)
if paste.Private {
pasteHTMLAsString = strings.Replace(pasteHTMLAsString, "{privateMode}", "Private", 1)
} else {
pasteHTMLAsString = strings.Replace(pasteHTMLAsString, "{privateMode}", "Public", 1)
}
// Highlight.
// Get lexer.
lexer := lexers.Get(paste.Language)
@ -193,6 +216,12 @@ func pastePOST(ec echo.Context) error {
}
}
// Private paste?
paste.Private = false
if params["paste-private"][0] == "on" {
paste.Private = true
}
id, err2 := Save(paste)
if err2 != nil {
c.Logger.Debug().Msgf("Failed to save paste: %s", err2.Error())
@ -202,6 +231,12 @@ func pastePOST(ec echo.Context) error {
newPasteIDAsString := strconv.FormatInt(id, 10)
c.Logger.Debug().Msgf("Paste saved, URL: /paste/" + newPasteIDAsString)
// Private pastes have it's timestamp in URL.
if paste.Private {
return ec.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString+"/"+strconv.FormatInt(paste.CreatedAt.Unix(), 10))
}
return ec.Redirect(http.StatusFound, "/paste/"+newPasteIDAsString)
}

View File

@ -41,11 +41,16 @@ func New(cc *context.Context) {
// New paste.
c.Echo.POST("/paste/", pastePOST)
// Show paste.
// Show public paste.
c.Echo.GET("/paste/:id", pasteGET)
// Show RAW.
// Show RAW representation of public paste.
c.Echo.GET("/paste/:id/raw", pasteRawGET)
// Show private paste.
c.Echo.GET("/paste/:id/:timestamp", pasteGET)
// Show RAW representation of private paste.
c.Echo.GET("/paste/:id/:timestamp/raw", pasteRawGET)
// Pastes list.
c.Echo.GET("/pastes/", pastesGET)
c.Echo.GET("/pastes/:page", pastesGET)

View File

@ -56,6 +56,5 @@ type Paste struct {
KeepFor int `db:"keep_for"`
KeepForUnitType int `db:"keep_for_unit_type"`
Language string `db:"language"`
Private bool `db:"private"`
}
func (p *Paste) Highlight() {}

View File

@ -52,7 +52,7 @@ func GetPagedPastes(page int) ([]Paste, error) {
startPagination = (page - 1) * PAGINATION
}
err := dbConn.Select(&pastes, dbConn.Rebind("SELECT * FROM `pastes` ORDER BY id DESC LIMIT ? OFFSET ?"), PAGINATION, startPagination)
err := dbConn.Select(&pastes, dbConn.Rebind("SELECT * FROM `pastes` WHERE private != true ORDER BY id DESC LIMIT ? OFFSET ?"), PAGINATION, startPagination)
if err != nil {
return nil, err
}
@ -84,7 +84,7 @@ func GetPastesPages() int {
// Save saves paste to database and returns it's ID.
func Save(p *Paste) (int64, error) {
dbConn := c.Database.GetDatabaseConnection()
result, err := dbConn.NamedExec("INSERT INTO `pastes` (title, data, created_at, keep_for, keep_for_unit_type, language) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language)", p)
result, err := dbConn.NamedExec("INSERT INTO `pastes` (title, data, created_at, keep_for, keep_for_unit_type, language, private) VALUES (:title, :data, :created_at, :keep_for, :keep_for_unit_type, :language, :private)", p)
if err != nil {
return 0, err
}

View File

@ -0,0 +1,11 @@
[Unit]
Description=fastpastebin
After=network.target
[Service]
ExecStart=/usr/bin/fastpastebin -config /etc/fastpastebin.yaml
User=paste
Group=paste
[Install]
WantedBy=multi-user.target