Make script use json endpoint + add support for raw viewing

pull/22/head
Cesura 2 years ago
parent 3ae36db717
commit 0eff189cd9

@ -1,6 +1,8 @@
FROM tensorflow/tensorflow:2.2.0
WORKDIR /app
COPY requirements.txt /app
RUN mkdir -p /app/data && pip install -r /app/requirements.txt
COPY . /app/
RUN pip install -r /app/requirements.txt
EXPOSE 5000
WORKDIR /app
ENV PASTEY_DATA_DIRECTORY=/app/data
ENTRYPOINT ["python3", "app.py"]

@ -2,7 +2,8 @@ FROM continuumio/conda-ci-linux-64-python3.8
USER root
COPY . /app/
RUN conda install -c anaconda tensorflow=2.2.0 pip
RUN pip install -r /app/requirements.txt
RUN mkdir -p /app/data && pip install -r /app/requirements.txt
EXPOSE 5000
WORKDIR /app
ENV PASTEY_DATA_DIRECTORY=/app/data
ENTRYPOINT ["python3", "app.py"]

@ -6,7 +6,7 @@ from os import environ
from distutils.util import strtobool
from threading import Thread
pastey_version = "0.4.2"
pastey_version = "0.5"
loaded_config = {}
loaded_themes = []

