Add text area to upload copied text #38

Open
opened 2020-01-24 16:21:21 +01:00 by minnozz · 5 comments
Collaborator
No description provided.
electricdusk added the
feature
good-beginner-bug
labels 2020-03-23 17:06:43 +01:00
Author
Collaborator

I made a very hacky first attempt by editing the HTML of the main page in the inspector:

HTML:

<section>
    <form action="/" method="post" enctype="multipart/form-data" id="pasteForm">
          <textarea name="file" form="pasteForm"></textarea>

        
        <input class="formSubmit" type="submit" value="paste!" id="pasteSubmit">
    </form>
</section>

Request:

POST / HTTP/1.1
Host: hashru.link
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary=---------------------------360978277341281805751182407705
Content-Length: 188
Origin: https://hashru.link
DNT: 1
Connection: keep-alive
Referer: https://hashru.link/
Upgrade-Insecure-Requests: 1

-----------------------------360978277341281805751182407705
Content-Disposition: form-data; name="file"

This is a test!
-----------------------------360978277341281805751182407705--

Response:

HTTP/2 400 Bad Request
server: nginx/1.16.1
date: Fri, 03 Apr 2020 09:06:53 GMT
content-type: text/html; charset=utf-8
content-length: 336
X-Firefox-Spdy: h2

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
Error - rushlink
</title>
        <link rel="stylesheet" type="text/css" href="/css/main.css" />
        </head>
    <body>
        
        <pre>
no &#39;file&#39; and no &#39;shorten&#39; fields given in form

        </pre>
        
    </body>
</html>
I made a very hacky first attempt by editing the HTML of the main page in the inspector: HTML: ``` <section> <form action="/" method="post" enctype="multipart/form-data" id="pasteForm"> <textarea name="file" form="pasteForm"></textarea> <input class="formSubmit" type="submit" value="paste!" id="pasteSubmit"> </form> </section> ``` Request: ``` POST / HTTP/1.1 Host: hashru.link User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br Content-Type: multipart/form-data; boundary=---------------------------360978277341281805751182407705 Content-Length: 188 Origin: https://hashru.link DNT: 1 Connection: keep-alive Referer: https://hashru.link/ Upgrade-Insecure-Requests: 1 -----------------------------360978277341281805751182407705 Content-Disposition: form-data; name="file" This is a test! -----------------------------360978277341281805751182407705-- ``` Response: ``` HTTP/2 400 Bad Request server: nginx/1.16.1 date: Fri, 03 Apr 2020 09:06:53 GMT content-type: text/html; charset=utf-8 content-length: 336 X-Firefox-Spdy: h2 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> Error - rushlink </title> <link rel="stylesheet" type="text/css" href="/css/main.css" /> </head> <body> <pre> no &#39;file&#39; and no &#39;shorten&#39; fields given in form </pre> </body> </html> ```
Author
Collaborator

The POST data is parsed here: https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L212

The method used is https://golang.org/pkg/net/http/#Request.FormFile

A <textarea> is not posted as a file, so that may be the problem.

I suggest adding a third possible POST field called paste for POSTing a regular form field that is not a file.

The POST data is parsed here: https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L212 The method used is https://golang.org/pkg/net/http/#Request.FormFile A `<textarea>` is not posted as a file, so that may be the problem. I suggest adding a third possible POST field called `paste` for POSTing a regular form field that is not a file.
minnozz was assigned by electricdusk 2020-04-03 12:07:30 +02:00
Author
Collaborator

internal/db/paste.go defines 3 paste types:

  • PasteTypeRedirect: used for shorten=
  • PasteTypeFileUpload: used for file=
  • PasteTypePaste: currently unused; seems to fit the definition of this feature

Open questions:

  • Which form field name should we use?
    Suggestions: paste=, content=, text=, data=, string=, body=
  • Should we autodetect the content type of the paste the same way as with file=? Or should it always be text/plain?
  • Where should the content be stored: in the database or in a file?
    We could even define a cutoff below which the content is stored in the database, and above which it is stored in a file. This would increase complexity though; we would need two new paste types instead of one.
`internal/db/paste.go` defines 3 paste types: - `PasteTypeRedirect`: used for `shorten=` - `PasteTypeFileUpload`: used for `file=` - `PasteTypePaste`: currently unused; seems to fit the definition of this feature Open questions: - Which form field name should we use?\ Suggestions: `paste=`, `content=`, `text=`, `data=`, `string=`, `body=` - Should we autodetect the content type of the paste the same way as with `file=`? Or should it always be `text/plain`? - Where should the content be stored: in the database or in a file?\ We could even define a cutoff below which the content is stored in the database, and above which it is stored in a file. This would increase complexity though; we would need two new paste types instead of one.
Owner

I forgot that I had introduced a PasteTypePaste in the past. We should probably use that type, yeah.

I think text= or paste= might be the best field type name, because I think it would fit best into the curl command:

curl -F'text=Hello World!' https://hashru.link
# or
curl -F'paste=Hello World!' https://hashru.link

With respect to saving the content in the database, I think we should be careful with storing big chunks of data into the database. Consider that the admin might choose to put the database on an SSD for performance, and the files on a hard disk, we would not want the database file to grow too quickly. I would always restrict a paste stored in the database to a couple of kilobytes.

I personally feel like it could be best to just always store it to disk. This is also easier for the sake of consistency. Later we could just store or cache all blobs with a small size (say <= 4KB) in the database.

I forgot that I had introduced a `PasteTypePaste` in the past. We should probably use that type, yeah. I think `text=` or `paste=` might be the best field type name, because I think it would fit best into the curl command: ```shell curl -F'text=Hello World!' https://hashru.link # or curl -F'paste=Hello World!' https://hashru.link ``` With respect to saving the content in the database, I think we should be careful with storing big chunks of data into the database. Consider that the admin might choose to put the database on an SSD for performance, and the files on a hard disk, we would not want the database file to grow too quickly. I would always restrict a paste stored in the database to a couple of kilobytes. I personally feel like it could be best to just *always* store it to disk. This is also easier for the sake of consistency. Later we could just store or cache all blobs with a small size (say <= 4KB) in the database.
Owner

I am unsure about the content type, but I lean towards forcing the content type to be text/plain.

I feel like if somebody pastes plain text into a <textarea>, then it always text/plain, expecially because we will likely not be adding an extension to the URL (I think we wouldn't want to risk guessing the wrong extension).

To achieve this, we should probably update db.NewFileUpload and add a contentType string argument, which will default to autodetect when the string is "".

I am unsure about the content type, but I lean towards forcing the content type to be `text/plain`. I feel like if somebody pastes plain text into a `<textarea>`, then it always `text/plain`, expecially because we will likely not be adding an extension to the URL (I think we wouldn't want to risk guessing the wrong extension). To achieve this, we should probably update [db.NewFileUpload](https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/internal/db/fileupload.go#L111) and add a `contentType string` argument, which will default to autodetect when the string is `""`.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: electricdusk/rushlink#38
No description provided.