<commit_before># -*- coding: utf-8 -*-

## a simple controller to handle AJAX uploads of study supporting files
## adapted from http://in10min.blogspot.com/2013/04/web2py-implement-multiple-files-upload.html

def upload_file():
    """
    File upload handler for the AJAX form of the plugin jquery-file-upload
    Return the response in JSON required by the plugin
    """
    response.view = 'generic.json'
    try:
        # Get the file from the form
        f = request.vars['files[]']
         
        # Store file
        id = db.supporting_files.insert(doc = db.supporting_files.doc.store(f.file, f.filename))
         
        # Compute size of the file and update the record
        import shutil
        record = db.supporting_files[id]
         
        path_list = []
        path_list.append(request.folder)
        path_list.append('uploads')
        path_list.append(record['doc'])
        size = shutil.os.path.getsize(shutil.os.path.join(*path_list))
         
        File = db(db.supporting_files.id==id).select()[0]
        db.supporting_files[id] = dict(file_size=size)
        db.supporting_files[id] = dict(study_id=response.study_id)
         
        res = dict(files=[{"name": str(f.filename), "size": size, "url": URL(f='download', args=[File['doc']]), "delete_url": URL(f='delete_file', args=[File['doc']])}])
         
        import gluon
        ## print(">>>>>>>")
        ## print(gluon.contrib.simplejson.dumps(res, separators=(',',':')))
        ## print("<<<<<<<")
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    except:
        return dict(message=T('Upload error'))


def download():
    return response.download(request, db)
    # this in turn calls response.stream() for large files
<commit_msg>Removing unused shutils; general cleanup.<commit_after># -*- coding: utf-8 -*-

## a simple controller to handle AJAX uploads of study supporting files
## adapted from http://in10min.blogspot.com/2013/04/web2py-implement-multiple-files-upload.html

def upload_file():
    """
    File upload handler for the AJAX form of the plugin jquery-file-upload
    Return the response in JSON required by the plugin
    """
    import os
    import gluon
    response.view = 'generic.json'
    try:
        # Get the file from the form
        f = request.vars['files[]']
         
        # Store file
        id = db.supporting_files.insert(doc = db.supporting_files.doc.store(f.file, f.filename))
         
        # Compute size of the file and update the record
        record = db.supporting_files[id]
         
        path_list = []
        path_list.append(request.folder)
        path_list.append('uploads')
        path_list.append(record['doc'])
        size = os.path.getsize(os.path.join(*path_list))
         
        File = db(db.supporting_files.id==id).select()[0]
        db.supporting_files[id] = dict(file_size=size)
        db.supporting_files[id] = dict(study_id=response.study_id)
         
        res = dict(files=[{"name": str(f.filename), "size": size, "url": URL(f='download', args=[File['doc']]), "delete_url": URL(f='delete_file', args=[File['doc']])}])
         
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))

    except:
        return dict(message=T('Upload error'))


def download():
    return response.download(request, db)
    # this in turn calls response.stream() for large files

{'fraction_non_alphanumeric': 0.14277320799059928, 'fraction_numerical': 0.006462984723854289, 'mean_word_length': 2.7335526315789473, 'pattern_counts': {'":': 8, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2751172', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1021, 'n_words': 236}