@ -1,11 +1,12 @@
from __main__ import app, limiter, loaded_config
from . import config, common, functions
from flask import Flask, render_template, request, redirect, abort
from flask import Flask, render_template, request, redirect, abort, make_response
from urllib.parse import quote
from datetime import datetime
from os import environ
import json
from base64 import b64decode
# Load themes
loaded_themes = common.get_themes()
@ -64,11 +65,11 @@ def config_page():
# View paste page
@app.route("/view/<unique_id>")
def view(unique_id):
content = functions.get_paste(unique_id)
paste = functions.get_paste(unique_id)
if content is not None:
if paste is not None:
return render_template("view.html",
paste=content,
paste=paste,
url=common.build_url(request, "/view/" + unique_id),
whitelisted=common.verify_whitelist(common.get_source_ip(request)),
active_theme=common.set_theme(request),
@ -76,6 +77,18 @@ def view(unique_id):
else:
abort(404)
# View paste page (raw)
@app.route("/view/<unique_id>/raw")
def view_raw(unique_id):
paste = functions.get_paste(unique_id)
if paste is not None:
response = make_response(paste['content'], 200)
response.mimetype = "text/plain"
return response
else:
abort(404)
# Delete paste
@app.route("/delete/<unique_id>")
def delete(unique_id):
@ -88,7 +101,7 @@ def delete(unique_id):
# Script download
@app.route("/pastey")
def pastey_script():
return render_template('pastey.sh', endpoint=common.build_url(request, "/raw")), 200, {
return render_template('pastey.sh', endpoint=common.build_url(request, "/paste")), 200, {
'Content-Disposition': 'attachment; filename="pastey"',
'Content-Type': 'text/plain'
}
@ -176,6 +189,12 @@ def paste_json():
abort(400)
content = paste['content']
# Check if content was base64 encoded (from the CLI client typically)
from_client = False
if 'base64' in paste and paste['base64'] == True:
from_client = True
content = b64decode(content).decode("utf-8")
# Optional fields
title = paste['title'] if ('title' in paste and paste['title'].strip() != "") else "Untitled"
single = paste['single'] if ('single' in paste and type(paste['single']) == bool) else False
@ -186,9 +205,15 @@ def paste_json():
# Create paste
unique_id, key = functions.new_paste(title, content, source_ip, expires=expiration, single=single, encrypt=encrypt, language=language)
if encrypt:
if from_client:
return common.build_url(request, "/view/" + unique_id + "#" + quote(key)), 200
else:
return {
"link": common.build_url(request, "/view/" + unique_id + "#" + quote(key))
}, 200
else:
if from_client:
return common.build_url(request, "/view/" + unique_id), 200
else:
return {
"link": common.build_url(request, "/view/" + unique_id)

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 35 KiB

@ -121,11 +121,55 @@
<div class="col-md-12">
<h1 class="mb-3 h4">Download Script</h1>
<p>Pastey provides a script that can be used to paste output directly from the command line:</p>
<pre>$ cat /var/log/nginx.log | pastey</pre>
<pre>$ echo "Hello, Pastey!" | pastey</pre>
<pre>$ pastey -f /var/log/nginx.log</pre>
<pre>$ echo "Hello, Pastey!" | pastey -c -</pre>
<p style="font-weight:bold;">Download the following, make it executable, and put it in your system PATH to be used anywhere!</p>
<!-- Help button -->
<br />
<a
class="btn btn-primary"
data-mdb-toggle="collapse"
href="#script-help"
role="button"
aria-expanded="false"
aria-controls="script-help"
>
<i class="fas fa-info"></i>&nbsp;&nbsp;&nbsp;Show script usage
</a>
<div class="collapse mt-3" style="text-align: left !important; margin-left:25%;" id="script-help">
<br /><pre>Usage: pastey [-h] [-v] [-f] -p param_value arg1 [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-c, --content Pass the content of the paste in a simple argument
-e, --encrypt Encrypt the paste content
-f, --file Read the content from this file. If file is "-", read from stdin
-s, --single Create a paste that expires after the first view
-t, --title Set the title of the paste
-x, --expiration
Set the time in hours after which the paste expires
-- Stop further option parsing
Arguments passed after the -- option are evaluated
as a command, and that command's output is pasted.
The full command is used a the title.
If zero arguments are passed,
or none of --content, --file or -- are passed,
content is read from stdin.</pre>
</div>
</div><br />
</div>
</div><br /><br />
<div class="row justify-content-md-center">
<div class="col-md-4">
@ -136,7 +180,7 @@
<button type="button" class="btn btn-success text-nowrap"><i class="fas fa-download"></i>&nbsp;&nbsp;&nbsp;Download</button>
</a>
</div>
</div><br />
</div><br /><br />
<div class="row text-center">
<div class="col-md-12">

@ -7,7 +7,7 @@
# script framework based on https://betterdev.blog/minimal-safe-bash-script-template/
# initially adapted and written by Serge van Ginderachter <serge@vanginderachter.be>
set -Eeuo pipefail
set -Eeo pipefail
#execute=(echo popo)trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
@ -74,8 +74,8 @@ parse_params() {
content=
title=
file=
single=
encrypt=
single="false"
encrypt="false"
while (( "$#" ))
do
@ -114,12 +114,12 @@ parse_params() {
-s | --single)
shift || :
single="-F single="
single="true"
;;
-e | --encrypt)
shift || :
encrypt="-F encrypt="
encrypt="true"
;;
--)
@ -170,7 +170,7 @@ parse_options(){
content="$(</dev/stdin)"
elif [ -r ${file} ]
then
content="$(<${file})"
content="$(cat ${file} | tr -d '\0')"
else
die "Could not read from ${file}"
fi
@ -206,15 +206,21 @@ parse_options(){
# just do it now
paste_it() {
curl \
content=$(echo -n "${content}" | base64)
payload="{
\"content\": \"${content}\",
\"title\": \"${title}\",
\"expiration\": \"${expiration}\",
\"encrypt\": ${encrypt},
\"single\": ${single},
\"base64\": true
}"
echo -n $payload | curl \
-X POST \
-F "cli=" \
-F "title=${title}" \
-F "content=${content}" \
-F "expiration=${expiration}" \
${encrypt} ${single} \
${PASTEY_ENDPOINT}
-H "Content-Type: application/json" \
--data-binary @- \
${PASTEY_ENDPOINT}; echo
}
## main execution

@ -98,7 +98,12 @@
<div class="pastey-title">
<h1 class="mb-3 h2" id="pastey-title">{{ paste.title }}</h1>
</div>
<span style="font-weight:bold;">Format: </span><span>{{ paste.language }}</span><br />
<span style="font-weight:bold;">Format: </span>{{ paste.language }}</span>
{% if not paste.encrypted %}
<span>&nbsp;(<a href='{{ url }}/raw' target='_blank'><i class="fas fa-eye"></i>&nbsp;View Raw</a>)</span>
{% endif %}<br />
<span style="font-weight:bold;">Date: </span><span>{{ paste.timestamp }}</span><br />
{% if paste.expiration is defined and paste.expiration != "" %}
<span style="font-weight:bold;">Expires: </span><span>{{ paste.expiration }}</span><br />

Loading…
Cancel
Save