From 50967378fcb12157613fd31f9e8959a7033b869d Mon Sep 17 00:00:00 2001
From: Calin Crisan <ccrisan@gmail.com>
Date: Fri, 25 Dec 2015 21:50:34 +0200
Subject: [PATCH] media browser file removal fix

---
 motioneye/mediafiles.py     | 8 +++++++-
 motioneye/static/js/main.js | 7 +++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/motioneye/mediafiles.py b/motioneye/mediafiles.py
index bd8bbbe..138cb07 100644
--- a/motioneye/mediafiles.py
+++ b/motioneye/mediafiles.py
@@ -652,7 +652,13 @@ def get_media_preview(camera_config, path, media_type, width, height):
         return content
     
     sio = StringIO.StringIO(content)
-    image = Image.open(sio)
+    try:
+        image = Image.open(sio)
+    
+    except Exception as e:
+        logging.error('failed to open media preview image file: %s' % e)
+        return None
+    
     width = width and int(width) or image.size[0]
     height = height and int(height) or image.size[1]
     
diff --git a/motioneye/static/js/main.js b/motioneye/static/js/main.js
index 7cd3e2c..0b80c8a 100644
--- a/motioneye/static/js/main.js
+++ b/motioneye/static/js/main.js
@@ -3501,7 +3501,7 @@ function runMediaDialog(cameraId, mediaType) {
         
         function addEntries() {
             /* add the entries to the media list */
-            entries.forEach(function (entry, i) {
+            entries.forEach(function (entry) {
                 var entryDiv = entry.div;
                 var detailsDiv = null;
                 
@@ -3534,7 +3534,10 @@ function runMediaDialog(cameraId, mediaType) {
                     deleteButton.click(function () {
                         doDeleteFile(basePath + mediaType + '/' + cameraId + '/delete' + entry.path, function () {
                             entryDiv.remove();
-                            entries.splice(i, 1); /* remove entry from group */
+                            var pos = entries.indexOf(entry);
+                            if (pos >= 0) {
+                                entries.splice(pos, 1); /* remove entry from group */
+                            }
 
                             /* update text on group button */
                             groupsDiv.find('div.media-dialog-group-button').each(function () {
-- 
2.39.5