]> www.vanbest.org Git - motioneye-debian/commitdiff
added a network share test button
authorCalin Crisan <ccrisan@gmail.com>
Sat, 2 Jul 2016 14:55:48 +0000 (17:55 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 2 Jul 2016 14:55:48 +0000 (17:55 +0300)
motioneye/handlers.py
motioneye/smbctl.py
motioneye/static/js/main.js
motioneye/templates/main.html

index b250b6147adc08cd09b8a207f2fb485cfa13f8a1..4eb923148d738ca4caa1cbe52f9d7b9b888a1c89 100644 (file)
@@ -749,18 +749,16 @@ class ConfigHandler(BaseHandler):
             
         else:
             self.finish_json({'ok': False})
-    
+
     @BaseHandler.auth(admin=True)
     def test(self, camera_id):
         what = self.get_argument('what')
         data = self.get_all_arguments()
         camera_config = config.get_camera(camera_id)
-        if what == 'upload_service':
-            service_name = data.get('service')
-            if not service_name:
-                raise HTTPError(400, 'service_name required')
-
-            if utils.local_motion_camera(camera_config): 
+        
+        if utils.local_motion_camera(camera_config):
+            if what == 'upload_service':
+                service_name = data['service']
                 service = uploadservices.get(camera_id, service_name)
                 service.load(data)
                 if not service:
@@ -775,24 +773,12 @@ class ConfigHandler(BaseHandler):
                 else:
                     logging.warn('accessing %s failed: %s' % (service, result))
                     self.finish_json({'error': result})
-
-            elif utils.remote_camera(camera_config):
-                def on_response(result=None, error=None):
-                    if result is True:
-                        self.finish_json()
-                        
-                    else:
-                        result = result or error
-                        self.finish_json({'error': result})
-
-                remote.test(camera_config, data, on_response)
-
-        elif what == 'email':
-            import sendmail
-            import tzctl
-            import smtplib
-            
-            if utils.local_motion_camera(camera_config):
+    
+            elif what == 'email':
+                import sendmail
+                import tzctl
+                import smtplib
+                
                 logging.debug('testing notification email')
     
                 try:
@@ -819,7 +805,9 @@ class ConfigHandler(BaseHandler):
                     settings.SMTP_TIMEOUT = old_timeout
 
                     self.finish_json()
-                
+                    
+                    logging.debug('notification email test succeeded')
+
                 except Exception as e:
                     if isinstance(e, smtplib.SMTPResponseException):
                         msg = e.smtp_error
@@ -842,20 +830,35 @@ class ConfigHandler(BaseHandler):
 
                     logging.error('notification email test failed: %s' % msg, exc_info=True)
                     self.finish_json({'error': str(msg)})
-            
-            elif utils.remote_camera(camera_config):
-                def on_response(result=None, error=None):
-                    if result is True:
-                        self.finish_json()
-                        
-                    else:
-                        result = result or error
-                        self.finish_json({'error': result})
 
-                remote.test(camera_config, data, on_response)
+            elif what == 'network_share':
+                logging.debug('testing access to network share //%s/%s' % (data['server'], data['share']))
+
+                try:
+                    smbctl.test_share(data['server'], data['share'], data['username'], data['password'], data['root_directory'])
+                    logging.debug('access to network share //%s/%s succeeded' % (data['server'], data['share']))
+                    self.finish_json()
+
+                except Exception as e:
+                    logging.error('access to network share //%s/%s failed: %s' % (data['server'], data['share'], e))
+                    self.finish_json({'error': str(e)})
+
+            else:
+                raise HTTPError(400, 'unknown test %s' % what)
 
+        elif utils.remote_camera(camera_config):
+            def on_response(result=None, error=None):
+                if result is True:
+                    self.finish_json()
+                    
+                else:
+                    result = result or error
+                    self.finish_json({'error': result})
+    
+            remote.test(camera_config, data, on_response)
+        
         else:
-            raise HTTPError(400, 'unknown test %s' % what)
+            raise HTTPError(400, 'cannot test features on this type of camera')
 
     @BaseHandler.auth(admin=True)
     def authorize(self, camera_id):
index 668f7d1212d6cdb1f2a4f04b8be781fc4644c73e..a79e03fcf32330ab4e003785645426e29f5152a4 100644 (file)
@@ -140,6 +140,39 @@ def update_mounts():
     return (should_stop, should_start)
 
 
+def test_share(server, share, username, password, root_directory):
+    mounts = list_mounts()
+    mounts = dict(((m['server'], m['share'], m['username'] or ''), m['mount_point']) for m in mounts)
+    
+    key = (server, share, username or '')
+    mounted = False
+    mount_point = mounts.get(key)
+    if not mount_point:
+        mount_point = _mount(server, share, username, password)
+        if not mount_point:
+            raise Exception('cannot mount network share')
+
+        mounted = True
+    
+    def maybe_umount():
+        if mounted:
+            time.sleep(1)
+            _umount(server, share, username)
+
+    path = os.path.join(mount_point, root_directory)
+    if os.path.exists(path):
+        return maybe_umount()
+    
+    try:
+        os.makedirs(path)
+    
+    except:
+        raise Exception('cannot create root directory')
+    
+    finally:
+        maybe_umount()
+
+
 def _mount(server, share, username, password):
     mount_point = make_mount_point(server, share, username)
     
index 9306b5060b7bc440f45815ad4953669b23eb9910..a990bad0cf8725201d6af2cd7f63a2930fccdafd 100644 (file)
@@ -2534,6 +2534,44 @@ function doTestEmail() {
     });
 }
 
