From 6ef218055ec6b8f1e2a460ca0495cfb8e0b9975a Mon Sep 17 00:00:00 2001 From: biglyderv Date: Sun, 2 Mar 2025 15:28:47 -0500 Subject: [PATCH] better embeds --- lib.js | 2 +- routes/init.js | 17 +++++++++++++++-- views/comment.ejs | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib.js b/lib.js index 39f80ab..0942a19 100644 --- a/lib.js +++ b/lib.js @@ -15,7 +15,7 @@ function splitUp(content) { for (let i in out) { let res = out[i]; if (res.startsWith('file::')) { - out[i] = ['file', res.slice('file::'.length)]; + out[i] = ['file', new URL(res.slice('file::'.length), 'https://tbg.dervland.net/').pathname , res.split('.').pop()]; continue; } if (res.startsWith('https://')) { diff --git a/routes/init.js b/routes/init.js index 763fe31..7bfd198 100644 --- a/routes/init.js +++ b/routes/init.js @@ -7,13 +7,26 @@ import { randomBytes } from 'node:crypto'; let db = await initDb(); +const mimes = { + 'image/gif': 'gif', + 'image/jpeg': 'jpg', + 'text/plain': 'txt', + 'application/json': 'json', + 'video/mp4': 'mp4', + 'video/webm': 'webm', + 'image/png': 'png', + 'audio/mpeg': 'mp3', + 'audio/wav': 'wav', +}; + const upload = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './uploads/') }, filename: function (req, file, cb) { const uniqueSuffix = randomBytes(8).toString('hex'); - cb(null, uniqueSuffix + '.png') + const type = mimes[file.mimetype] || 'txt'; + cb(null, uniqueSuffix + '.' + type) } }); @@ -81,7 +94,7 @@ function initr(req, res, next) { { link: '/walls/list', icon: '/walls.svg', name: 'Explore' }, { link: '/users/' + (res.auth ? res.auth.username : ''), icon: '/login.svg', name: 'You' }, { link: '/you/messages', icon: '/mail.svg', name: 'Messages' }, - { link: '/you/logout', icon: '/logout.svg', name: 'Leave' } + { link: '/you/logout', icon: '/logout.svg', name: 'Leave' } ]; if (!res.auth || !res.auth.valid) { diff --git a/views/comment.ejs b/views/comment.ejs index afc6a93..bbb7c32 100644 --- a/views/comment.ejs +++ b/views/comment.ejs @@ -10,9 +10,19 @@
<% for (let contentN of content) { %> <% if (contentN[0] == 'file') { %> - '> + + <% if (['gif','png','jpg'].indexOf(contentN[2]) != -1) { %> + + <% } else if (['txt','json'].indexOf(contentN[2]) != -1) { %> + <%= contentN[1] %> + <% } else if (['mp4','mp3','webm','wav'].indexOf(contentN[2]) != -1) { %> + + <% } %> + <% } else if (contentN[0] == 'link') { %> - <%= contentN[1] %> + <%= contentN[1] %> <% } else if (contentN[0].replaceAll(' ','').length > 0) { %> <%= contentN[0] %> <% } } %>