-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* News API add ability to retrieve images * Updated following review * Set superdesk core version (cherry picked from commit d74c6ec)
- Loading branch information
1 parent
6137772
commit 56812e7
Showing
11 changed files
with
162 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import superdesk | ||
import flask | ||
from newsroom.news_api.api_tokens import CompanyTokenAuth | ||
from flask import abort | ||
from newsroom.upload import ASSETS_RESOURCE | ||
from flask_babel import gettext | ||
import bson.errors | ||
from werkzeug.wsgi import wrap_file | ||
from newsroom.news_api.utils import post_api_audit | ||
|
||
blueprint = superdesk.Blueprint('assets', __name__) | ||
|
||
|
||
def init_app(app): | ||
superdesk.blueprint(blueprint, app) | ||
|
||
|
||
@blueprint.route('/assets/<path:asset_id>', methods=['GET']) | ||
def get_item(asset_id): | ||
if CompanyTokenAuth().check_auth(flask.request.headers.get('Authorization'), None, None, 'GET'): | ||
try: | ||
media_file = flask.current_app.media.get(asset_id, ASSETS_RESOURCE) | ||
except bson.errors.InvalidId: | ||
media_file = None | ||
if not media_file: | ||
flask.abort(404) | ||
|
||
data = wrap_file(flask.request.environ, media_file, buffer_size=1024 * 256) | ||
response = flask.current_app.response_class( | ||
data, | ||
mimetype=media_file.content_type, | ||
direct_passthrough=True) | ||
response.content_length = media_file.length | ||
response.last_modified = media_file.upload_date | ||
response.set_etag(media_file.md5) | ||
response.make_conditional(flask.request) | ||
response.headers['Content-Disposition'] = 'inline' | ||
post_api_audit({'_items': [{'_id': asset_id}]}) | ||
return response | ||
else: | ||
abort(401, gettext('Invalid token')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .ninjs import NINJSFormatter | ||
from newsroom.news_api.utils import remove_internal_renditions | ||
|
||
|
||
class NINJSFormatter2(NINJSFormatter): | ||
""" | ||
Overload the NINJSFormatter and add the associations as a field to copy | ||
""" | ||
|
||
def __init__(self): | ||
self.direct_copy_properties += ('associations',) | ||
|
||
def _transform_to_ninjs(self, item): | ||
return remove_internal_renditions(super()._transform_to_ninjs(item)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ xhtml2pdf | |
werkzeug>=0.9.4,<=0.11.15 | ||
-e . | ||
git+git://github.com/superdesk/[email protected]#egg=superdesk-planning | ||
git+git://github.com/superdesk/superdesk-core.git@master#egg=superdesk-core | ||
git+git://github.com/superdesk/superdesk-core.git@v1.33.7#egg=superdesk-core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
from superdesk.storage.desk_media_storage import SuperdeskGridFSMediaStorage | ||
from tests.news_api.test_api_audit import audit_check | ||
|
||
|
||
def get_fixture_path(fixture): | ||
return os.path.join(os.path.dirname(__file__), '../fixtures', fixture) | ||
|
||
|
||
def setup_image(app): | ||
with open(get_fixture_path('picture.jpg'), 'rb') as f: | ||
res = SuperdeskGridFSMediaStorage(app=app).put(f, 'picture.jpg', content_type='image/jpg') | ||
return res | ||
|
||
|
||
def test_get_asset(client, app): | ||
app.data.insert('companies', [{"_id": "company_123", "name": "Test Company", "is_enabled": True}]) | ||
app.data.insert('news_api_tokens', [{"company": "company_123", "enabled": True}]) | ||
token = app.data.find_one('news_api_tokens', req=None, company='company_123') | ||
|
||
id = setup_image(app) | ||
response = client.get('api/v1/assets/{}'.format(id), headers={'Authorization': token.get('token')}) | ||
assert response.status_code == 200 | ||
audit_check(str(id)) | ||
|
||
|
||
def test_authorization_get_asset(client, app): | ||
response = client.get('api/v1/assets/{}'.format(id), headers={'Authorization': 'xxxxxxxx'}) | ||
assert response.status_code == 401 |