from os import path
from flask import Blueprint, request, jsonify, abort
import config
import oletools.olevba

olevba_bp = Blueprint('olevba', __name__)

@olevba_bp.route('/analyze', methods=['GET'])
def analyze_vba():
    file = request.args.get('file', '')
    if file == '':
        abort(400)
    filepath = path.join(config.Config.FILE_DIRECTORY, file)

    # Analyze with olevba
    vbaparser = oletools.olevba.VBA_Parser(filepath)
    results = vbaparser.analyze_macros()

    return jsonify({'filename': file, 'result': results})