+function doTestNetworkShare() {
+    var q = $('#networkServerEntry, #networkShareNameEntry, #rootDirectoryEntry');
+    var valid = true;
+    q.each(function() {
+        this.validate();
+        if (this.invalid) {
+            valid = false;
+        }
+    });
+    
+    if (!valid) {
+        return runAlertDialog('Make sure all the configuration options are valid!');
+    }
+    
+    showModalDialog('<div class="modal-progress"></div>', null, null, true);
+    
+    var data = {
+        what: 'network_share',
+        server: $('#networkServerEntry').val(),
+        share: $('#networkShareNameEntry').val(),
+        username: $('#networkUsernameEntry').val(),
+        password: $('#networkPasswordEntry').val(),
+        root_directory: $('#rootDirectoryEntry').val()
+    };
+    
+    var cameraId = $('#cameraSelect').val();
+
+    ajax('POST', basePath + 'config/' + cameraId + '/test/', data, function (data) {
+        hideModalDialog(); /* progress */
+        if (data.error) {
+            showErrorMessage('Accessing network share failed: ' + data.error + '!');
+        }
+        else {
+            showPopupMessage('Accessing network share succeeded!', 'info');
+        }
+    });
+}
+
 function doDownloadZipped(cameraId, groupKey) {
     showModalDialog('<div class="modal-progress"></div>', null, null, true);
     ajax('GET', basePath + 'picture/' + cameraId + '/zipped/' + groupKey + '/', null, function (data) {
@@ -4511,6 +4549,7 @@ $(document).ready(function () {
     /* test buttons */
     $('div#uploadTestButton').click(doTestUpload);
     $('div#emailTestButton').click(doTestEmail);
+    $('div#networkShareTestButton').click(doTestNetworkShare);
     
     initUI();
     beginProgress();
index 09cbe400edb8fd13bf11789220f6ef06d2b5ab1b..5a230ecb0a5cfe05714f8300875517055e9d1504 100644 (file)
                         <td class="settings-item-value"><input type="text" class="styled storage camera-config" id="rootDirectoryEntry"></td>
                         <td><span class="help-mark" title="the root path (on the selected storage device) where the files will be saved">?</span></td>
                     </tr>
+                    <tr class="settings-item advanced-setting" depends="storageDevice=network-share">
+                        <td class="settings-item-label"><span class="settings-item-label"></span></td>
+                        <td class="settings-item-value"><div class="button normal-button test-button" id="networkShareTestButton">Test Share</div></td>
+                        <td><span class="help-mark" title="click this button to test the network share connectivity after you have filled in the required details">?</span></td>
+                    </tr>
                     <tr class="settings-item advanced-setting">
                         <td colspan="100"><div class="settings-item-separator"></div></td>
                     </tr>