Make parsed URLs look a little nicer.

This commit is contained in:
Azareal 2019-10-13 14:45:15 +10:00
parent df6e268a06
commit 38bbdfe0c2
1 changed files with 28 additions and 24 deletions

View File

@ -3,13 +3,13 @@ package common
import ( import (
"bytes" "bytes"
//"fmt" //"fmt"
"encoding/json"
"io/ioutil"
"net/url" "net/url"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"io/ioutil"
"os"
"encoding/json"
"unicode/utf8" "unicode/utf8"
) )
@ -283,7 +283,7 @@ func PreparseMessage(msg string) string {
// TODO: Scan through tags and make sure the suffix is present to reduce the number of false positives which hit the loop below // TODO: Scan through tags and make sure the suffix is present to reduce the number of false positives which hit the loop below
//fmt.Printf("tags: %+v\n", tags) //fmt.Printf("tags: %+v\n", tags)
var newI = -1 newI := -1
var out string var out string
toActionList := tagToAction[char] toActionList := tagToAction[char]
for _, toAction := range toActionList { for _, toAction := range toActionList {
@ -473,8 +473,8 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
// Search for URLs, mentions and hashlinks in the messages... // Search for URLs, mentions and hashlinks in the messages...
var sb strings.Builder var sb strings.Builder
var lastItem = 0 lastItem := 0
var i = 0 i := 0
//var c bool //var c bool
//fmt.Println("msg:", "'"+msg+"'") //fmt.Println("msg:", "'"+msg+"'")
for ; len(msg) > i; i++ { for ; len(msg) > i; i++ {
@ -652,7 +652,7 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
} }
sb.WriteString(msg[i : i+urlLen]) sb.WriteString(msg[i : i+urlLen])
sb.Write(URLOpen2) sb.Write(URLOpen2)
sb.WriteString(msg[i : i+urlLen]) sb.WriteString(media.FURL)
sb.Write(URLClose) sb.Write(URLClose)
i += urlLen i += urlLen
lastItem = i lastItem = i
@ -712,7 +712,6 @@ func validateURLString(data string) bool {
func validatedURLBytes(data []byte) (url []byte) { func validatedURLBytes(data []byte) (url []byte) {
datalen := len(data) datalen := len(data)
i := 0 i := 0
if datalen >= 6 { if datalen >= 6 {
if bytes.Equal(data[0:6], []byte("ftp://")) || bytes.Equal(data[0:6], []byte("git://")) { if bytes.Equal(data[0:6], []byte("ftp://")) || bytes.Equal(data[0:6], []byte("git://")) {
i = 6 i = 6
@ -842,6 +841,7 @@ func PartialURLStringLen2(data string) int {
type MediaEmbed struct { type MediaEmbed struct {
Type string //image Type string //image
URL string URL string
FURL string
Body string Body string
Trusted bool // samesite urls Trusted bool // samesite urls
@ -882,7 +882,7 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
if samesite && pathFrags[1] == "attachs" && (scheme == "http" || scheme == "https") { if samesite && pathFrags[1] == "attachs" && (scheme == "http" || scheme == "https") {
var sport string var sport string
// ? - Assumes the sysadmin hasn't mixed up the two standard ports // ? - Assumes the sysadmin hasn't mixed up the two standard ports
if port != "443" && port != "80" { if port != "443" && port != "80" && port != "" {
sport = ":" + port sport = ":" + port
} }
media.URL = scheme + "://" + hostname + sport + path media.URL = scheme + "://" + hostname + sport + path
@ -913,16 +913,14 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
} }
} }
lastFrag := pathFrags[len(pathFrags)-1] if lastFrag := pathFrags[len(pathFrags)-1]; lastFrag != "" {
if lastFrag != "" {
// TODO: Write a function for getting the file extension of a string // TODO: Write a function for getting the file extension of a string
extarr := strings.Split(lastFrag, ".") if extarr := strings.Split(lastFrag, "."); len(extarr) >= 2 {
if len(extarr) >= 2 {
ext := extarr[len(extarr)-1] ext := extarr[len(extarr)-1]
if ImageFileExts.Contains(ext) { if ImageFileExts.Contains(ext) {
media.Type = "image" media.Type = "image"
var sport string var sport string
if port != "443" && port != "80" { if port != "443" && port != "80" && port != "" {
sport = ":" + port sport = ":" + port
} }
media.URL = scheme + "://" + hostname + sport + path media.URL = scheme + "://" + hostname + sport + path
@ -931,6 +929,12 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
} }
} }
var sport string
if port != "443" && port != "80" && port != "" {
sport = ":" + port
}
media.FURL = hostname + sport + path
return media, true return media, true
} }