Very basic non-image attachment embeds.

This commit is contained in:
Azareal 2019-04-16 21:45:55 +10:00
parent adb7babc02
commit 4df0313961
4 changed files with 46 additions and 16 deletions

View File

@ -26,6 +26,8 @@ var URLClose = []byte("</a>")
var imageOpen = []byte("<a href=\"") var imageOpen = []byte("<a href=\"")
var imageOpen2 = []byte("\"><img src='") var imageOpen2 = []byte("\"><img src='")
var imageClose = []byte("' class='postImage' /></a>") var imageClose = []byte("' class='postImage' /></a>")
var attachOpen = []byte("<a download class='attach' href=\"")
var attachClose = []byte("\">Attachment</a>")
var urlPattern = `(?s)([ {1}])((http|https|ftp|mailto)*)(:{??)\/\/([\.a-zA-Z\/]+)([ {1}])` var urlPattern = `(?s)([ {1}])((http|https|ftp|mailto)*)(:{??)\/\/([\.a-zA-Z\/]+)([ {1}])`
var urlReg *regexp.Regexp var urlReg *regexp.Regexp
@ -631,6 +633,13 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
} else if media.Type == "image" { } else if media.Type == "image" {
addImage(media.URL) addImage(media.URL)
continue continue
} else if media.Type == "aother" {
sb.Write(attachOpen)
sb.WriteString(media.URL + "?sectionID=" + strconv.Itoa(sectionID) + "&amp;sectionType=" + sectionType)
sb.Write(attachClose)
i += urlLen
lastItem = i
continue
} else if media.Type == "raw" { } else if media.Type == "raw" {
sb.WriteString(media.Body) sb.WriteString(media.Body)
i += urlLen i += urlLen
@ -820,13 +829,23 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
pathFrags := strings.Split(path, "/") pathFrags := strings.Split(path, "/")
if len(pathFrags) >= 2 { if len(pathFrags) >= 2 {
if samesite && pathFrags[1] == "attachs" && (scheme == "http" || scheme == "https") { if samesite && pathFrags[1] == "attachs" && (scheme == "http" || scheme == "https") {
media.Type = "attach"
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" {
sport = ":" + port sport = ":" + port
} }
media.URL = scheme + "://" + hostname + sport + path 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 return media, true
} }
} }

View File

@ -860,9 +860,8 @@ function mainInit(){
} }
for(let i = 0; i < files.length; i++) { for(let i = 0; i < files.length; i++) {
let reader = new FileReader(); let filename = files[i]["name"];
reader.onload = (e) => { let f = (e) => {
let filename = files[i]["name"];
step1(e,filename) step1(e,filename)
let reader = new FileReader(); let reader = new FileReader();
@ -874,8 +873,16 @@ function mainInit(){
}).then(hash => step2(e,hash,filename)); }).then(hash => step2(e,hash,filename));
} }
reader.readAsArrayBuffer(files[i]); 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(); let req = new XMLHttpRequest();
req.addEventListener("load", () => { req.addEventListener("load", () => {
let data = JSON.parse(req.responseText); let data = JSON.parse(req.responseText);
//console.log("hash:",hash);
//console.log("rdata:",data); //console.log("rdata:",data);
let fileItem = document.createElement("div"); let fileItem = document.createElement("div");
let ext = getExt(filename); let ext = getExt(filename);
//console.log("ext:",ext); // TODO: Push ImageFileExts to the client from the server in some sort of gen.js?
// TODO: Check if this is actually an image, maybe 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);
fileItem.className = "attach_item attach_item_item attach_image_holder"; let c = "";
if(isImage) c = " attach_image_holder"
fileItem.className = "attach_item attach_item_item" + c;
fileItem.innerHTML = Template_topic_c_attach_item({ fileItem.innerHTML = Template_topic_c_attach_item({
ID: data.elems[hash+"."+ext], ID: data.elems[hash+"."+ext],
ImgSrc: e.target.result, ImgSrc: isImage ? e.target.result : "",
Path: hash+"."+ext, Path: hash+"."+ext,
FullPath: "//" + window.location.host + "/attachs/" + hash + "." + ext, FullPath: "//" + window.location.host + "/attachs/" + hash + "." + ext,
}); });
@ -932,7 +940,7 @@ function mainInit(){
// Quick Topic / Quick Reply // Quick Topic / Quick Reply
function uploadAttachHandler() { function uploadAttachHandler() {
try { try {
uploadFileHandler(this.files,5,(e,filename) => { uploadFileHandler(this.files, 5, (e,filename) => {
// TODO: Use client templates here // TODO: Use client templates here
let fileDock = document.getElementById("upload_file_dock"); let fileDock = document.getElementById("upload_file_dock");
let fileItem = document.createElement("label"); let fileItem = document.createElement("label");

View File

@ -1,4 +1,4 @@
<img src='{{.ImgSrc}}' height=24 width=24 /> {{if .ImgSrc}}<img src='{{.ImgSrc}}' height=24 width=24 />{{end}}
<span class='attach_item_path' aid='{{.ID}}' fullpath='{{.FullPath}}'>{{.Path}}</span> <span class='attach_item_path' aid='{{.ID}}' fullpath='{{.FullPath}}'>{{.Path}}</span>
<button class='attach_item_select'>{{lang "topic.select_button_text"}}</button> <button class='attach_item_select'>{{lang "topic.select_button_text"}}</button>
<button class='attach_item_copy'>{{lang "topic.copy_button_text"}}</button> <button class='attach_item_copy'>{{lang "topic.copy_button_text"}}</button>

View File

@ -994,7 +994,7 @@ input[type=checkbox]:checked + label .sel {
margin-right: 8px; margin-right: 8px;
border-radius: 4px; border-radius: 4px;
} }
.attach_image_holder span { .attach_item_item span {
margin-bottom: 4px; margin-bottom: 4px;
} }
.attach_edit_bay button { .attach_edit_bay button {
@ -1007,10 +1007,13 @@ input[type=checkbox]:checked + label .sel {
padding: 8px; padding: 8px;
width: 100%; width: 100%;
} }
.attach_image_holder span { .attach_item_item span {
margin-right: auto; margin-right: auto;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
width: 350px;
}
.attach_image_holder span {
width: 300px; width: 300px;
} }
.attach_item button { .attach_item button {