aboutsummaryrefslogtreecommitdiff
path: root/post
diff options
context:
space:
mode:
authorFChannel <>2022-04-30 11:00:55 -0700
committerFChannel <>2022-06-19 12:53:29 -0700
commit1892327cee2c3fa1d3bea729bd08eb63c2189a96 (patch)
tree7b846f7d9caf46fba6c9d15ff81b9d89dcca9476 /post
parent5b52d269faa2ce2014d0feba603a2122361cf4eb (diff)
restructured code base to prevent circular dependicies
Diffstat (limited to 'post')
-rw-r--r--post/util.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/post/util.go b/post/util.go
new file mode 100644
index 0000000..c4920f7
--- /dev/null
+++ b/post/util.go
@@ -0,0 +1,93 @@
+package post
+
+import (
+ "mime/multipart"
+ "net/http"
+ "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 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 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
+}