From af3f51a9016418af2797bb210b2f0942e08b8d18 Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Mon, 11 Nov 2013 17:47:10 +0200 Subject: [PATCH] added a download button to each media browser entry --- doc/todo.txt | 2 +- src/handlers.py | 34 +++++++++++++++++++++------------- src/remote.py | 11 +++++++---- static/css/main.css | 21 +++++++++++++++++++++ static/js/main.js | 12 +++++++++++- 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/doc/todo.txt b/doc/todo.txt index ddcf181..ca23fec 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,4 +1,4 @@ --> fix remote picture previewer +-> add support for picture resizing -> add a download option in media list -> add a previewer for movies -> make camera frames positions configurable diff --git a/src/handlers.py b/src/handlers.py index eb62cb2..fb223a0 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -613,7 +613,7 @@ class PictureHandler(BaseHandler): camera_config = config.get_camera(camera_id) if camera_config['@proto'] != 'v4l2': - def on_response(remote_list): + def on_response(response): camera_url = remote.make_remote_camera_url( camera_config.get('@host'), camera_config.get('@port'), @@ -621,19 +621,26 @@ class PictureHandler(BaseHandler): camera_full_url = camera_config['@proto'] + '://' + camera_url - if remote_list is None: - return self.finish_json({'error': 'Failed to get picture list for %(url)s.' % { + if response is None: + return self.finish_json({'error': 'Failed to download picture from %(url)s.' % { 'url': camera_full_url}}) - self.finish_json(remote_list) - - remote.download_picture( + pretty_filename = os.path.basename(filename) # no camera name available w/o additional request + self.set_header('Content-Type', 'image/jpeg') + self.set_header('Content-Disposition', 'attachment; filename=' + pretty_filename + ';') + + self.finish(response) + + remote.get_media( camera_config.get('@host'), camera_config.get('@port'), camera_config.get('@username'), camera_config.get('@password'), - camera_config.get('@remote_camera_id'), on_response) - + camera_config.get('@remote_camera_id'), + on_response, + filename=filename, + media_type='picture') + else: content = mediafiles.get_media_content(camera_config, filename) @@ -663,22 +670,23 @@ class PictureHandler(BaseHandler): camera_full_url = camera_config['@proto'] + '://' + camera_url if response is None: - return self.finish_json({'error': 'Failed to get picture list for %(url)s.' % { + return self.finish_json({'error': 'Failed to get picture preview for %(url)s.' % { 'url': camera_full_url}}) self.set_header('Content-Type', 'image/jpeg') self.finish(response) - remote.preview_picture( + remote.get_media( camera_config.get('@host'), camera_config.get('@port'), camera_config.get('@username'), camera_config.get('@password'), camera_config.get('@remote_camera_id'), - filename, + on_response, + filename=filename, + media_type='picture', width=self.get_argument('width', None), - height=self.get_argument('height', None), - callback=on_response) + height=self.get_argument('height', None)) else: content = mediafiles.get_media_content(camera_config, filename) diff --git a/src/remote.py b/src/remote.py index 21299da..7766293 100644 --- a/src/remote.py +++ b/src/remote.py @@ -243,19 +243,22 @@ def list_pictures(host, port, username, password, camera_id, callback): http_client.fetch(request, on_response) -def preview_picture(host, port, username, password, camera_id, filename, width, height, callback): - logging.debug('getting preview for file %(filename)s of remote camera %(id)s on %(host)s:%(port)s' % { +def get_media(host, port, username, password, camera_id, callback, filename, media_type, width=None, height=None): + logging.debug('getting file %(filename)s of remote camera %(id)s on %(host)s:%(port)s' % { 'filename': filename, 'id': camera_id, 'host': host, 'port': port}) - uri = '/picture/%(id)s/preview/%(filename)s/?' % { + uri = '/%(media_type)s/%(id)s/%(op)s/%(filename)s?' % { + 'media_type': media_type, + 'op': 'preview' if (width or height) else 'download', 'id': camera_id, 'filename': filename} if width: uri += 'width=' + str(width) + if height: uri += 'height=' + str(height) @@ -263,7 +266,7 @@ def preview_picture(host, port, username, password, camera_id, filename, width, def on_response(response): if response.error: - logging.error('failed to get preview for file %(filename)s of remote camera %(id)s on %(host)s:%(port)s: %(msg)s' % { + logging.error('failed to get media file %(filename)s of remote camera %(id)s on %(host)s:%(port)s: %(msg)s' % { 'filename': filename, 'id': camera_id, 'host': host, diff --git a/static/css/main.css b/static/css/main.css index 6c44ffb..d90e787 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -404,6 +404,27 @@ div.media-list-entry-moment { white-space: nowrap; } +div.media-list-download-button { + float: right; + height: 1.5em; + line-height: 1.5em; + text-align: center; + margin: 1.2em 0.5em; + padding: 0px 0.5em; + color: white; + background-color: #317CAD; + border-radius: 3px; + transition: all 0.1s linear; +} + +div.media-list-download-button:HOVER { + background-color: #3498db; +} + +div.media-list-download-button:ACTIVE { + background-color: #317CAD; +} + div.picture-dialog-content { position: relative; } diff --git a/static/js/main.js b/static/js/main.js index cb6d00d..e58c674 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1393,11 +1393,21 @@ function runMediaDialog(cameraId, mediaType) { var previewImg = $(''); entryDiv.append(previewImg); + var downloadButton = $('
download
'); + entryDiv.append(downloadButton); + var nameDiv = $('
' + entry.name + '
'); entryDiv.append(nameDiv); var momentDiv = $('
' + entry.momentStr + '
'); entryDiv.append(momentDiv); + + downloadButton.click(function () { + window.location.href = '/picture/' + cameraId + '/download' + entry.path; + + return false; + }); + entryDiv.click(function () { if (mediaType === 'picture') { runPictureDialog(entries, pos); @@ -1452,7 +1462,7 @@ function addCameraFrameUi(cameraId, cameraName, framerate) { '' + '
' + '
' + - '
' + +// '
' + '
' + // '
' + '
' + -- 2.39.5