logging.go 2.08 KB
Newer Older
1
package internal
Adin Schmahmann's avatar
Adin Schmahmann committed
2 3 4 5 6

import (
	"fmt"
	"strings"

Adin Schmahmann's avatar
Adin Schmahmann committed
7
	"github.com/multiformats/go-multibase"
8
	"github.com/multiformats/go-multihash"
9
	"gitlab.dms3.io/dms3/go-cid"
Adin Schmahmann's avatar
Adin Schmahmann committed
10 11
)

Adin Schmahmann's avatar
Adin Schmahmann committed
12 13 14 15 16 17 18
func multibaseB32Encode(k []byte) string {
	res, err := multibase.Encode(multibase.Base32, k)
	if err != nil {
		// Should be unreachable
		panic(err)
	}
	return res
19 20
}

Adin Schmahmann's avatar
Adin Schmahmann committed
21 22
func tryFormatLoggableRecordKey(k string) (string, error) {
	if len(k) == 0 {
23
		return "", fmt.Errorf("LoggableRecordKey is empty")
Adin Schmahmann's avatar
Adin Schmahmann committed
24 25 26 27 28 29
	}
	var proto, cstr string
	if k[0] == '/' {
		// it's a path (probably)
		protoEnd := strings.IndexByte(k[1:], '/')
		if protoEnd < 0 {
30
			return "", fmt.Errorf("LoggableRecordKey starts with '/' but is not a path: %s", multibaseB32Encode([]byte(k)))
Adin Schmahmann's avatar
Adin Schmahmann committed
31 32 33 34
		}
		proto = k[1 : protoEnd+1]
		cstr = k[protoEnd+2:]

Adin Schmahmann's avatar
Adin Schmahmann committed
35
		encStr := multibaseB32Encode([]byte(cstr))
Adin Schmahmann's avatar
Adin Schmahmann committed
36 37 38
		return fmt.Sprintf("/%s/%s", proto, encStr), nil
	}

39
	return "", fmt.Errorf("LoggableRecordKey is not a path: %s", multibaseB32Encode([]byte(cstr)))
Adin Schmahmann's avatar
Adin Schmahmann committed
40 41
}

42
type LoggableRecordKeyString string
Adin Schmahmann's avatar
Adin Schmahmann committed
43

44
func (lk LoggableRecordKeyString) String() string {
Adin Schmahmann's avatar
Adin Schmahmann committed
45 46 47 48 49 50 51 52
	k := string(lk)
	newKey, err := tryFormatLoggableRecordKey(k)
	if err == nil {
		return newKey
	}
	return err.Error()
}

53
type LoggableRecordKeyBytes []byte
Adin Schmahmann's avatar
Adin Schmahmann committed
54

55
func (lk LoggableRecordKeyBytes) String() string {
Adin Schmahmann's avatar
Adin Schmahmann committed
56 57 58 59 60 61 62 63
	k := string(lk)
	newKey, err := tryFormatLoggableRecordKey(k)
	if err == nil {
		return newKey
	}
	return err.Error()
}

64
type LoggableProviderRecordBytes []byte
Adin Schmahmann's avatar
Adin Schmahmann committed
65

66
func (lk LoggableProviderRecordBytes) String() string {
67
	newKey, err := tryFormatLoggableProviderKey(lk)
Adin Schmahmann's avatar
Adin Schmahmann committed
68 69 70 71 72 73
	if err == nil {
		return newKey
	}
	return err.Error()
}

74
func tryFormatLoggableProviderKey(k []byte) (string, error) {
Adin Schmahmann's avatar
Adin Schmahmann committed
75
	if len(k) == 0 {
76
		return "", fmt.Errorf("LoggableProviderKey is empty")
Adin Schmahmann's avatar
Adin Schmahmann committed
77 78
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
79
	encodedKey := multibaseB32Encode(k)
Adin Schmahmann's avatar
Adin Schmahmann committed
80 81 82

	// The DHT used to provide CIDs, but now provides multihashes
	// TODO: Drop this when enough of the network has upgraded
83 84 85 86 87 88
	if _, err := cid.Cast(k); err == nil {
		return encodedKey, nil
	}

	if _, err := multihash.Cast(k); err == nil {
		return encodedKey, nil
Adin Schmahmann's avatar
Adin Schmahmann committed
89 90
	}

91
	return "", fmt.Errorf("LoggableProviderKey is not a Multihash or CID: %s", encodedKey)
Adin Schmahmann's avatar
Adin Schmahmann committed
92
}