use bufio scanner

This commit is contained in:
tobi 2025-01-07 18:48:01 +01:00
parent 28193df3d0
commit 6ea156cc7d

View File

@ -18,6 +18,7 @@
package subscriptions
import (
"bufio"
"context"
"encoding/csv"
"encoding/json"
@ -687,21 +688,25 @@ func permsFromPlain(
permType gtsmodel.DomainPermissionType,
body io.ReadCloser,
) ([]gtsmodel.DomainPermission, error) {
// Read body into memory as bytes.
b, err := io.ReadAll(body)
// Scan + split by line.
sc := bufio.NewScanner(body)
// Read into domains
// line by line.
var domains []string
for sc.Scan() {
domains = append(domains, sc.Text())
}
// Whatever happened, we're
// done with the body now.
body.Close()
// Check if error reading body.
if err != nil {
if err := sc.Err(); err != nil {
return nil, gtserror.NewfAt(3, "error decoding into plain: %w", err)
}
// Coerce to newline-separated list of domains.
domains := strings.Split(string(b), "\n")
// Convert raw domains to permissions.
perms := make([]gtsmodel.DomainPermission, 0, len(domains))
for _, domainRaw := range domains {