Remove filesystem saving and add sqlite support
This commit is contained in:
parent
584e56aae9
commit
ef1415f55f
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
CREATE TABLE `pastebin` (
|
||||||
|
`id` varchar(30) NOT NULL,
|
||||||
|
`data` longtext,
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
}
|
75
main.go
75
main.go
|
@ -1,22 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/dchest/uniuri"
|
"github.com/dchest/uniuri"
|
||||||
"github.com/ewhal/pygments"
|
"github.com/ewhal/pygments"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DIRECTORY = "/tmp/"
|
ADDRESS = "https://p.pantsu.cat/"
|
||||||
ADDRESS = "https://p.pantsu.cat/"
|
LENGTH = 6
|
||||||
LENGTH = 4
|
TEXT = "$ <command> | curl -F 'p=<-' " + ADDRESS + "\n"
|
||||||
TEXT = "$ <command> | curl -F 'p=<-' " + ADDRESS + "\n"
|
PORT = ":9900"
|
||||||
PORT = ":9900"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func check(err error) {
|
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 {
|
func generateName() string {
|
||||||
s := uniuri.NewLen(LENGTH)
|
s := uniuri.NewLen(LENGTH)
|
||||||
file := exists(DIRECTORY + s)
|
db, err := sql.Open("sqlite3", "./database.db")
|
||||||
if file == true {
|
check(err)
|
||||||
generateName()
|
|
||||||
|
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
|
return s
|
||||||
|
|
||||||
}
|
}
|
||||||
func save(raw []byte) string {
|
func save(raw []byte) string {
|
||||||
paste := raw[85 : len(raw)-46]
|
paste := raw[86 : len(raw)-46]
|
||||||
|
|
||||||
s := generateName()
|
s := generateName()
|
||||||
location := DIRECTORY + s
|
db, err := sql.Open("sqlite3", "./database.db")
|
||||||
|
|
||||||
err := ioutil.WriteFile(location, paste, 0644)
|
|
||||||
check(err)
|
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
|
return s
|
||||||
}
|
}
|
||||||
|
@ -60,21 +64,21 @@ func save(raw []byte) string {
|
||||||
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
param1 := r.URL.Query().Get("p")
|
param1 := html.EscapeString(r.URL.Query().Get("p"))
|
||||||
param2 := r.URL.Query().Get("lang")
|
param2 := html.EscapeString(r.URL.Query().Get("lang"))
|
||||||
if param1 != "" {
|
db, err := sql.Open("sqlite3", "./database.db")
|
||||||
d := DIRECTORY + param1
|
var s string
|
||||||
s, err := ioutil.ReadFile(d)
|
err = db.QueryRow("select data from pastebin where id=?", param1).Scan(&s)
|
||||||
if err != nil {
|
check(err)
|
||||||
http.Error(w, err.Error(), 500)
|
db.Close()
|
||||||
}
|
|
||||||
|
|
||||||
|
if param1 != "" {
|
||||||
if param2 != "" {
|
if param2 != "" {
|
||||||
highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
highlight := pygments.Highlight(html.UnescapeString(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
||||||
io.WriteString(w, string(highlight))
|
io.WriteString(w, highlight)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
io.WriteString(w, string(s))
|
io.WriteString(w, html.UnescapeString(s))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
io.WriteString(w, TEXT)
|
io.WriteString(w, TEXT)
|
||||||
|
@ -84,7 +88,8 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
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":
|
case "DELETE":
|
||||||
// Remove the record.
|
// Remove the record.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue