From 4df031396149659d209c71379b214fb0d70476ac Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 16 Apr 2019 21:45:55 +1000 Subject: [PATCH] Very basic non-image attachment embeds. --- common/parser.go | 21 +++++++++++++++++++- public/global.js | 32 +++++++++++++++++++----------- templates/topic_c_attach_item.html | 2 +- themes/nox/public/main.css | 7 +++++-- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/common/parser.go b/common/parser.go index e2ee0776..212f88b0 100644 --- a/common/parser.go +++ b/common/parser.go @@ -26,6 +26,8 @@ var URLClose = []byte("") var imageOpen = []byte("") +var attachOpen = []byte("Attachment") var urlPattern = `(?s)([ {1}])((http|https|ftp|mailto)*)(:{??)\/\/([\.a-zA-Z\/]+)([ {1}])` var urlReg *regexp.Regexp @@ -631,6 +633,13 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/) } else if media.Type == "image" { addImage(media.URL) continue + } else if media.Type == "aother" { + sb.Write(attachOpen) + sb.WriteString(media.URL + "?sectionID=" + strconv.Itoa(sectionID) + "&sectionType=" + sectionType) + sb.Write(attachClose) + i += urlLen + lastItem = i + continue } else if media.Type == "raw" { sb.WriteString(media.Body) i += urlLen @@ -820,13 +829,23 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) { pathFrags := strings.Split(path, "/") if len(pathFrags) >= 2 { if samesite && pathFrags[1] == "attachs" && (scheme == "http" || scheme == "https") { - media.Type = "attach" var sport string // ? - Assumes the sysadmin hasn't mixed up the two standard ports if port != "443" && port != "80" { sport = ":" + port } media.URL = scheme + "://" + hostname + sport + path + var extarr = strings.Split(path, ".") + if len(extarr) == 0 { + // TODO: Write a unit test for this + return media, false + } + var ext = extarr[len(extarr)-1] + if ImageFileExts.Contains(ext) { + media.Type = "attach" + } else { + media.Type = "aother" + } return media, true } } diff --git a/public/global.js b/public/global.js index 20cc0b9e..b7e96763 100644 --- a/public/global.js +++ b/public/global.js @@ -860,11 +860,10 @@ function mainInit(){ } for(let i = 0; i < files.length; i++) { - let reader = new FileReader(); - reader.onload = (e) => { - let filename = files[i]["name"]; + let filename = files[i]["name"]; + let f = (e) => { step1(e,filename) - + let reader = new FileReader(); reader.onload = (e2) => { crypto.subtle.digest('SHA-256',e2.target.result) @@ -874,8 +873,16 @@ function mainInit(){ }).then(hash => step2(e,hash,filename)); } reader.readAsArrayBuffer(files[i]); - } - reader.readAsDataURL(files[i]); + }; + + let ext = getExt(filename); + // TODO: Push ImageFileExts to the client from the server in some sort of gen.js? + let isImage = ["png", "jpg", "jpeg", "svg", "bmp", "gif", "tif", "webp"].includes(ext); + if(isImage) { + let reader = new FileReader(); + reader.onload = f; + reader.readAsDataURL(files[i]); + } else f(null); } } @@ -902,16 +909,17 @@ function mainInit(){ let req = new XMLHttpRequest(); req.addEventListener("load", () => { let data = JSON.parse(req.responseText); - //console.log("hash:",hash); //console.log("rdata:",data); let fileItem = document.createElement("div"); let ext = getExt(filename); - //console.log("ext:",ext); - // TODO: Check if this is actually an image, maybe push ImageFileExts to the client from the server in some sort of gen.js? - fileItem.className = "attach_item attach_item_item attach_image_holder"; + // TODO: Push ImageFileExts to the client from the server in some sort of gen.js? + let isImage = ["png", "jpg", "jpeg", "svg", "bmp", "gif", "tif", "webp"].includes(ext); + let c = ""; + if(isImage) c = " attach_image_holder" + fileItem.className = "attach_item attach_item_item" + c; fileItem.innerHTML = Template_topic_c_attach_item({ ID: data.elems[hash+"."+ext], - ImgSrc: e.target.result, + ImgSrc: isImage ? e.target.result : "", Path: hash+"."+ext, FullPath: "//" + window.location.host + "/attachs/" + hash + "." + ext, }); @@ -932,7 +940,7 @@ function mainInit(){ // Quick Topic / Quick Reply function uploadAttachHandler() { try { - uploadFileHandler(this.files,5,(e,filename) => { + uploadFileHandler(this.files, 5, (e,filename) => { // TODO: Use client templates here let fileDock = document.getElementById("upload_file_dock"); let fileItem = document.createElement("label"); diff --git a/templates/topic_c_attach_item.html b/templates/topic_c_attach_item.html index 4165056e..5fd73ac4 100644 --- a/templates/topic_c_attach_item.html +++ b/templates/topic_c_attach_item.html @@ -1,4 +1,4 @@ - +{{if .ImgSrc}}{{end}} {{.Path}} \ No newline at end of file diff --git a/themes/nox/public/main.css b/themes/nox/public/main.css index b99ecfaa..c0f38f49 100644 --- a/themes/nox/public/main.css +++ b/themes/nox/public/main.css @@ -994,7 +994,7 @@ input[type=checkbox]:checked + label .sel { margin-right: 8px; border-radius: 4px; } -.attach_image_holder span { +.attach_item_item span { margin-bottom: 4px; } .attach_edit_bay button { @@ -1007,10 +1007,13 @@ input[type=checkbox]:checked + label .sel { padding: 8px; width: 100%; } -.attach_image_holder span { +.attach_item_item span { margin-right: auto; overflow: hidden; text-overflow: ellipsis; + width: 350px; +} +.attach_image_holder span { width: 300px; } .attach_item button {