From 503a6637b8294aeb8e5e5546f8acbd2b3d6c4744 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 30 Apr 2022 22:17:32 -0700 Subject: first steps in posting connected. can make reply with no quote or quote OP do not recommend working on this branch for the time being since things are being moved around a lot --- util/blacklist.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ util/util.go | 26 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 util/blacklist.go (limited to 'util') diff --git a/util/blacklist.go b/util/blacklist.go new file mode 100644 index 0000000..5368037 --- /dev/null +++ b/util/blacklist.go @@ -0,0 +1,76 @@ +package util + +import ( + "regexp" + + "github.com/FChannel0/FChannel-Server/config" +) + +type PostBlacklist struct { + Id int + Regex string +} + +func DeleteRegexBlacklistDB(id int) error { + query := `delete from postblacklist where id=$1` + + _, err := config.DB.Exec(query, id) + return err +} + +func GetRegexBlacklistDB() ([]PostBlacklist, error) { + var list []PostBlacklist + + query := `select id, regex from postblacklist` + + rows, err := config.DB.Query(query) + if err != nil { + return list, err + } + + defer rows.Close() + for rows.Next() { + var temp PostBlacklist + rows.Scan(&temp.Id, &temp.Regex) + + list = append(list, temp) + } + + return list, nil +} + +func IsPostBlacklist(comment string) (bool, error) { + postblacklist, err := GetRegexBlacklistDB() + + if err != nil { + return false, err + } + + for _, e := range postblacklist { + re := regexp.MustCompile(e.Regex) + + if re.MatchString(comment) { + return true, nil + } + } + + return false, nil +} + +func WriteRegexBlacklistDB(regex string) error { + var re string + + query := `select from postblacklist where regex=$1` + if err := config.DB.QueryRow(query, regex).Scan(&re); err != nil { + return err + } + + if re != "" { + return nil + } + + query = `insert into postblacklist (regex) values ($1)` + + _, err := config.DB.Exec(query, regex) + return err +} diff --git a/util/util.go b/util/util.go index 381db94..ade5eae 100644 --- a/util/util.go +++ b/util/util.go @@ -4,6 +4,8 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "mime/multipart" + "net/http" "os" "regexp" "strings" @@ -225,3 +227,27 @@ func CreateUniqueID(actor string) (string, error) { return newID, nil } + +func GetFileContentType(out multipart.File) (string, error) { + buffer := make([]byte, 512) + + _, err := out.Read(buffer) + if err != nil { + return "", err + } + + out.Seek(0, 0) + + contentType := http.DetectContentType(buffer) + + return contentType, nil +} + +func GetContentType(location string) string { + elements := strings.Split(location, ";") + if len(elements) > 0 { + return elements[0] + } else { + return location + } +} -- cgit v1.2.3