From: Calin Crisan Date: Sat, 2 Jul 2016 14:55:48 +0000 (+0300) Subject: added a network share test button X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=e2eb676aaed46f8f35d158736fa0dae8ef03dc8a;p=motioneye-debian added a network share test button --- diff --git a/motioneye/handlers.py b/motioneye/handlers.py index b250b61..4eb9231 100644 --- a/motioneye/handlers.py +++ b/motioneye/handlers.py @@ -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): diff --git a/motioneye/smbctl.py b/motioneye/smbctl.py index 668f7d1..a79e03f 100644 --- a/motioneye/smbctl.py +++ b/motioneye/smbctl.py @@ -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) diff --git a/motioneye/static/js/main.js b/motioneye/static/js/main.js index 9306b50..a990bad 100644 --- a/motioneye/static/js/main.js +++ b/motioneye/static/js/main.js @@ -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('', 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('', 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(); diff --git a/motioneye/templates/main.html b/motioneye/templates/main.html index 09cbe40..5a230ec 100644 --- a/motioneye/templates/main.html +++ b/motioneye/templates/main.html @@ -352,6 +352,11 @@ ? + + +
Test Share
+ ? +