<commit_before>import time
import base64
import urllib
import hmac
import sha

from framework.flask import request

from website.project.decorators import must_have_addon
from website.project.decorators import must_be_contributor_or_public
from website.addons.s3.api import create_bucket


def generate_signed_url(mime, file_name, s3):

    expires = int(time.time() + 10)
    amz_headers = 'x-amz-acl:private'

    request_to_sign = str("PUT\n\n{mime_type}\n{expires}\n{amz_headers}\n/{resource}".format(
        mime_type=mime, expires=expires, amz_headers=amz_headers, resource=s3.bucket + '/' + file_name))

    url = 'https://s3.amazonaws.com/{bucket}/{filename}'.format(
        filename=file_name, bucket=s3.bucket)

    signed = urllib.quote_plus(base64.encodestring(
        hmac.new(str(s3.user_settings.secret_key), request_to_sign, sha).digest()).strip())

    return '{url}?AWSAccessKeyId={access_key}&Expires={expires}&Signature={signed}'.format(url=url, access_key=s3.user_settings.access_key, expires=expires, signed=signed),
    #/blackhttpmagick


@must_be_contributor_or_public
@must_have_addon('s3', 'node')
def create_new_bucket(*args, **kwargs):
    user = kwargs['auth'].user
    user_settings = user.get_addon('s3')
    message = create_bucket(user_settings, request.json.get('bucket_name'))
    if message is True:
        return {}, 200
    else:
        return {'message': message}, 500


def get_cache_file_name(key_name, etag):
    return u'{0}_{1}.html'.format(key_name.replace('/', ''), etag)<commit_msg>Better Error codes, and server side checking.<commit_after>import time
import base64
import urllib
import hmac
import sha
import re

from framework.flask import request

from website.project.decorators import must_have_addon
from website.project.decorators import must_be_contributor_or_public
from website.addons.s3.api import create_bucket


def generate_signed_url(mime, file_name, s3):

    expires = int(time.time() + 10)
    amz_headers = 'x-amz-acl:private'

    request_to_sign = str("PUT\n\n{mime_type}\n{expires}\n{amz_headers}\n/{resource}".format(
        mime_type=mime, expires=expires, amz_headers=amz_headers, resource=s3.bucket + '/' + file_name))

    url = 'https://s3.amazonaws.com/{bucket}/{filename}'.format(
        filename=file_name, bucket=s3.bucket)

    signed = urllib.quote_plus(base64.encodestring(
        hmac.new(str(s3.user_settings.secret_key), request_to_sign, sha).digest()).strip())

    return '{url}?AWSAccessKeyId={access_key}&Expires={expires}&Signature={signed}'.format(url=url, access_key=s3.user_settings.access_key, expires=expires, signed=signed),
    #/blackhttpmagick


@must_be_contributor_or_public
@must_have_addon('s3', 'node')
def create_new_bucket(*args, **kwargs):
    user = kwargs['auth'].user
    user_settings = user.get_addon('s3')
    validateName = re.compile('^(?!.*(\.\.|-\.))[^.][\w\d.-]{2,61}[^.]$')
    if not validateName.match(request.json.get('bucket_name')):
        return {'message': 'That bucket name is not valid.'}, 406
    message = create_bucket(user_settings, request.json.get('bucket_name'))
    if message is True:
        return {}
    else:
        return {'message': message}, 406


def get_cache_file_name(key_name, etag):
    return u'{0}_{1}.html'.format(key_name.replace('/', ''), etag)
{'fraction_non_alphanumeric': 0.14587737843551796, 'fraction_numerical': 0.014799154334038054, 'mean_word_length': 4.8827708703374775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4019194', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1124, 'n_words': 194}