dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit db58a6654333efe2fe6158bced19368d26e6a947
parent 1d833ecbe35cf3b05ad153417c7df949ed2fc7ab
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  6 Jun 2023 01:13:48 -0700

cleanup consts usage

Diffstat:
Mpkg/web/handlers/api/v1/topBarHandler.go | 80+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 46 insertions(+), 34 deletions(-)

diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -1085,16 +1085,18 @@ func convertLinks(in string, } func extractPGPMessage(html string) (out string) { - startIdx := strings.Index(html, pgpPrefix) - endIdx := strings.Index(html, pgpSuffix) + pgpPrefixL := pgpPrefix + pgpSuffixL := pgpSuffix + startIdx := strings.Index(html, pgpPrefixL) + endIdx := strings.Index(html, pgpSuffixL) if startIdx != -1 && endIdx != -1 { - out = html[startIdx : endIdx+len(pgpSuffix)] + out = html[startIdx : endIdx+len(pgpSuffixL)] out = strings.TrimSpace(out) - out = strings.TrimPrefix(out, pgpPrefix) - out = strings.TrimSuffix(out, pgpSuffix) + out = strings.TrimPrefix(out, pgpPrefixL) + out = strings.TrimSuffix(out, pgpSuffixL) out = strings.Join(strings.Split(out, " "), "\n") - out = pgpPrefix + out - out += pgpSuffix + out = pgpPrefixL + out + out += pgpSuffixL } return out } @@ -1108,15 +1110,17 @@ func convertNewLines(html string, canUseMultiline bool) string { // Auto convert pasted pgp public key into uploaded file func convertPGPPublicKeyToFile(db *database.DkfDB, html string, authUserID database.UserID) string { - startIdx := strings.Index(html, pgpPKeyPrefix) - endIdx := strings.Index(html, pgpPKeySuffix) + pgpPKeyPrefixL := pgpPKeyPrefix + pgpPKeySuffixL := pgpPKeySuffix + startIdx := strings.Index(html, pgpPKeyPrefixL) + endIdx := strings.Index(html, pgpPKeySuffixL) if startIdx != -1 && endIdx != -1 { - pkeySubSlice := html[startIdx : endIdx+len(pgpPKeySuffix)] + pkeySubSlice := html[startIdx : endIdx+len(pgpPKeySuffixL)] unescapedPkey := html2.UnescapeString(pkeySubSlice) tmp := convertInlinePGPPublicKey(unescapedPkey) upload, _ := db.CreateUpload("pgp_pkey.txt", []byte(tmp), authUserID) msgBefore := html[0:startIdx] - msgAfter := html[endIdx+len(pgpPKeySuffix):] + msgAfter := html[endIdx+len(pgpPKeySuffixL):] html = msgBefore + ` [` + upload.GetHTMLLink() + `] ` + msgAfter html = strings.TrimSpace(html) } @@ -1125,12 +1129,14 @@ func convertPGPPublicKeyToFile(db *database.DkfDB, html string, authUserID datab func convertPGPClearsignToFile(db *database.DkfDB, html string, authUserID database.UserID) string { if b, _ := clearsign.Decode([]byte(html)); b != nil { - startIdx := strings.Index(html, pgpSignedPrefix) - endIdx := strings.Index(html, pgpSignedSuffix) - tmp := html[startIdx : endIdx+len(pgpSignedSuffix)] + pgpSignedPrefixL := pgpSignedPrefix + pgpSignedSuffixL := pgpSignedSuffix + startIdx := strings.Index(html, pgpSignedPrefixL) + endIdx := strings.Index(html, pgpSignedSuffixL) + tmp := html[startIdx : endIdx+len(pgpSignedSuffixL)] upload, _ := db.CreateUpload("pgp_clearsign.txt", []byte(tmp), authUserID) msgBefore := html[0:startIdx] - msgAfter := html[endIdx+len(pgpSignedSuffix):] + msgAfter := html[endIdx+len(pgpSignedSuffixL):] html = msgBefore + ` [` + upload.GetHTMLLink() + `] ` + msgAfter html = strings.TrimSpace(html) } @@ -1139,19 +1145,21 @@ func convertPGPClearsignToFile(db *database.DkfDB, html string, authUserID datab // Auto convert pasted pgp message into uploaded file func convertPGPMessageToFile(db *database.DkfDB, html string, authUserID database.UserID) string { - startIdx := strings.Index(html, pgpPrefix) - endIdx := strings.Index(html, pgpSuffix) + pgpPrefixL := pgpPrefix + pgpSuffixL := pgpSuffix + startIdx := strings.Index(html, pgpPrefixL) + endIdx := strings.Index(html, pgpSuffixL) if startIdx != -1 && endIdx != -1 { - tmp := html[startIdx : endIdx+len(pgpSuffix)] + tmp := html[startIdx : endIdx+len(pgpSuffixL)] tmp = strings.TrimSpace(tmp) - tmp = strings.TrimPrefix(tmp, pgpPrefix) - tmp = strings.TrimSuffix(tmp, pgpSuffix) + tmp = strings.TrimPrefix(tmp, pgpPrefixL) + tmp = strings.TrimSuffix(tmp, pgpSuffixL) tmp = strings.Join(strings.Split(tmp, " "), "\n") - tmp = pgpPrefix + tmp - tmp += pgpSuffix + tmp = pgpPrefixL + tmp + tmp += pgpSuffixL upload, _ := db.CreateUpload("pgp.txt", []byte(tmp), authUserID) msgBefore := html[0:startIdx] - msgAfter := html[endIdx+len(pgpSuffix):] + msgAfter := html[endIdx+len(pgpSuffixL):] html = msgBefore + ` [` + upload.GetHTMLLink() + `] ` + msgAfter html = strings.TrimSpace(html) } @@ -1160,19 +1168,21 @@ func convertPGPMessageToFile(db *database.DkfDB, html string, authUserID databas // Auto convert pasted age message into uploaded file func convertAgeMessageToFile(db *database.DkfDB, html string, authUserID database.UserID) string { - startIdx := strings.Index(html, agePrefix) - endIdx := strings.Index(html, ageSuffix) + agePrefixL := agePrefix + ageSuffixL := ageSuffix + startIdx := strings.Index(html, agePrefixL) + endIdx := strings.Index(html, ageSuffixL) if startIdx != -1 && endIdx != -1 { - tmp := html[startIdx : endIdx+len(ageSuffix)] + tmp := html[startIdx : endIdx+len(ageSuffixL)] tmp = strings.TrimSpace(tmp) - tmp = strings.TrimPrefix(tmp, agePrefix) - tmp = strings.TrimSuffix(tmp, ageSuffix) + tmp = strings.TrimPrefix(tmp, agePrefixL) + tmp = strings.TrimSuffix(tmp, ageSuffixL) tmp = strings.Join(strings.Split(tmp, " "), "\n") - tmp = agePrefix + tmp - tmp += ageSuffix + tmp = agePrefixL + tmp + tmp += ageSuffixL upload, _ := db.CreateUpload("age.txt", []byte(tmp), authUserID) msgBefore := html[0:startIdx] - msgAfter := html[endIdx+len(ageSuffix):] + msgAfter := html[endIdx+len(ageSuffixL):] html = msgBefore + ` [` + upload.GetHTMLLink() + `] ` + msgAfter html = strings.TrimSpace(html) } @@ -1180,13 +1190,15 @@ func convertAgeMessageToFile(db *database.DkfDB, html string, authUserID databas } func convertInlinePGPPublicKey(inlinePKey string) string { + pgpPKeyPrefixL := pgpPKeyPrefix + pgpPKeySuffixL := pgpPKeySuffix // If it contains new lines, it was probably pasted using multi-line text box if strings.Contains(inlinePKey, "\n") { return inlinePKey } inlinePKey = strings.TrimSpace(inlinePKey) - inlinePKey = strings.TrimPrefix(inlinePKey, pgpPKeyPrefix) - inlinePKey = strings.TrimSuffix(inlinePKey, pgpPKeySuffix) + inlinePKey = strings.TrimPrefix(inlinePKey, pgpPKeyPrefixL) + inlinePKey = strings.TrimSuffix(inlinePKey, pgpPKeySuffixL) inlinePKey = strings.TrimSpace(inlinePKey) commentsParts := strings.Split(inlinePKey, "Comment: ") commentsParts, lastCommentPart := commentsParts[:len(commentsParts)-1], commentsParts[len(commentsParts)-1] @@ -1213,6 +1225,6 @@ func convertInlinePGPPublicKey(inlinePKey string) string { } else { key = "\n" + strings.Join(strings.Split(lastCommentPart, " "), "\n") } - inlinePKey = pgpPKeyPrefix + "\n" + commentsStr + key + "\n" + pgpPKeySuffix + inlinePKey = pgpPKeyPrefixL + "\n" + commentsStr + key + "\n" + pgpPKeySuffixL return inlinePKey }