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 (
"bytes"
//"fmt"
"encoding/json"
"io/ioutil"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"io/ioutil"
"os"
"encoding/json"
"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
//fmt.Printf("tags: %+v\n", tags)
var newI = -1
newI := -1
var out string
toActionList := tagToAction[char]
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...
var sb strings.Builder
var lastItem = 0
var i = 0
lastItem := 0
i := 0
//var c bool
//fmt.Println("msg:", "'"+msg+"'")
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.Write(URLOpen2)
sb.WriteString(msg[i : i+urlLen])
sb.WriteString(media.FURL)
sb.Write(URLClose)
i += urlLen
lastItem = i
@ -712,7 +712,6 @@ func validateURLString(data string) bool {
func validatedURLBytes(data []byte) (url []byte) {
datalen := len(data)
i := 0
if datalen >= 6 {
if bytes.Equal(data[0:6], []byte("ftp://")) || bytes.Equal(data[0:6], []byte("git://")) {
i = 6
@ -842,6 +841,7 @@ func PartialURLStringLen2(data string) int {
type MediaEmbed struct {
Type string //image
URL string
FURL string
Body string
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") {
var sport string
// ? - Assumes the sysadmin hasn't mixed up the two standard ports
if port != "443" && port != "80" {
if port != "443" && port != "80" && port != "" {
sport = ":" + port
}
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 != "" {
if lastFrag := pathFrags[len(pathFrags)-1]; lastFrag != "" {
// TODO: Write a function for getting the file extension of a string
extarr := strings.Split(lastFrag, ".")
if len(extarr) >= 2 {
if extarr := strings.Split(lastFrag, "."); len(extarr) >= 2 {
ext := extarr[len(extarr)-1]
if ImageFileExts.Contains(ext) {
media.Type = "image"
var sport string
if port != "443" && port != "80" {
if port != "443" && port != "80" && port != "" {
sport = ":" + port
}
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
}