aboutsummaryrefslogtreecommitdiff
path: root/post/util.go
blob: c4920f7a9e6cb750c439c351f33df68fe6fadf26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
}