implement browser created pastes

Signed-off-by: Xe Iaso <xe@tailscale.com>
This commit is contained in:
Xe Iaso 2022-12-14 18:31:08 +00:00
parent 49239f2830
commit 24b7c5452c
5 changed files with 37 additions and 12 deletions

View file

@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
)
var (
@ -58,9 +59,17 @@ func main() {
q := url.Values{}
q.Set("filename", filepath.Base(*fname))
q.Set("data", string(data))
q.Set("content", string(data))
resp, err := http.PostForm(u.String(), q)
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(q.Encode))
if err != nil {
log.Fatalf("can't make HTTP request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "text/plain")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("can't post to %s: %v", u, err)
}

View file

@ -124,14 +124,15 @@ func (s *Server) TailnetSubmitPaste(w http.ResponseWriter, r *http.Request) {
return
}
if !r.Form.Has("filename") && !r.Form.Has("data") {
if !r.Form.Has("filename") && !r.Form.Has("content") {
log.Printf("%s", r.Form.Encode())
log.Printf("%s: posted form without filename and data", r.RemoteAddr)
http.Error(w, "include form values filename and data", http.StatusBadRequest)
return
}
fname := r.Form.Get("filename")
data := r.Form.Get("data")
data := r.Form.Get("content")
id := uuid.NewString()
q := `
@ -164,8 +165,14 @@ VALUES
log.Printf("new paste: %s", id)
switch r.Header.Get("Accept") {
case "text/plain":
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "http://%s/paste/%s", r.Host, id)
default:
http.Redirect(w, r, fmt.Sprintf("http://%s/%s", r.Host, id), http.StatusTemporaryRedirect)
}
}
func (s *Server) ShowPost(w http.ResponseWriter, r *http.Request) {

View file

@ -1,7 +1,7 @@
:root {
--background-color: 236;
--text-color: 55;
--accent-color: 200;
--background-color: 0;
--text-color: 43;
--accent-color: 344;
--width: 80ch;
--padding: 0;
@ -16,7 +16,7 @@
--pre-background-light: hsla(var(--background-color), 10%, 80%, 100%);
--a-background: hsla(var(--background-color), 90%, 5%, 100%);
--a-background-light: hsla(var(--background-color), 30%, 90%, 100%);
--a-color: hsla(var(--accent-color), 70%, 85%, 100%);
--a-color: hsla(var(--accent-color), 80%, 90%, 100%);
--a-color-light: hsla(var(--accent-color), 80%, 10%, 100%);
--blockquote-border: 0.5ch solid hsla(var(--accent-color), 80%, 80%, 100%);
--blockquote-border-light: 0.5ch solid hsla(var(--accent-color), 50%, 30%, 100%);
@ -59,7 +59,7 @@ pre {
}
a, a:active, a:visited {
color: var(--selection);
color: var(--a-color);
background-color: var(--a-background);
}
@ -108,3 +108,11 @@ footer {
padding: 0.5em 10px;
}
}
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}

View file

@ -3,7 +3,7 @@
<textarea name="content" rows=20 cols=80></textarea>
<br />
<label for="filename">Filename:</label>
<input type="text" id="filename" filename="filename" placeholder="filename.txt" />
<input type="text" id="filename" name="filename" value="filename.txt" />
<input type="submit" value="Submit" />
</form>
{{template "footer" .}}

View file

@ -1,6 +1,7 @@
{{template "header" .}}
<div class="right">{{.PasterDisplayName}} <img style="width:32px;height:32px" src="{{.PasterProfilePicURL}}" /></div>
<pre><code>
{{.Data}}
</code></pre>