feat: Initial commit

This commit is contained in:
ptrcnull 2022-02-28 15:56:36 +01:00
commit 9f6190a762
Signed by: ptrcnull
GPG key ID: 411F7B30801DD9CA
2 changed files with 81 additions and 0 deletions

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module gaypaste
go 1.17

78
main.go Normal file
View file

@ -0,0 +1,78 @@
package main
import (
"errors"
"flag"
"io"
"log"
"math/rand"
"net/http"
"os"
"path"
"time"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func init() {
rand.Seed(time.Now().UnixNano())
}
// RandString taken from https://stackoverflow.com/a/31832326
func RandString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
var dataDirFlag = flag.String("dataDir", "/tmp", "directory for files")
var domainFlag = flag.String("domain", "http://localhost:8085", "base domain")
func main() {
h := &Handler{
DataDir: *dataDirFlag,
Domain: *domainFlag,
}
log.Println("listening")
panic(http.ListenAndServe(":8085", h))
}
type Handler struct {
DataDir string
Domain string
}
func (h *Handler) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
return
}
name := RandString(8)
for {
_, err := os.Stat(path.Join(h.DataDir, name))
if errors.Is(err, os.ErrNotExist) {
break
}
name = RandString(8)
}
file, err := os.OpenFile(path.Join(h.DataDir, name), os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
log.Println("error:", err)
return
}
defer file.Close()
defer req.Body.Close()
_, err = io.Copy(file, req.Body)
if err != nil {
log.Println("error:", err)
return
}
log.Println("uploaded as", name)
wr.Write([]byte(h.Domain + "/" + name))
}