aboutsummaryrefslogtreecommitdiff
path: root/db/pem.go
blob: d9bcee2484e9dd54f07e1fe436bea756f64cefd3 (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
package db

import (
	"os"
	"strings"

	"github.com/FChannel0/FChannel-Server/activitypub"
)

func GetActorPemFromDB(pemID string) (activitypub.PublicKeyPem, error) {
	var pem activitypub.PublicKeyPem

	query := `select id, owner, file from publickeypem where id=$1`

	rows, err := db.Query(query, pemID)
	if err != nil {
		return pem, err
	}

	defer rows.Close()

	rows.Next()
	rows.Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem)
	f, err := os.ReadFile(pem.PublicKeyPem)
	if err != nil {
		return pem, err
	}

	pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`)

	return pem, nil
}

func GetActorPemFileFromDB(pemID string) (string, error) {
	query := `select file from publickeypem where id=$1`
	rows, err := db.Query(query, pemID)
	if err != nil {
		return "", err
	}

	defer rows.Close()

	var file string
	rows.Next()
	rows.Scan(&file)

	return file, nil
}