Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
189 additions
and
6 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
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
from . import editor | ||
from . import groups | ||
from . import debug | ||
from . import polly_api |
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 @@ | ||
""" | ||
APIs for polly and their routes | ||
Includes: | ||
- /api/uploadkr | ||
- /api/uploadpost | ||
""" | ||
|
||
import os | ||
import json | ||
from builtins import str | ||
from collections import namedtuple | ||
from flask import request, render_template, redirect, Blueprint, current_app, make_response | ||
from flask_login import login_required | ||
from sqlalchemy import case, desc | ||
|
||
from .. import permissions | ||
from ..proxies import db_session, current_repo | ||
from ..utils.posts import get_posts | ||
from ..models import Post, Tag, User, PageView | ||
from ..utils.requests import from_request_get_feed_params | ||
from ..utils.render import render_post_tldr | ||
from ..utils.s3_talk import download_dir,download_from_s3 | ||
blueprint = Blueprint('api', __name__, template_folder='../templates', static_folder='../static') | ||
|
||
@blueprint.route('/api/uploadkr') | ||
@PageView.logged | ||
def upload_kr(): | ||
""" | ||
API to upload a KR to the server | ||
args: | ||
S3-Path | ||
""" | ||
global current_repo,current_app | ||
path = request.args.get('path') | ||
dir_name,dir_path = download_dir(path) | ||
dbobj = current_repo.migrate_to_dbrepo(dir_path,dir_name) | ||
current_repo = current_app.append_repo_obj(dir_name,dbobj) | ||
return redirect('/feed') | ||
|
||
|
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,70 @@ | ||
""" | ||
Given S3 path download to local and trigger upload to database repository | ||
""" | ||
import os | ||
import boto3,botocore | ||
import tempfile | ||
import errno | ||
|
||
BUCKET_NAME = os.environ['KR_BUCKET_NAME'] | ||
S3 = boto3.resource('s3') | ||
CLIENT = boto3.client('s3') | ||
|
||
def assert_dir(path): | ||
""" | ||
Checks if directory tree in path exists. :param path: the path to check if it exists | ||
""" | ||
try: | ||
os.makedirs(path) | ||
except OSError as e: | ||
if e.errno != errno.EEXIST: | ||
raise | ||
|
||
def _extract_name(path): | ||
# Internal function to return name of the folder/file to use downstream | ||
if path.endswith('/'): | ||
return path.split('/')[-2] | ||
else: | ||
return path.split('/')[-1] | ||
|
||
def download_dir(path): | ||
""" | ||
Downloads recursively the given S3 path to the target directory. | ||
arg path: The S3 directory to download. | ||
""" | ||
|
||
# Handle missing / at end of prefix | ||
if not path.endswith('/'): | ||
path += '/' | ||
|
||
dir_name = _extract_name(path) | ||
target = "/tmp/%s/"%dir_name | ||
|
||
paginator = CLIENT.get_paginator('list_objects_v2') | ||
for result in paginator.paginate(Bucket=BUCKET_NAME, Prefix=path): | ||
# Download each file individually | ||
for key in result['Contents']: | ||
# Calculate relative path | ||
rel_path = key['Key'][len(path):] | ||
# Skip paths ending in / | ||
if not key['Key'].endswith('/'): | ||
local_file_path = os.path.join(target, rel_path) | ||
# Make sure directories exist | ||
local_file_dir = os.path.dirname(local_file_path) | ||
assert_dir(local_file_dir) | ||
CLIENT.download_file(BUCKET_NAME, key['Key'], local_file_path) | ||
return dir_name,target | ||
|
||
|
||
|
||
|
||
def download_from_s3(path): | ||
try: | ||
fname = _extract_path(path) | ||
temp_name = '/tmp/%s'%fname | ||
S3.Bucket(BUCKET_NAME).download_file(path,temp_name) | ||
except botocore.exceptions.ClientError as e: | ||
if e.response['Error']['Code'] == "404": | ||
return -1 | ||
|
||
return fname,temp_name |
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