This commit is contained in:
a 2022-08-03 05:21:56 -05:00
parent 5c2c358ff3
commit 0dfd668894
6 changed files with 25 additions and 11 deletions

View File

@ -279,7 +279,6 @@ func (s *Server) APIHandler(w http.ResponseWriter, r *http.Request) {
// pasteHandler generates the html paste pages // pasteHandler generates the html paste pages
func (s *Server) pasteHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) pasteHandler(w http.ResponseWriter, r *http.Request) {
pasteId := chi.URLParam(r, "pasteId") pasteId := chi.URLParam(r, "pasteId")
lang := chi.URLParam(r, "lang") lang := chi.URLParam(r, "lang")
style := chi.URLParam(r, "style") style := chi.URLParam(r, "style")
@ -288,12 +287,14 @@ func (s *Server) pasteHandler(w http.ResponseWriter, r *http.Request) {
p, err := s.store.GetPaste(r.Context(), pasteId) p, err := s.store.GetPaste(r.Context(), pasteId)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
// Run it through the highgligther., // Run it through the highgligther.,
p.Paste, p.Extra, p.Lang, p.Style, err = s.styler.Highlight(p.Paste, lang, style) p.Paste, p.Extra, p.Lang, p.Style, err = s.styler.Highlight(p.Paste, lang, style)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
l := s.styler.Legacy() l := s.styler.Legacy()
@ -331,7 +332,7 @@ func (s *Server) CloneHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
user, _ := s.store.GetUserKey(r.Context(), paste) user, _ := s.getUserKey(r)
page := &Page{ page := &Page{
Body: template.HTML(p.Paste), Body: template.HTML(p.Paste),
PasteTitle: "Copy of " + p.Title, PasteTitle: "Copy of " + p.Title,
@ -428,7 +429,6 @@ func (s *Server) pastesHandler(w http.ResponseWriter, r *http.Request) {
key, err := s.getUserKey(r) key, err := s.getUserKey(r)
b, err := s.store.GetUserPastes(r.Context(), key) b, err := s.store.GetUserPastes(r.Context(), key)
err = templates.ExecuteTemplate(w, "pastes.html", &b) err = templates.ExecuteTemplate(w, "pastes.html", &b)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -440,11 +440,11 @@ func (s *Server) getUserKey(r *http.Request) (string, error) {
cookie, err := r.Cookie("session") cookie, err := r.Cookie("session")
cookieValue := make(map[string]string) cookieValue := make(map[string]string)
if err != nil { if err != nil {
return "", err return "", nil
} }
err = cookieHandler.Decode("session", cookie.Value, &cookieValue) err = cookieHandler.Decode("session", cookie.Value, &cookieValue)
if err != nil { if err != nil {
return "", err return "", nil
} }
email := cookieValue["email"] email := cookieValue["email"]
// Query database if id exists and if it does call generateName again // Query database if id exists and if it does call generateName again

BIN
db.sqlite

Binary file not shown.

View File

@ -148,6 +148,9 @@ func (s *Sqlike) SavePaste(ctx context.Context, title string, data string, expir
id = idgen.MustGenerate() id = idgen.MustGenerate()
expiretime := time.Now().Add(expiry) expiretime := time.Now().Add(expiry)
if expiry == 0 {
expiretime = time.Time{}
}
// Set the generated id as title if not given, // Set the generated id as title if not given,
if title == "" { if title == "" {
title = id title = id
@ -167,7 +170,7 @@ func (s *Sqlike) SavePaste(ctx context.Context, title string, data string, expir
return nil, err return nil, err
} }
_, err = stmt.Exec(id, title, sha, data, delKey, expiretime, userKey) _, err = stmt.Exec(id, title, sha, data, delKey, expiretime.Unix(), userKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -98,9 +98,17 @@ func (s *Styler) Highlight(paste string, lang string, style string) (string, str
lang = sanitize(lang) lang = sanitize(lang)
style = sanitize(style) style = sanitize(style)
lang, supported_lang = s.listOfLangsFirst[lang] lang, supported_lang = s.listOfLangsFirst[lang]
if supported_lang {
lang = sanitize(lang)
} else {
lang = ""
}
style, supported_styles = s.listOfStyles[style] style, supported_styles = s.listOfStyles[style]
lang = sanitize(lang) if supported_styles {
style = sanitize(style) style = sanitize(style)
} else {
style = ""
}
if lang == "" { if lang == "" {
lang = "autodetect" lang = "autodetect"

4
readme.md Normal file
View File

@ -0,0 +1,4 @@
# paste server i guess
whatever

View File

@ -162,7 +162,6 @@
}); });
$( "#button-save" ).click(function() { $( "#button-save" ).click(function() {
// Construct the data, // Construct the data,
var data_lang = $("#button-language").attr("value"); var data_lang = $("#button-language").attr("value");
var data_expiry = $("#button-expiry").attr("value"); var data_expiry = $("#button-expiry").attr("value");
@ -184,7 +183,7 @@
data: JSON.stringify(json_data), data: JSON.stringify(json_data),
dataType: "json", dataType: "json",
success: function(json){ success: function(json){
window.location = json.url+"/"+data_lang window.location = "/p/"+json.id
}, },
error: function(json){ error: function(json){
sweetAlert("", json.responseText, "error"); sweetAlert("", json.responseText, "error");