<commit_before>import json
import base64
import datetime
import hmac
import hashlib
import uuid
import time
import Crypto.Hash.SHA
import Crypto.PublicKey.RSA
import Crypto.Signature.PKCS1_v1_5

from django.conf import settings
from django.utils.timezone import now
from django.utils.six.moves.urllib.parse import urlencode, quote, urlparse
from django.utils.encoding import smart_bytes, smart_text


def sign_s3_url(url, expires_in=None, response_headers=None):
    if not expires_in:
        expires_in = settings.AWS_SIGNATURE_EXPIRES_IN

    signer = S3UrlSigner(settings.AWS_ACCESS_KEY_ID,
                         settings.AWS_SECRET_ACCESS_KEY)

    return signer.sign_url('GET', url, expires_in, response_headers)


def sign_cloudfront_url(url, expires_in=None, response_headers=None):
    """
    If CLOUDFRONT_* settings are available return signed CloudFront URL,
    if not fallback to returning signed S3 URL.
    """
    if not expires_in:
        expires_in = settings.AWS_SIGNATURE_EXPIRES_IN

    signer = CloudFrontSigner(settings.CLOUDFRONT_SUBDOMAIN,
                              settings.CLOUDFRONT_KEY_PAIR_ID,
                              settings.CLOUDFRONT_PRIVATE_KEY)

    return signer.sign_url(url, expires_in)


def generate_policy(bucket, mime_type, file_size):
    """
    Returns a Base64-encoded policy document that applies rules
    to file uploads sent by the S3 POST form. It is used to
    authorize the form, and to impose conditions on the files
    that can be uploaded.
    """
    tomorrow = now() + datetime.timedelta(hours=24)
    expiration = tomorrow.strftime('%Y-%m-%dT%H:%M:%S.000Z')

    policy_object = {
        "expiration": expiration,
        "conditions": [<commit_msg>Add logging to sign_cloudfront_url()<commit_after>import json
import base64
import datetime
import hmac
import hashlib
import uuid
import time
import Crypto.Hash.SHA
import Crypto.PublicKey.RSA
import Crypto.Signature.PKCS1_v1_5

from django.conf import settings
from django.utils.timezone import now
from django.utils.six.moves.urllib.parse import urlencode, quote, urlparse
from django.utils.encoding import smart_bytes, smart_text
from django.utils.log import getLogger


logger = getLogger(__name__)


def sign_s3_url(url, expires_in=None, response_headers=None):
    if not expires_in:
        expires_in = settings.AWS_SIGNATURE_EXPIRES_IN

    signer = S3UrlSigner(settings.AWS_ACCESS_KEY_ID,
                         settings.AWS_SECRET_ACCESS_KEY)

    return signer.sign_url('GET', url, expires_in, response_headers)


def sign_cloudfront_url(url, expires_in=None, response_headers=None):
    """
    If CLOUDFRONT_* settings are available return signed CloudFront URL,
    if not fallback to returning signed S3 URL.
    """
    if not expires_in:
        expires_in = settings.AWS_SIGNATURE_EXPIRES_IN

    try:
        signer = CloudFrontSigner(settings.CLOUDFRONT_SUBDOMAIN,
                                  settings.CLOUDFRONT_KEY_PAIR_ID,
                                  settings.CLOUDFRONT_PRIVATE_KEY)

        return signer.sign_url(url, expires_in)
    except Exception as e:
        logger.exception(e)
        return sign_s3_url(url, expires_in)


def generate_policy(bucket, mime_type, file_size):
    """
    Returns a Base64-encoded policy document that applies rules
    to file uploads sent by the S3 POST form. It is used to
    authorize the form, and to impose conditions on the files
    that can be uploaded.
    """
    tomorrow = now() + datetime.timedelta(hours=24)
    expiration = tomorrow.strftime('%Y-%m-%dT%H:%M:%S.000Z')

    policy_object = {
        "expiration": expiration,
        "conditions": [
{'fraction_non_alphanumeric': 0.0732371358562483, 'fraction_numerical': 0.008984481350394773, 'mean_word_length': 3.358244365361803, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6171466', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1138, 'n_words': 292}