<commit_before>from flask import Blueprint, render_template, abort, request, redirect, session, url_for, send_file
from flask.ext.login import current_user, login_user, logout_user
from sqlalchemy import desc, or_, and_
from srht.objects import *
from srht.common import *
from srht.config import _cfg
from srht.email import send_invite, send_rejection

from datetime import datetime
import hashlib
import binascii
import os
import zipfile
import urllib
import re
import json
import locale
import shlex
import math

encoding = locale.getdefaultlocale()[1]
api = Blueprint('api', __name__, template_folder='../../templates')

@api.route("/api/approve/<id>", methods=["POST"])
@adminrequired
@with_session
@json_output
def approve(id):
    u = User.query.filter(User.id == id).first()
    u.approved = True
    u.approvalDate = datetime.now()
    db.commit()
    send_invite(u)
    return { "success": True }

@api.route("/api/reject/<id>", methods=["POST"])
@adminrequired
@with_session
@json_output
def reject(id):
    u = User.query.filter(User.id == id).first()
    u.rejected = True
    db.commit()
    send_rejection(u)
    return { "success": True }

@api.route("/api/resetkey", methods=["POST"])
@json_output
def reset_key():
    key = request.form.get('key')
    if not key:
        return { "error": "Maybe you should include the actual key, dumbass" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    user.generate_api_key()
    db.commit()
    return { "key": user.apiKey }

@api.route("/api/upload", methods=["POST"])
@json_output
def upload():
    key = request.form.get('key')
    f = request.files.get('file')
    if not key:
        return { "error": "API key is required" }, 401
    if not f:
        return { "error": "File is required" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    filename = ''.join(c for c in f.filename if c.isalnum() or c == '.')
    upload = Upload()
    upload.user = user
    upload.hash = get_hash(f)
    existing = Upload.query.filter(Upload.hash == upload.hash).first()
    if existing:
        return {
            "success": True,
            "hash": existing.hash,
            "shorthash": existing.shorthash,
            "url": file_link(existing.path)
        }
    len = 4
    shorthash = upload.hash[:len]
    while any(Upload.query.filter(Upload.shorthash == shorthash)):
        len += 1
        shorthash = upload.hash[:len]
    upload.shorthash = shorthash
    upload.path = os.path.join(upload.shorthash + "." + extension(filename))
    upload.original_name = f.filename

    f.seek(0)
    f.save(os.path.join(_cfg("storage"), upload.path))

    db.add(upload)
    db.commit()
    return {
        "success": True,
        "hash": upload.hash,
        "shorthash": upload.shorthash,
        "url": _cfg("protocol") + "://" + _cfg("domain") + "/" + upload.path
    }

@api.route("/api/disown", methods=["POST"])
@json_output
def disown():
    key = request.form.get('key')
    filename = request.form.get('filename')
    if not key:
        return { "error": "API key is required" }, 401
    if not filename:
        return { "error": "File is required" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    Upload.query.filter_by(path=filename).first().hidden = True
    db.commit()
    return {
            "success": True,
            "filename": filename
    }

def get_hash(f):<commit_msg>Deal with null shorthash<commit_after>from flask import Blueprint, render_template, abort, request, redirect, session, url_for, send_file
from flask.ext.login import current_user, login_user, logout_user
from sqlalchemy import desc, or_, and_
from srht.objects import *
from srht.common import *
from srht.config import _cfg
from srht.email import send_invite, send_rejection

from datetime import datetime
import hashlib
import binascii
import os
import zipfile
import urllib
import re
import json
import locale
import shlex
import math

encoding = locale.getdefaultlocale()[1]
api = Blueprint('api', __name__, template_folder='../../templates')

@api.route("/api/approve/<id>", methods=["POST"])
@adminrequired
@with_session
@json_output
def approve(id):
    u = User.query.filter(User.id == id).first()
    u.approved = True
    u.approvalDate = datetime.now()
    db.commit()
    send_invite(u)
    return { "success": True }

@api.route("/api/reject/<id>", methods=["POST"])
@adminrequired
@with_session
@json_output
def reject(id):
    u = User.query.filter(User.id == id).first()
    u.rejected = True
    db.commit()
    send_rejection(u)
    return { "success": True }

@api.route("/api/resetkey", methods=["POST"])
@json_output
def reset_key():
    key = request.form.get('key')
    if not key:
        return { "error": "Maybe you should include the actual key, dumbass" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    user.generate_api_key()
    db.commit()
    return { "key": user.apiKey }

@api.route("/api/upload", methods=["POST"])
@json_output
def upload():
    key = request.form.get('key')
    f = request.files.get('file')
    if not key:
        return { "error": "API key is required" }, 401
    if not f:
        return { "error": "File is required" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    filename = ''.join(c for c in f.filename if c.isalnum() or c == '.')
    upload = Upload()
    upload.user = user
    upload.hash = get_hash(f)
    existing = Upload.query.filter(Upload.hash == upload.hash).first()
    if existing:
        return {
            "success": True,
            "hash": existing.hash,
            "shorthash": existing.shorthash,
            "url": file_link(existing.path)
        }
    len = 4
    shorthash = upload.hash[:len]
    while any(Upload.query.filter(Upload.shorthash == shorthash)):
        len += 1
        shorthash = upload.hash[:len]
    upload.shorthash = shorthash
    upload.path = os.path.join(upload.shorthash + "." + extension(filename))
    upload.original_name = f.filename

    f.seek(0)
    f.save(os.path.join(_cfg("storage"), upload.path))

    if not upload.shorthash:
        return {
            "success": False,
            "error": "Upload interrupted"
        }

    db.add(upload)
    db.commit()
    return {
        "success": True,
        "hash": upload.hash,
        "shorthash": upload.shorthash,
        "url": _cfg("protocol") + "://" + _cfg("domain") + "/" + upload.path
    }

@api.route("/api/disown", methods=["POST"])
@json_output
def disown():
    key = request.form.get('key')
    filename = request.form.get('filename')
    if not key:
        return { "error": "API key is required" }, 401
    if not filename:
        return { "error": "File is required" }, 400
    user = User.query.filter(User.apiKey == key).first()
    if not user:
        return { "error": "API key not recognized" }, 403
    Upload.query.filter_by(path=filename).first().hidden = True
    db.commit()
    return {
            "success": True,
            "filename": filename
    }

def get_hash(f):
{'fraction_non_alphanumeric': 0.1469713358572201, 'fraction_numerical': 0.0075716603569497025, 'mean_word_length': 2.949279231179925, 'pattern_counts': {'":': 46, '<': 7, '<?xml version=': 0, '>': 7, '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': '4475410', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2342, 'n_words': 609}