From ef1415f55f8339bdb4c4edaa71cb90b72ebb02bc Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Sun, 19 Jun 2016 11:48:57 +1000 Subject: [PATCH] Remove filesystem saving and add sqlite support --- database.db | Bin 0 -> 12288 bytes database.sql | 5 ++++ main.go | 75 +++++++++++++++++++++++++++------------------------ 3 files changed, 45 insertions(+), 35 deletions(-) create mode 100644 database.db create mode 100644 database.sql diff --git a/database.db b/database.db new file mode 100644 index 0000000000000000000000000000000000000000..7222ac47479317b5988f65d2637370c12e0ada61 GIT binary patch literal 12288 zcmeI1%WvXF7{JXV51dl>60KHMq|sJtUxn_(W`G-?$U z$)IQ2Iz%AFe8Bj8%o6|@hFQW-06){OL z(`<5}*Vq0ZM=ppadv^e=~v4d<*whR(zjV5xg467Ict&r5e+(KVL8K zVgZPJu2KM(Q}rc?umD`D(j~ZocDE1hNGcHpheskfY*s33BvFD0UV?#XC`i6VM0H#* zRrz`c927brLJCLO=!Sp(UVg>LsD>opI@beyroVtgWV-Q-X?+*TsYK58i-czwKdfJ1 z-rsrekEL(f+Tx!J-_5t@f`PBGk_JlP4G28#u)+J!Vw>WfS*eFvN$$bn0Igq0rVdp@ zlkY;M!;sh(LlN-tN2#ss&8~1zswT5Q+qE5-^^|M~c!ITCEfsrJK2gj!u8WP*RZ6H8 za(u1H^IOG3UdZRv+CfgK32k0Uk9wy^y=qg5^)qm*rS%75UKwqV9hm9pd1PTokK4_G*)4Ht1 zde;>g%WbsPqTY9eTWQow^t49WZb`A^MpE63l@nTQByN<8=Cx8Nc1L@e@^wQ|h~??S|~VfHBuVYb;ayi*?S z%dUkmR#V2aWL#Usj6bX&+NV}J%=T%_L#9?!OZhgTo_4rlV39Pv!70a6Fn zI>?TYa?v%=HFX^t5>|RGkGk!i7+AMpm()qx`ggq(AqLJSxy~moTql3_foWQ}1Rl4? z3&G`QD+TFj5639dnIN4fNXI9g5=DxKZT3Tcoawd$}*k=~o}F{zq5%FY>l-tV!P$;W?%Wb+U#y_ zXVRH;Hvdt@bhE&Ky1e_qdO&u$Z++YFf9RK2 z_JgbZXJ^FYVV1?`JC=LSaL>7y+%MdZ-1poM+;7~k-0$4aZ*cUZSttQYfD)htC;>`< z5}*Vq0ZM=ppaduZN?;ZQ*uZ_CceBb8(Ypu?5z)J^T3ie)`^YuR0ujjNz&sI%OM8w8 Y#1R`L0&yP(h(MfP{)NDDh`4V40`SP4zW@LL literal 0 HcmV?d00001 diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..6e280fe --- /dev/null +++ b/database.sql @@ -0,0 +1,5 @@ +CREATE TABLE `pastebin` ( + `id` varchar(30) NOT NULL, + `data` longtext, + PRIMARY KEY (`id`) +} diff --git a/main.go b/main.go index f05e023..85e6430 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,23 @@ package main import ( + "database/sql" "fmt" + "html" "io" "io/ioutil" "net/http" - "os" "github.com/dchest/uniuri" "github.com/ewhal/pygments" + _ "github.com/mattn/go-sqlite3" ) const ( - DIRECTORY = "/tmp/" - ADDRESS = "https://p.pantsu.cat/" - LENGTH = 4 - TEXT = "$ | curl -F 'p=<-' " + ADDRESS + "\n" - PORT = ":9900" + ADDRESS = "https://p.pantsu.cat/" + LENGTH = 6 + TEXT = "$ | curl -F 'p=<-' " + ADDRESS + "\n" + PORT = ":9900" ) func check(err error) { @@ -25,34 +26,37 @@ func check(err error) { } } -func exists(location string) bool { - if _, err := os.Stat(location); err != nil { - if os.IsNotExist(err) { - return false - } - } - return true - -} - func generateName() string { s := uniuri.NewLen(LENGTH) - file := exists(DIRECTORY + s) - if file == true { - generateName() + db, err := sql.Open("sqlite3", "./database.db") + check(err) + + query, err := db.Query("select id from pastebin") + for query.Next() { + var id string + err := query.Scan(&id) + if err != nil { + + } + if id == s { + generateName() + } } + db.Close() return s } func save(raw []byte) string { - paste := raw[85 : len(raw)-46] + paste := raw[86 : len(raw)-46] s := generateName() - location := DIRECTORY + s - - err := ioutil.WriteFile(location, paste, 0644) + db, err := sql.Open("sqlite3", "./database.db") check(err) + stmt, err := db.Prepare("INSERT INTO pastebin(id, data) values(?,?)") + _, err = stmt.Exec(s, html.EscapeString(string(paste))) + check(err) + db.Close() return s } @@ -60,21 +64,21 @@ func save(raw []byte) string { func pasteHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": - param1 := r.URL.Query().Get("p") - param2 := r.URL.Query().Get("lang") - if param1 != "" { - d := DIRECTORY + param1 - s, err := ioutil.ReadFile(d) - if err != nil { - http.Error(w, err.Error(), 500) - } + param1 := html.EscapeString(r.URL.Query().Get("p")) + param2 := html.EscapeString(r.URL.Query().Get("lang")) + db, err := sql.Open("sqlite3", "./database.db") + var s string + err = db.QueryRow("select data from pastebin where id=?", param1).Scan(&s) + check(err) + db.Close() + if param1 != "" { if param2 != "" { - highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8") - io.WriteString(w, string(highlight)) + highlight := pygments.Highlight(html.UnescapeString(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8") + io.WriteString(w, highlight) } else { - io.WriteString(w, string(s)) + io.WriteString(w, html.UnescapeString(s)) } } else { io.WriteString(w, TEXT) @@ -84,7 +88,8 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, err.Error(), 500) } - io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n") + name := save(buf) + io.WriteString(w, ADDRESS+"?p="+name+"\n") case "DELETE": // Remove the record. }