예제 #1
0
파일: admin.py 프로젝트: smajida/predator-2
def file_upload():
    error = None
    success = None
    if request.method == 'POST':
        try:
            uploaded = request.files['file']
            if not uploaded:
                flash('file not supplied', 'error')
                return render_template('file_upload.html', error=error)
                
            invalid = ''
            processed = ''
            for item in request.files.listvalues():
                for fileobj in item:
                    filename = fileobj.filename
                    if not is_valid_file(filename):
                        invalid += filename + ', '
                        continue
                    
                    audit = Audit()
                    audit.update(dict(
                                username = session['username'],
                                type = 'upload',
                                doc = filename))
                    audit.save()
                    
                    filepath = '/tmp/%s' % filename
                    content = fileobj.read()
                    with open(filepath, 'w') as stream:
                        stream.write(content)
                    
                    s = agent_req_dispatcher()
                    s.send_json(dict(pdf_path = filepath, do_what = 'archive'))
                    processed += filename + ', '
            
            if invalid:
                invalid = invalid.rstrip(', ')
                flash('Invalid: { %s }' % invalid, 'error')
            if processed:
                processed = processed.rstrip(', ')
                flash('Processing : { %s }' % processed, 'info')
        except werkzeug.exceptions.RequestEntityTooLarge, e:
            logger.warn('Maximum file size exceeded while uploading file')
            flash('File size exceeded the maximum upload limit(50MB).')
예제 #2
0
파일: cop.py 프로젝트: smajida/predator-2
def check():
    result = None
    match = False
    if request.method == 'POST':
        try:
            uploaded = request.files.get('file')
            error = """<div id="match" style="display:none;"></div>
                        <div class="alert-message error fade in">
                        <a class="close" href="#">x</a>
                        <p>"""
            if not uploaded:
                return error + "File not supplied." + "</p></div>"
                
            filename = uploaded.filename
            if not is_valid_file(filename):
                return error + "Not a valid file type. Please use file in pdf format." \
                                + "</p></div>"
                
            audit = Audit()
            audit.update(dict(
                        username = session['username'],
                        type = 'check',
                        doc = filename))
            audit.save()
                
            filepath = '/tmp/%s' % filename
            content = uploaded.read()
            with open(filepath, 'w') as stream:
                stream.write(content)
            
    
            s = agent_req_dispatcher()
            content = '<div id="match" style="display:none;"></div>'
    
            s.send_json(dict(pdf_path = filepath, do_what = 'check'))
            response = s.recv_multipart()
            result = json.loads(response[0])
            logging.warn('Webserver: received result; result=%s' % result)
        
            if result['match']['per_match']:
                labels, label_scale, threshold = get_user_prefs()
                stat = label_scale[int(result['match']['total_match']) - 1]
                
                content += """<div class="match_overview"><span class="desc"
                            style="margin-right: 15px;">
                            Results </span><span class="group">
                            <span class="match_percentage">
                            <span class="key">Percentage Match =</span>
                            <strong>%d</strong> %%</span><span class="divider">|
                            </span><span class="match_documents"><span class="key">
                            Matching Documents = </span><strong>%d</strong>
                            <span class="divider">|</span><span class="match_label">
                            <span class="key">Label =</span>
                            <strong>%s</strong></span></span></div>
                            <table class='zebra-striped bordered-table'>
                            <thead><tr><th>#</th><th>File
                            </th><th>Match</th></tr>
                            </thead><tbody>""" % (result['match']['total_match'], \
                                            len(result['match']['per_match']), stat)
                for i, row in enumerate(result['match']['per_match']):
                    above_threshold, row = filter_row(row, labels, label_scale, threshold)
                    if above_threshold:
                        match = True
                        content += """<tr><td>%s</td><td>%s<span style='background: #%s;
                                    color: #FFF; border-radius: 2px; margin-left: 60px;
                                    padding: 2px 6px 2px 6px;'>%s</span></td>
                                    <td>%d %%</td></tr>""" % (i+1, row[0], row[3].lstrip('#'), row[2], row[1])
                            
                content += '</tbody></table>'
                if not match:
                    content = """<div id="match" style="display:none;"></div>
                                <div class="alert-message success fade in">
                                <a class="close" href="#">x</a>
                                <p>No match found.</p></div>
                                """
            else:
                content += """<div class="alert-message success fade in">
                            <a class="close" href="#">x</a>
                            <p>No match found.</p></div>
                            """
            
            return content
        
        except werkzeug.exceptions.RequestEntityTooLarge, e:
            logger.warn('Maximum file size exceeded while uploading file')
            flash('File size exceeded the maximum upload limit(50MB).')