<commit_before>from flask import Flask, render_template, redirect, url_for
from flask import request, jsonify, send_file
from werkzeug import secure_filename
from os import path, system, remove
from multiprocessing import Process
from hashlib import md5
import time

""" инициализируем flask """
app = Flask(__name__, static_url_path='')
""" настраиваем папку для загрузки файлов """
app.config['UPLOAD_FOLDER'] = path.join(path.dirname(path.realpath(__file__)), 'upload')

def get_hash(text):
    return md5(text.encode('utf-8')).hexdigest()

""" обрабатываем файл """
def gs_shakalizing(filename, new_name, dpi):
    old_file = path.join(app.config['UPLOAD_FOLDER'], filename)
    new_file = path.join(app.config['UPLOAD_FOLDER'], new_name)
    cmd = """/usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/ebook -dNOPAUSE \
             -dQUIET -dBATCH -dColorImageResolution={dpi} -sOutputFile='{output}' '{input}'\
          """.format(dpi=dpi, output=new_file, input=old_file)
    system(cmd)
    remove(old_file)

""" проверяем статус обработки """
@app.route('/process', methods=['POST', 'GET'])
def process():
    if request.method == 'POST':
        if request.json:
            filename = request.json.get('code')
            file_path = path.join(app.config['UPLOAD_FOLDER'], filename)
            if path.isfile(file_path):
                return jsonify({"status": "founded"})
            else:
                return jsonify({"status": "not found"})
        else:
            return jsonify({"status": "error"})
    else:
        filename = request.values.get('code')
        if filename:
            file_path = path.join(app.config['UPLOAD_FOLDER'], filename)
            return send_file(file_path, mimetype='application/pdf')
        else:
            return 404

""" загрузка файла и страница с ожиданием """
@app.route('/shakalazing', methods=['POST', 'GET'])
def shakalazing():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(path.join(app.config['UPLOAD_FOLDER'], filename))
            dpi = request.form.get('dpi')
            hash_code = get_hash(filename)
            sh_thread = Process(target=gs_shakalizing, args=(filename, hash_code, dpi))
            sh_thread.start()<commit_msg>[fix] send file as attachment<commit_after>from flask import Flask, render_template, redirect, url_for
from flask import request, jsonify, send_file
from werkzeug import secure_filename
from os import path, system, remove
from multiprocessing import Process
from hashlib import md5
import time

""" инициализируем flask """
app = Flask(__name__, static_url_path='')
""" настраиваем папку для загрузки файлов """
app.config['UPLOAD_FOLDER'] = path.join(path.dirname(path.realpath(__file__)), 'upload')

def get_hash(text):
    return md5(text.encode('utf-8')).hexdigest()

""" обрабатываем файл """
def gs_shakalizing(filename, new_name, dpi):
    old_file = path.join(app.config['UPLOAD_FOLDER'], filename)
    new_file = path.join(app.config['UPLOAD_FOLDER'], new_name)
    cmd = """/usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/ebook -dNOPAUSE \
             -dQUIET -dBATCH -dColorImageResolution={dpi} -sOutputFile='{output}' '{input}'\
          """.format(dpi=dpi, output=new_file, input=old_file)
    system(cmd)
    remove(old_file)

""" проверяем статус обработки """
@app.route('/process', methods=['POST', 'GET'])
def process():
    if request.method == 'POST':
        if request.json:
            filename = request.json.get('code')
            file_path = path.join(app.config['UPLOAD_FOLDER'], filename)
            if path.isfile(file_path):
                return jsonify({"status": "founded"})
            else:
                return jsonify({"status": "not found"})
        else:
            return jsonify({"status": "error"})
    else:
        filename = request.values.get('code')
        if filename:
            file_path = path.join(app.config['UPLOAD_FOLDER'], filename)
            return send_file(file_path, mimetype='application/pdf', 
                as_attachment=True, attachment_filename=filename + '.pdf')
        else:
            return 404

""" загрузка файла и страница с ожиданием """
@app.route('/shakalazing', methods=['POST', 'GET'])
def shakalazing():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(path.join(app.config['UPLOAD_FOLDER'], filename))
            dpi = request.form.get('dpi')
            hash_code = get_hash(filename)
            sh_thread = Process(target=gs_shakalizing, args=(filename, hash_code, dpi))
            sh_thread.start()
{'fraction_non_alphanumeric': 0.13933054393305438, 'fraction_numerical': 0.0033472803347280333, 'mean_word_length': 3.2123348017621147, 'pattern_counts': {'":': 6, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6750642', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1568, 'n_words': 318}