From: Calin Crisan Date: Sat, 20 Aug 2016 17:39:46 +0000 (+0300) Subject: fixed various bugs when changing upload services settings or provider X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=e4c69fae4ca0df1a0ca79102f5d6cf62c253883b;p=motioneye-debian fixed various bugs when changing upload services settings or provider --- diff --git a/motioneye/config.py b/motioneye/config.py index 1fb4690..fb8b9e3 100644 --- a/motioneye/config.py +++ b/motioneye/config.py @@ -32,6 +32,7 @@ from tornado.ioloop import IOLoop import diskctl import powerctl import settings +import tasks import update import uploadservices import utils @@ -801,6 +802,8 @@ def motion_camera_ui_to_dict(ui, old_config=None): service.load(upload_settings) service.save() + tasks.add(0, uploadservices.invalidate, tag='invalidate_uploadservices', async=True) + if ui['text_overlay']: left_text = ui['left_text'] if left_text == 'camera-name': diff --git a/motioneye/static/js/main.js b/motioneye/static/js/main.js index 26a2ec7..2615682 100644 --- a/motioneye/static/js/main.js +++ b/motioneye/static/js/main.js @@ -2486,6 +2486,15 @@ function doTestUpload() { var cameraId = $('#cameraSelect').val(); ajax('POST', basePath + 'config/' + cameraId + '/test/', data, function (data) { + /* clear the authorization key as it's definitely not usable anymore; + * the credentials must have already been obtained and saved */ + $('#uploadAuthorizationKeyEntry').val(''); + + /* also clear it from the pending configs dict */ + Object.keys(pushConfigs).forEach(function (id) { + delete pushConfigs[id].upload_authorization_key; + }); + hideModalDialog(); /* progress */ if (data.error) { showErrorMessage('Accessing the upload service failed: ' + data.error + '!'); diff --git a/motioneye/tasks.py b/motioneye/tasks.py index e29c636..47418e0 100644 --- a/motioneye/tasks.py +++ b/motioneye/tasks.py @@ -31,7 +31,11 @@ import settings _INTERVAL = 10 _STATE_FILE_NAME = 'tasks.pickle' _MAX_TASKS = 100 -_POOL_SIZE = 1 # we just need one extra process to handle async tasks + +# we must be sure there's only one extra process +# that handles all asynchronous tasks, +# as we often invalidate various caches +_POOL_SIZE = 1 _tasks = [] _pool = None diff --git a/motioneye/uploadservices.py b/motioneye/uploadservices.py index 1010293..5728acc 100644 --- a/motioneye/uploadservices.py +++ b/motioneye/uploadservices.py @@ -169,6 +169,7 @@ class GoogleDrive(UploadService): def test_access(self): try: + self._credentials = None # invalidate credentials self._folder_ids = {} self._get_folder_id() return True @@ -217,10 +218,11 @@ class GoogleDrive(UploadService): if 'location' in data: self._location = data['location'] self._folder_ids = {} - if 'credentials' in data: - self._credentials = data['credentials'] if 'authorization_key' in data: self._authorization_key = data['authorization_key'] + self._credentials = None + if 'credentials' in data: + self._credentials = data['credentials'] def _get_folder_id(self, path=''): now = time.time() @@ -454,6 +456,8 @@ class Dropbox(UploadService): return self.AUTH_URL + '?' + urllib.urlencode(query) def test_access(self): + self._credentials = None # invalidate credentials + body = { 'path': self._clean_location(), 'recursive': False, @@ -502,10 +506,11 @@ class Dropbox(UploadService): def load(self, data): if 'location' in data: self._location = data['location'] - if 'credentials' in data: - self._credentials = data['credentials'] if 'authorization_key' in data: self._authorization_key = data['authorization_key'] + self._credentials = None + if 'credentials' in data: + self._credentials = data['credentials'] def _clean_location(self): location = self._location @@ -604,8 +609,10 @@ def get(camera_id, service_name): if _services is None: _services = _load() + + camera_id = str(camera_id) - service = _services.get(str(camera_id), {}).get(service_name) + service = _services.get(camera_id, {}).get(service_name) if service is None: cls = UploadService.get_service_classes().get(service_name) if cls: @@ -673,7 +680,7 @@ def _save(services): data = {} for camera_id, camera_services in services.iteritems(): for name, service in camera_services.iteritems(): - data.setdefault(camera_id, {})[name] = service.dump() + data.setdefault(str(camera_id), {})[name] = service.dump() try: json.dump(data, file, sort_keys=True, indent=4) @@ -685,6 +692,12 @@ def _save(services): file.close() +def invalidate(): + global _services + + _services = None + + def upload_media_file(camera_id, target_dir, service_name, filename): service = get(camera_id, service_name) if not service: @@ -694,4 +707,4 @@ def upload_media_file(camera_id, target_dir, service_name, filename): service.upload_file(target_dir, filename) except Exception as e: - logging.error('failed to upload file "%s" with service %s: %s' % (filename, service, e)) + logging.error('failed to upload file "%s" with service %s: %s' % (filename, service, e), exc_info=True)