Explorar el Código

fix: Thumbnail and preview

gugdun hace 9 meses
padre
commit
0358750059
Se han modificado 6 ficheros con 47 adiciones y 4 borrados
  1. 1 1
      package.json
  2. 6 0
      public/css/main.css
  3. 19 0
      src/routes/attachment.js
  4. 1 1
      src/routes/chat.js
  5. 7 2
      src/views/chat.ejs
  6. 13 0
      src/views/preview.ejs

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "svin-chat",
-  "version": "1.1.0",
+  "version": "1.1.1",
   "description": "Coolest chat in the world 😎",
   "main": "src/index.js",
   "scripts": {

+ 6 - 0
public/css/main.css

@@ -421,6 +421,12 @@ body {
     display: none;
 }
 
+.preview {
+    width: 100dvw;
+    height: 100dvh;
+    object-fit: contain;
+}
+
 @media screen and (max-width: 600px) {
     .hs {
         height: 12px;

+ 19 - 0
src/routes/attachment.js

@@ -48,4 +48,23 @@ router.get("/attachment/:id", async (req, res) => {
     }
 });
 
+router.get("/preview/:id", async (req, res) => {
+    if (req.user) {
+        try {
+            const message = await db.one("SELECT chat_id FROM messages WHERE attachment_id = $1", [ req.params.id ]);
+            const chat = await db.one("SELECT user1_id, user2_id FROM chats WHERE id = $1", [ message?.chat_id ]);
+            if (chat?.user1_id !== req.user.id && chat?.user2_id !== req.user.id) {
+                throw "User has no access to this attachment!";
+            }
+            const attachment = await db.one("SELECT id FROM attachments WHERE id = $1", [ req.params.id ]);
+            res.render("preview", { attachment: attachment.id });
+        } catch (err) {
+            console.log(err);
+            res.json({ success: false });
+        }
+    } else {
+        res.redirect("/login");
+    }
+});
+
 module.exports = router;

+ 1 - 1
src/routes/chat.js

@@ -35,7 +35,7 @@ router.get("/chat/:chat_id", async (req, res) => {
                             text: decrypt(message.text),
                             datetime: message.timestamp,
                             attachment: message.attachment_id
-                        }
+                        };
                     }),
                     hasMoreMessages: moreMessages.length > 0,
                     firstTimestamp: messages[messages.length - 1]?.timestamp || new Date().toISOString()

+ 7 - 2
src/views/chat.ejs

@@ -12,14 +12,19 @@
         <% messages.forEach(function(message) { %>
             <% if (message.username === username) { %>
             <div class="message align-end">
-                <% if (message.attachment) { %>
-                <a href="/attachment/<%- message.attachment %>" target="_blank">
+                <% if (message.attachment !== null) { %>
+                <a href="/preview/<%- message.attachment %>" target="_blank">
                     <img src="/thumbnail/<%- message.attachment %>" />
                 </a>
                 <% } %>
                 <span class="message-text user-message">
             <% } else { %>
             <div class="message align-start">
+                <% if (message.attachment !== null) { %>
+                <a href="/preview/<%- message.attachment %>" target="_blank">
+                    <img src="/thumbnail/<%- message.attachment %>" />
+                </a>
+                <% } %>
                 <span class="message-text friend-message">
             <% } %>
                     <%- message.text %>

+ 13 - 0
src/views/preview.ejs

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="utf-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1" />
+    <link rel="icon" href="/favicon.ico" type="image/x-icon">
+    <link rel="stylesheet" href="/css/main.css">
+    <title>Preview</title>
+</head>
+<body>
+    <img src="/attachment/<%- attachment %>" class="preview" />
+</body>
+</html>