Esempio n. 1
0
def generate_dl_link():
    filelist = []
    for i in get_s3_files('uploads'):
        filelist.append(i[0])
    try:
        keyname = request.args['keyname']
        # keyname = base64.decodestring(urllib.parse.unquote(keyname))
        keyname = urllib.parse.unquote(keyname)
        # print('dendl :' + keyname)
        version_id = request.args['version']
        version_id = base64.decodestring(urllib.parse.unquote(version_id))
        assert keyname in filelist
        dl_url = get_temp_s3_url(keyname, version_id)
        filename = keyname.split('/')[-1]
        logging.info('User [%s] generated a url for %s' %
                     (get_user(request), filename))
        return render_template('s3_redir_dl.html',
                               url=dl_url,
                               keyname=keyname,
                               filename=filename,
                               ua=True)

    except:
        return render_template('error.html',
                               message='Invalid Parameters %s' %
                               str(sys.exc_info()),
                               ua=True)
def generate_dl_link():
    filelist = []
    for i in get_s3_files('uploads'):
        filelist.append(i[0])
    try:
        keyname = request.args['keyname']
        keyname = base64.decodestring(urllib2.unquote(keyname))
        version_id = request.args['version']
        version_id = base64.decodestring(urllib2.unquote(version_id))
        assert keyname in filelist
        dl_url = get_temp_s3_url(keyname, version_id)
        filename = keyname.split('/')[-1]
        logging.info('User [%s] generated a url for %s' % (get_user(request),
                                                           filename))
        return render_template('s3_redir_dl.html',
                               url=dl_url,
                               keyname=keyname,
                               filename=filename)

    except:
        return render_template('error.html',
                               message='Invalid Parameters %s'
                                       % str(sys.exc_info()))
Esempio n. 3
0
def generate_form():
    ua = True

    try:
        bucket_name = os.environ['BUCKET']
        access_key, secret_key = get_env_creds()
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error obtaining valid creds: %s' %
                               str(sys.exc_info()))

    try:
        lc_expiration = request.form['lifecycle']
        lc_expiration = int(lc_expiration)
        assert lc_expiration <= 180
    except:
        return render_template('error.html',
                               ua=True,
                               message='Invalid Lifecycle Duration')

    try:
        exp = request.form['exp']
        exp = datetime.strptime(exp, '%Y-%m-%d').isoformat() + 'Z'
    except:
        return render_template('error.html',
                               ua=True,
                               message='Invalid Expiration Date')

    try:
        maxupload = request.form['maxupload']
        max_megs = int(maxupload) / (1024 * 1024)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Invalid File Size')

    try:
        directory = request.form['directory']
        assert valid_name(directory)
        assert directory != ''
        directory = 'uploads/' + directory
    except:
        return render_template('error.html',
                               ua=True,
                               message='Invalid/Empty Name for \
                               Customer/Identifier. \'A-Z\', \'0-9\',\
                                \'-\' and \'_\' only.')

    try:
        notes = request.form['notes']
    except:
        notes = ''

    try:
        policy = gen_policy(bucket_name=bucket_name,
                            expiration=exp,
                            max_byte_size=maxupload,
                            directory=directory)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error Gen Policy: %s' %
                               str(sys.exc_info()))

    try:
        signature, policy = sign_policy(policy=policy, secret=secret_key)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error Sign Policy: %s' %
                               str(sys.exc_info()))

    try:
        env = Environment(loader=FileSystemLoader('templates'))
        template = env.get_template('upload.html')
        html = template.render(bucket_name=bucket_name,
                               access_key=access_key,
                               policy=policy,
                               signature=signature,
                               max_in_megs=max_megs,
                               notes=notes,
                               directory=directory)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error rendering template: %s' %
                               str(sys.exc_info()))

    try:
        create_folder_and_lifecycle(bucket_name=bucket_name,
                                    directory=directory,
                                    expiration=lc_expiration)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error setting lifecycle: %s' %
                               str(sys.exc_info()))

    try:
        url = upload_s3(contents=html, bucket_name=bucket_name)
    except:
        return render_template('error.html',
                               ua=True,
                               message='Error uploading to s3: %s' %
                               str(sys.exc_info()))

    if url is None:
        return render_template('error.html',
                               ua=True,
                               message='Could not generate URL. Check Logs')

    # strip signature from url; we dont need since the form is public
    url = url.split('?')[0]

    logging.info('User [%s] generated a form called %s' %
                 (get_user(request), directory))
    portal = directory[len(PREFIX):]
    return render_template('done.html',
                           url=url,
                           bucket_name=bucket_name,
                           access_key=access_key,
                           expiration=exp,
                           policy=policy,
                           signature=signature,
                           directory=directory,
                           url_root=request.url_root,
                           portal=portal,
                           ua=True)
