<commit_before>var $ = require('jquery');
var $osf = require('js/osfHelpers');
var ctx = window.contextVars;
var Range = ace.require('ace/range').Range;

var getExtension = function(filename) {
    return /(?:\.([^.]+))?$/.exec(filename)[1];
};
var validImgExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
var imageFolder = 'Wiki images';

var autoIncrementFileName = function(name, nameList) {
    var num = 1;
    var newName;
    var ext = getExtension(name);
    var baseName = name.replace('.' + ext, '');

    rename:
    while (true) {
        for (var i = 0; i < nameList.length; i++) {
            newName = baseName + '(' + num + ').' + ext;
            if (nameList[i] === newName) {
                num += 1;
                newName = baseName + '(' + num + ').' + ext;
                continue rename;
            }
        }
        break;
    }
    return newName;
};


var localFileHandler = function(files, cm, init, fixupInputArea) {
    var multiple = files.length > 1;
    var urls = [];
    var name;
    var fileNames = [];
    var ext;
    var num = cm.addLinkDef(init) + 1;
    var promises = [];
    var editor = ace.edit('editor');
    editor.disable();
    getOrCreateWikiImagesFolder().fail(function(response) {
        notUploaded(response, multiple);
        editor.enable();
    }).done(function(path) {
        $.ajax({ // Check to makes sure we don't overwrite a file with the same name.
            url: ctx.waterbutlerURL + 'v1/resources/' + ctx.node.id + '/providers/osfstorage' + encodeURI(path) + '?meta=',
            beforeSend: $osf.setXHRAuthorization,
        }).done(function (response) {
            fileNames = response.data.map(function(file) {
                return file.attributes.name;
            });
            if (path) {
                var newName;
                $.each(files, function (i, file) {
                    if (fileNames.indexOf(file.name) !== -1) {
                        newName = autoIncrementFileName(file.name, fileNames);
                    }
                    ext = getExtension(file.name);
                    name = newName ? newName : file.name;
                    if (validImgExtensions.indexOf(ext.toLowerCase()) <= -1) {
                        $osf.growl('Error', 'This file type cannot be embedded  (' + file.name + ')', 'danger');
                        editor.enable();
                    } else {
                        var waterbutlerURL = ctx.waterbutlerURL + 'v1/resources/' + ctx.node.id + '/providers/osfstorage' + encodeURI(path) + '?name=' + encodeURI(name) + '&type=file';
                        $osf.trackClick('wiki', 'dropped-image', ctx.node.id);
                        promises.push(
                            $.ajax({
                                url: waterbutlerURL,
                                type: 'PUT',
                                processData: false,
                                contentType: false,
                                beforeSend: $osf.setXHRAuthorization,
                                data: file,
                            }).done(function (response) {
                                urls.splice(i, 0, response.data.links.download + '?mode=render');
                            }).fail(function (response) {
                                notUploaded(response, false, cm, init, fixupInputArea, path, file);
                            })
                        );
                    }
                });
                $.when.apply(null, promises).done(function () {
                    $.each(urls, function (i, url) {
                        cm.doLinkOrImage(init, null, true, url, multiple, num + i);
                    });
                    fixupInputArea();
                    editor.enable();
                });
            } else {
                notUploaded(null, multiple);
            }
        }).fail(function (response) {<commit_msg>Loading indicator DnD fix (#8244)<commit_after>var $ = require('jquery');
var $osf = require('js/osfHelpers');
var ctx = window.contextVars;
var Range = ace.require('ace/range').Range;

var getExtension = function(filename) {
    return /(?:\.([^.]+))?$/.exec(filename)[1];
};
var validImgExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
var imageFolder = 'Wiki images';

var autoIncrementFileName = function(name, nameList) {
    var num = 1;
    var newName;
    var ext = getExtension(name);
    var baseName = name.replace('.' + ext, '');

    rename:
    while (true) {
        for (var i = 0; i < nameList.length; i++) {
            newName = baseName + '(' + num + ').' + ext;
            if (nameList[i] === newName) {
                num += 1;
                newName = baseName + '(' + num + ').' + ext;
                continue rename;
            }
        }
        break;
    }
    return newName;
};


var localFileHandler = function(files, cm, init, fixupInputArea) {
    var multiple = files.length > 1;
    var urls = [];
    var name;
    var fileNames = [];
    var ext;
    var num = cm.addLinkDef(init) + 1;
    var promises = [];
    var editor = ace.edit('editor');
    editor.disable();
    getOrCreateWikiImagesFolder().fail(function(response) {
        notUploaded(response, multiple);
        editor.enable();
    }).done(function(path) {
        $.ajax({ // Check to makes sure we don't overwrite a file with the same name.
            url: ctx.waterbutlerURL + 'v1/resources/' + ctx.node.id + '/providers/osfstorage' + encodeURI(path) + '?meta=',
            beforeSend: $osf.setXHRAuthorization,
        }).done(function (response) {
            fileNames = response.data.map(function(file) {
                return file.attributes.name;
            });
            if (path) {
                var newName;
                $.each(files, function (i, file) {
                    if (fileNames.indexOf(file.name) !== -1) {
                        newName = autoIncrementFileName(file.name, fileNames);
                    }
                    ext = getExtension(file.name);
                    name = newName ? newName : file.name;
                    if (validImgExtensions.indexOf(ext.toLowerCase()) <= -1) {
                        $osf.growl('Error', 'This file type cannot be embedded  (' + file.name + ')', 'danger');
                    } else {
                        var waterbutlerURL = ctx.waterbutlerURL + 'v1/resources/' + ctx.node.id + '/providers/osfstorage' + encodeURI(path) + '?name=' + encodeURI(name) + '&type=file';
                        $osf.trackClick('wiki', 'dropped-image', ctx.node.id);
                        promises.push(
                            $.ajax({
                                url: waterbutlerURL,
                                type: 'PUT',
                                processData: false,
                                contentType: false,
                                beforeSend: $osf.setXHRAuthorization,
                                data: file,
                            }).done(function (response) {
                                urls.splice(i, 0, response.data.links.download + '?mode=render');
                            }).fail(function (response) {
                                notUploaded(response, false, cm, init, fixupInputArea, path, file);
                            })
                        );
                    }
                });
                $.when.apply(null, promises).done(function () {
                    $.each(urls, function (i, url) {
                        cm.doLinkOrImage(init, null, true, url, multiple, num + i);
                    });
                    fixupInputArea();
                    editor.enable();
                });
            } else {
                notUploaded(null, multiple);
            }
        }).fail(function (response) {
{'fraction_non_alphanumeric': 0.1359385009609225, 'fraction_numerical': 0.0033311979500320306, 'mean_word_length': 1.412982998454405, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2111944', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2082, 'n_words': 463}