Reduce the amount of boilerplate for images in the parser.

This commit is contained in:
Azareal 2018-09-19 16:59:07 +10:00
parent 335fe4fdbe
commit 6941786490
1 changed files with 13 additions and 14 deletions

View File

@ -428,6 +428,7 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
msgbytes = append(msgbytes, SpaceGap...) msgbytes = append(msgbytes, SpaceGap...)
var lastItem = 0 var lastItem = 0
var i = 0 var i = 0
// TODO: Use a bytes buffer or a string builder here
for ; len(msgbytes) > (i + 1); i++ { for ; len(msgbytes) > (i + 1); i++ {
if (i == 0 && (msgbytes[0] > 32)) || ((msgbytes[i] < 33) && (msgbytes[i+1] > 32)) { if (i == 0 && (msgbytes[0] > 32)) || ((msgbytes[i] < 33) && (msgbytes[i+1] > 32)) {
if (i != 0) || msgbytes[i] < 33 { if (i != 0) || msgbytes[i] < 33 {
@ -563,24 +564,22 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
continue continue
} }
var addImage = func(url string) {
outbytes = append(outbytes, imageOpen...)
outbytes = append(outbytes, []byte(url)...)
outbytes = append(outbytes, imageOpen2...)
outbytes = append(outbytes, []byte(url)...)
outbytes = append(outbytes, imageClose...)
i += urlLen
lastItem = i
}
// TODO: Reduce the amount of code duplication // TODO: Reduce the amount of code duplication
if media.Type == "attach" { if media.Type == "attach" {
outbytes = append(outbytes, imageOpen...) addImage(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"&sectionType="+sectionType)
outbytes = append(outbytes, []byte(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"&sectionType="+sectionType)...)
outbytes = append(outbytes, imageOpen2...)
outbytes = append(outbytes, []byte(media.URL+"?sectionID="+strconv.Itoa(sectionID)+"&sectionType="+sectionType)...)
outbytes = append(outbytes, imageClose...)
i += urlLen
lastItem = i
continue continue
} else if media.Type == "image" { } else if media.Type == "image" {
outbytes = append(outbytes, imageOpen...) addImage(media.URL)
outbytes = append(outbytes, []byte(media.URL)...)
outbytes = append(outbytes, imageOpen2...)
outbytes = append(outbytes, []byte(media.URL)...)
outbytes = append(outbytes, imageClose...)
i += urlLen
lastItem = i
continue continue
} else if media.Type == "raw" { } else if media.Type == "raw" {
outbytes = append(outbytes, []byte(media.Body)...) outbytes = append(outbytes, []byte(media.Body)...)