-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* flake * Fixing super method call * add auth handler, fix CORS * add assetHandler and NotFoundHandler * add network to AssetHandler * fix unused var * add detailed query string to shows, fix mime_type * flake8 * fix import * Flake8 fixes * Detailed show returns episodes grouped by season
- Loading branch information
1 parent
ecdc557
commit d1f3534
Showing
7 changed files
with
133 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# coding=utf-8 | ||
"""Request handler for assets.""" | ||
import glob | ||
import mimetypes | ||
import os | ||
|
||
from .base import BaseRequestHandler | ||
from .... import app | ||
|
||
|
||
class AssetHandler(BaseRequestHandler): | ||
"""Asset request handler.""" | ||
|
||
def get(self, asset_group=None, query=None, *args, **kwargs): | ||
"""Get an asset.""" | ||
if asset_group and query: | ||
if asset_group == 'show': | ||
asset_type = self.get_argument('type', default='banner') | ||
return self._serve_asset(path=os.path.join(app.CACHE_DIR, 'images/'), filename=query + '.' + asset_type) | ||
if asset_group == 'network': | ||
return self._serve_asset(path=os.path.join(app.PROG_DIR, 'static/images/network/'), filename=query) | ||
|
||
def _serve_asset(self, path=None, filename=None): | ||
"""Serve the asset from the provided path.""" | ||
if path and filename: | ||
for infile in glob.glob(os.path.join(path, filename.lower() + '.*')): | ||
path = infile | ||
mime_type, _ = mimetypes.guess_type(path) | ||
if mime_type: | ||
self.set_status(200) | ||
self.set_header('Content-type', mime_type) | ||
try: | ||
with open(path, 'rb') as f: | ||
while 1: | ||
data = f.read(16384) | ||
if not data: | ||
break | ||
self.write(data) | ||
self.finish() | ||
except IOError: | ||
self.api_finish(status=404, error='Asset or Asset Type Does Not Exist') | ||
else: | ||
self.api_finish(status=404, error='Asset or Asset Type Does Not Exist') |
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,43 @@ | ||
# coding=utf-8 | ||
"""Request handler for authentication.""" | ||
|
||
import base64 | ||
|
||
from .base import BaseRequestHandler | ||
from .... import app, helpers, logger, notifiers | ||
|
||
|
||
class LoginHandler(BaseRequestHandler): | ||
"""Login request handler.""" | ||
|
||
def set_default_headers(self): | ||
"""Set default CORS headers.""" | ||
super(LoginHandler, self).set_default_headers() | ||
self.set_header('Access-Control-Allow-Methods', 'POST, OPTIONS') | ||
|
||
def prepare(self): | ||
"""Prepare.""" | ||
pass | ||
|
||
def post(self, *args, **kwargs): | ||
"""Submit login.""" | ||
username = app.WEB_USERNAME | ||
password = app.WEB_PASSWORD | ||
|
||
if self.request.headers.get('Authorization'): | ||
auth_decoded = base64.decodestring(self.request.headers.get('Authorization')[6:]) | ||
decoded_username, decoded_password = auth_decoded.split(':', 2) | ||
|
||
if app.NOTIFY_ON_LOGIN and not helpers.is_ip_private(self.request.remote_ip): | ||
notifiers.notify_login(self.request.remote_ip) | ||
|
||
if username != decoded_username or password != decoded_password: | ||
logger.log('User attempted a failed login to the Medusa API from IP: {ip}'.format(ip=self.request.remote_ip), logger.WARNING) | ||
self.api_finish(status=401, error='Invalid credentials') | ||
else: | ||
logger.log('User logged into the Medusa API', logger.INFO) | ||
self.api_finish(data={ | ||
'apiKey': app.API_KEY | ||
}) | ||
else: | ||
self.api_finish(status=401, error='No Credentials Provided') |
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