microbin icon indicating copy to clipboard operation
microbin copied to clipboard

[Feature Request] better CLI upload

Open luochen1990 opened this issue 2 years ago • 7 comments

I found that rustypaste provide a CLI tool, do we support that?

This is talked about in the Document at chapter "Use MicroBin from the console with cURL":

curl -d "expiration=10min&content=$( < mypastafile.txt )" -X POST https://microbin.myserver.com/upload

But this seems buggy in some case:

  1. if there is special characters which needs URIencode.
  2. when the file is not small, I got zsh: argument list too long: curl

I hope to define a microbin-upload command so that we can use it like:

microbin-upload test-code.py
cat test-code.py | microbin-upload

And hoping that:

  1. all special charactors are processed correctly.
  2. if there is an file postfix, then the highlight format is detectely automatically.
  3. the file size is not limited until it hits the hard limit of the microbin server config.

Is there any easy way to implement that?

luochen1990 avatar Jul 01 '23 15:07 luochen1990

Hi there, thanks for your feature request! Currently curl is the only way to upload contents from the terminal, and I am sure it is quite difficult to use, especially on v2. I think it is a good idea to create a little CLI tool for this and I don't imagine it being too difficult. I would like to have v2 ready to go and published, and then we can start working on this tool (in a new repository)! 🙂

szabodanika avatar Jul 02 '23 07:07 szabodanika

My alias for microbin v1 has been this

python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())' | curl -is -X POST https://microbin.example.com/upload -F content=@- -F expiration=10d | grep ^location: | cut -d  -f2-

breakdown:

  • use perl to remove trailing newlines, needed for microbin to recognize urls
  • upload stdin using curl, printing response headers
  • filter for the location header
  • isolate the destination url

This command could be simplified greatly with the two following additions:

  • be more lenient on whitespace when determining if the paste is a url and should support redirection
  • return the resulting url in the body in addition to the location header

I am against making a cli utility, and would rather prefer a sanctioned way to use curl. For reference, here are some great examples which make using curl/nc alone very simple:

  • http://ix.io/
  • http://termbin.com/

EDIT: the perl chomp stripped newlines. Now i use python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())'

pbsds avatar Jul 02 '23 12:07 pbsds

I am against making a cli utility, and would rather prefer a sanctioned way to use curl.

That's right, It will be great if we can define microbin-upload as simple as an alias of curl, but that need us to provide an extra server API which doesn't exists right now (or extend current API to support more use case), I think that is also a good solution of current feature request :)

luochen1990 avatar Jul 03 '23 08:07 luochen1990

Also interesting is that one can expand a shell alias with Ctrl+alt+e, which allows you to modify default parameters in the alias

pbsds avatar Jul 03 '23 17:07 pbsds

PS: I think "auto detect file type (for syntax highlighting) via filename extension" will be a sweet feature also.

luochen1990 avatar Sep 06 '23 02:09 luochen1990

I can no longer find documentation regarding usage from a shell, so I had to refer to this issue and dev tools to figure out what params microbin takes after installing it.

I've added the following zsh functions:

mbclean() {
  perl -lne 's!/upload/!/p/!; print $1 if /^location: (.*)$/i'
}
mbf() {
  if [[ -n $1 && -f $1 ]]; then
    curl -is -X POST https://$HOST/upload -F "file=@$1" -F "uploader_password=$MICROBIN_PASS" | mbclean
  else
    echo "Usage: mbf <filename>"
  fi
}

mbt() {
  FILE="${1:-/dev/stdin}"
  curl -is -X POST https://$HOST/upload -F "content=@$FILE" -F "uploader_password=$MICROBIN_PASS"  | mbclean
}

Where mbt allows both piping and specifying a text file via the first argument, while mbf allows uploading arbitrary files.

While I think having microbin be easy to use with curl is great, some examples in the docs would be helpful for people. Not sure if there is API documentation anywhere.

vaskozl avatar Nov 08 '23 01:11 vaskozl

My use case might be a bit different, but IMO it still counts for something: upload from cli and return the QR code directly, maybe determined via curl header like --header 'Accept: image/svg'.

To be honest I have no idea if it's possible without some RESTful API. For now it's possible to upload via cli, get the URL, and change upload to qr in it, then curl & parse the results into a svg that can be saved. But I have to manually open it, which makes no difference to just return the qr URL and open it in browser.

Vinfall avatar Nov 08 '23 02:11 Vinfall