Prechádzať zdrojové kódy

Added upload_image route

deadtom 2 týždňov pred
rodič
commit
94e02c80c8
2 zmenil súbory, kde vykonal 50 pridanie a 1 odobranie
  1. 25 1
      ownchatbot/templates/mgmt.html
  2. 25 0
      ownchatbot/web_panels.py

+ 25 - 1
ownchatbot/templates/mgmt.html

@@ -238,7 +238,7 @@
 
     <div id='settings' class="tabcontent">
         <body style="text-align: left;">
-            <form method="POST" action="/mgmt/settings">
+            <form id='settings' method="POST" action="/mgmt/settings">
                 <table>
                 <h3>OwnchatBot Settings</h3>
                     <thead>
@@ -328,6 +328,30 @@
                 
                 <br><button class="button button2" type="submit">Save Changes</button><br>
             </form>
+            
+                <h3>Celebration Image</h3>
+                <form id='upload' method="post" enctype="multipart/form-data" action="/mgmt/uploadimage">
+                    <table>
+                        <thead>
+                            <tr style="border-bottom: none;">
+                                <th style="width: 20%;"></th>
+                                <th></th>
+                                <th style="width: 50%;"></th>
+                            </tr>
+                        </thead>
+                        <tr>
+                            <td>Current Image:</td>
+                            <td><img src=""></td>
+                            <td></td>
+                        </tr>
+                        <tr>
+                            <td> <label for="c_image">Upload:</label> </td>
+                            <td>The image (can be an animated GIF) that will be displayed in chat when a goal is reached</td>
+                            <td> <input type="file" name="c_image" value="" checked> </td>
+                        </tr>
+                    </table>
+                    <br><button class="button button2" type="submit">Upload</button><br>
+                </form>
             <br><br>
         </body>
     </div>

+ 25 - 0
ownchatbot/web_panels.py

@@ -338,6 +338,31 @@ def settings():
     return redirect(url_for('web_panels.mgmt'))
 
 
+@ocb.route('/mgmt/uploadimage', methods=['POST'])
+def upload_image():
+    if 'file' not in request.files:
+        return redirect(request.url)
+    
+    file = request.files['file']
+    if file.filename == '':
+        return redirect(request.url)
+
+    allowed_extensions = {'png', 'jpg', 'jpeg', 'gif'}
+    if '.' in file.filename:  # Make sure it's an approved file type
+        extension = file.filename.rsplit('.', 1)[1].lower()
+        if extension not in allowed_extensions:
+            return redirect(request.url)
+
+    upload_folder = os.path.join(current_app.instance_path, 'uploads')
+
+    if not os.path.exists(upload_folder):  # Create uploads directory if it doesn't exist
+        os.makedirs(upload_folder)
+
+    filename = secure_filename(file.filename)
+    file.save(os.path.join(upload_folder, filename))
+    return f'Image successfully uploaded: {filename}'
+
+
 @ocb.route('/mgmt/announcements', methods=['GET', 'POST'])  # OwnchatBot settings panel
 @requires_login
 def announcements():