Cesura / pastey

A minimal, self-hosted paste platform
https://pastey.link/
BSD 3-Clause "New" or "Revised" License
106 stars 12 forks source link

[Feature Request] retrieve paste content via cli #13

Closed g-rock84 closed 1 year ago

g-rock84 commented 3 years ago

Unless pastey can already do this and I just reinvented the wheel big time, I'd like to propose a feature to allow for paste content retrieval from cli. Here are the changes I experimented with:

common.py (line 105) I replaced uuid.uuid4() with the below function. A UUID is way too much to type in my humble opinion and using a random, six character combination of uppercase/lowercase letters and numbers that gives you 56,800,235,584 possible combinations. It would take you 1,800 years to burn through those if you created a new paste every second. Five characters would still take 31 years. ```python def id_generator(size=6, chars=string.ascii_uppercase + string.digits + string.ascii_lowercase ): return ''.join(random.SystemRandom().choice(chars) for _ in range(size)) ``` [https://github.com/g-rock84/pastey/blob/main/pastey/common.py](https://github.com/g-rock84/pastey/blob/main/pastey/common.py)
functions.py (line 82) ```python unique_id = str(common.id_generator()) ``` [https://github.com/g-rock84/pastey/blob/main/pastey/functions.py](https://github.com/g-rock84/pastey/blob/main/pastey/functions.py)
routes.py (line 191) ```python @app.route('/content/', methods = ['GET']) @limiter.limit(config.rate_limit, exempt_when=lambda: common.verify_whitelist(common.get_source_ip(request))) def get_paste(unique_id): source_ip = common.get_source_ip(request) # Check if restrict pasting to whitelist CIDRs is enabled if config.restrict_get and not common.verify_whitelist(source_ip): abort(401) paste_file = config.data_directory + "/" + unique_id if path.exists(paste_file): with open(paste_file, 'r') as f: paste = json.loads(f.read()) return paste["content"], 200 else: abort(400) ``` [https://github.com/g-rock84/pastey/blob/main/pastey/routes.py](https://github.com/g-rock84/pastey/blob/main/pastey/routes.py)

Thoughts? It could definitely be more polished/efficient/pretty, but it works. I've been using it on my network for a few days now without any issues. Of course, I haven't really given it a full shakedown. I think it is a pretty solid proof of concept if anything.

If this does not fit into what you envision for pastey then I will make sure to clearly indicate the differences between the two.

Either way, thank you for all your hard work. This is an awesome and extremely useful utility!

stvhay commented 2 years ago

Look at the new /view//raw endpoint @Cesura added which may get you most of the way there.

As for the length of the uuid, i tend to agree but i made changes to my private repo to reflect. I did 5 characters in mine and was thinking of reducing it to 3 since my pastebin is just for me. If there is interest i could make a patch that minimizes the url length.

stvhay commented 2 years ago

This is what I went with

id_len = int(math.ceil( 2*math.log(len(listdir(config.data_directory))+2)/math.log(256) )) unique_id = secrets.token_urlsafe(id_len)

The mathematics of this are to calculate the number of digits (in bytes) needed to represent the square of the number of current entries in the pastebin. (+2 to avoid an edge case at 0 and 1.) The secrets.token_url is probably overkill for randomness, but works quite well to make a base64-encoded string of that number of bytes. The square of the number is used because that is a measure of unlikeliness of collisions in the namespace. 10 entries will have >100 spots to occupy, etc.

You can see an example of it working here: https://bin.st5ve.com/view/kR4/raw

Should I make a pull request?

g-rock84 commented 1 year ago

This is what I went with

id_len = int(math.ceil( 2*math.log(len(listdir(config.data_directory))+2)/math.log(256) )) unique_id = secrets.token_urlsafe(id_len)

SUPER sorry that it has taken me almost two years to respond! I packed up and moved across several states since this. Anyway, this is pure genius! Way better than my solution. Bravo! I'll finally close this.