Esempio n. 4
0
def main_page(request):
    return jinja.render('ind.html', request, username=utils.get_user(request))
def generate_form():

    try:
        bucket_name = os.environ['BUCKET']
        access_key, secret_key = get_env_creds()
    except:
        return render_template('error.html',
                               message='Error obtaining valid creds: %s'
                                       % str(sys.exc_info()))

    try:
        lc_expiration = request.form['lifecycle']
        lc_expiration = int(lc_expiration)
        assert lc_expiration <= 180
    except:
        return render_template('error.html',
                               message='Invalid Lifecycle Duration')

    try:
        exp = request.form['exp']
        exp = datetime.strptime(exp, '%Y-%m-%d').isoformat() + 'Z'
    except:
        return render_template('error.html',
                               message='Invalid Expiration Date')

    try:
        maxupload = request.form['maxupload']
        max_megs = int(maxupload) / (1024 * 1024)
    except:
        return render_template('error.html',
                               message='Invalid File Size')

    try:
        directory = request.form['directory']
        assert valid_name(directory)
        assert directory != ''
        directory = 'uploads/' + directory
    except:
        return render_template('error.html',
                               message='Invalid/Empty Name for \
                               Customer/Identifier. \'A-Z\', \'0-9\',\
                                \'-\' and \'_\' only.')

    try:
        notes = request.form['notes']
    except:
        notes = ''

    try:
        policy = gen_policy(bucket_name   = bucket_name,
                            expiration    = exp,
                            max_byte_size = maxupload,
                            directory     = directory)
    except:
        return render_template('error.html',
                               message='Error Gen Policy: %s'
                                       % str(sys.exc_info()))

    try:
        signature, policy = sign_policy(policy=policy,
                                        secret=secret_key)
    except:
        return render_template('error.html',
                               message='Error Sign Policy: %s'
                                       % str(sys.exc_info()))

    try:
        env = Environment(loader=FileSystemLoader('templates'))
        template = env.get_template('upload.html')
        html = template.render(bucket_name=bucket_name,
                               access_key=access_key,
                               policy=policy,
                               signature=signature,
                               max_in_megs=max_megs,
                               notes=notes,
                               directory=directory)
    except:
        return render_template('error.html',
                               message='Error rendering template: %s'
                                       % str(sys.exc_info()))

    try:
        create_folder_and_lifecycle(bucket_name=bucket_name,
                                    directory=directory,
                                    expiration=lc_expiration)
    except:
        return render_template('error.html',
                               message='Error setting lifecycle: %s'
                                       % str(sys.exc_info()))

    try:
        url = upload_s3(contents=html,
                        bucket_name=bucket_name)
    except:
        return render_template('error.html',
                               message='Error uploading to s3: %s'
                                       % str(sys.exc_info()))

    if url is None:
        return render_template('error.html',
                               message='Could not generate URL. Check Logs')

    # strip signature from url; we dont need since the form is public
    url = url.split('?')[0]

    logging.info('User [%s] generated a form called %s'
                 % (get_user(request), directory))
    portal = directory[len(PREFIX):]
    return render_template('done.html',
                           url=url,
                           bucket_name=bucket_name,
                           access_key=access_key,
                           expiration=exp,
                           policy=policy,
                           signature=signature,
                           directory=directory,
                           url_root=request.url_root,
                           portal=portal)