示例#1
0
def upload():
    # Get priority
    priority = int(request.form.get('priority', PRIORITY.medium))
    if priority not in PRIORITY.get_values():
        priority = PRIORITY.medium

    # Get output formats
    output_formats = request.form.get('output-formats', '')
    output_formats = list(set(
        filter(
            lambda format: format in app.config['ALLOWED_EXTENSIONS'],
            output_formats.split(';')
        )
    ))
    if not output_formats:
        return jsonify({'Error': 'Must provide valid output formats'}), 400

    # Get file (either directly or via URL)
    file = request.files.get('file')
    allowed_extensions = app.config['ALLOWED_EXTENSIONS']

    if file:
        if allowed_filename(file.filename, allowed_extensions):
            filename = secure_filename(file.filename).strip()[-FILE_NAME_LIMIT]
            local_path = os.path.join(app.config['UPLOAD_FOLDER'],
                                      timestamp_filename(filename))
            file.save(local_path)
        else:
            return jsonify({'Error': 'File format not allowed'}), 400
    else:
        fileURL = request.form.get('fileURL')
        if fileURL:
            filename = get_filename_from_url(fileURL)

            try:
                local_path = download_url(
                    fileURL, app.config['UPLOAD_FOLDER'], timestamp=True)
            except FileAccessDenied as fad:
                return jsonify({
                    'status': 'error',
                    'code': fad.status_code,
                    'message': fad.message
                }), 500

        else:
            return jsonify({'status': 'error',
                            'message': 'Unable to decode uploaded file'}), 500

    # Upload to remote and remove file from local
    remote_destination = os.path.join(app.config['REMOTE_INPUT_FOLDER'],
                                      get_uuid(), filename)
    upload_to_remote(remote_destination, local_path)
    os.remove(local_path)

    # Register the file for conversions and return docIds
    docIds = Conversion.register_file(filename, remote_destination,
                                      g.user, output_formats, priority)

    # Call request fetcher
    request_fetcher.delay()

    return jsonify({'status': STATUS.introduced, 'doc_ids': docIds})