<commit_before>define([
    '/file/file-crypto.js',
    '/common/common-hash.js',
    '/bower_components/tweetnacl/nacl-fast.min.js',
], function (FileCrypto, Hash) {
    var Nacl = window.nacl;
    var module = {};

    module.upload = function (file, noStore, common, updateProgress, onComplete, onError, onPending) {
        var u8 = file.blob; // This is not a blob but a uint8array
        var metadata = file.metadata;

        // if it exists, path contains the new pad location in the drive
        var path = file.path;

        var password = file.password;
        var hash = Hash.createRandomHash('file', password);
        var secret = Hash.getSecrets('file', hash, password);
        var key = secret.keys.cryptKey;
        var id = secret.channel;

        // XXX check id here (getFileSize)

        var next = FileCrypto.encrypt(u8, metadata, key);

        var estimate = FileCrypto.computeEncryptedSize(u8.length, metadata);

        var sendChunk = function (box, cb) {
            var enc = Nacl.util.encodeBase64(box);
            common.uploadChunk(enc, function (e, msg) {
                cb(e, msg);
            });
        };

        var actual = 0;
        var again = function (err, box) {
            if (err) { throw new Error(err); }
            if (box) {
                actual += box.length;
                var progressValue = (actual / estimate * 100);
                updateProgress(progressValue);

                return void sendChunk(box, function (e) {
                    if (e) { return console.error(e); }
                    next(again);
                });
            }

            if (actual !== estimate) {
                console.error('Estimated size does not match actual size');
            }

            // if not box then done
            common.uploadComplete(id, function (e) {
                if (e) { return void console.error(e); }
                var uri = ['', 'blob', id.slice(0,2), id].join('/');
                console.log("encrypted blob is now available as %s", uri);

                var href = '/file/#' + hash;

                var title = metadata.name;

                if (noStore) { return void onComplete(href); }

                var data = {
                    title: title || "",
                    href: href,
                    path: path,
                    password: password,
                    channel: id
                };
                common.setPadTitle(data, function (err) {
                    if (err) { return void console.error(err); }
                    onComplete(href);
                    common.setPadAttribute('fileType', metadata.type, null, href);
                });
            });
        };

        common.uploadStatus(estimate, function (e, pending) {
            if (e) {
                console.error(e);
                onError(e);
                return;
            }

            if (pending) {
                return void onPending(function () {
                    // if the user wants to cancel the pending upload to execute that one
                    common.uploadCancel(estimate, function (e) {
                        if (e) {
                            return void console.error(e);
                        }
                        next(again);
                    });
                });
            }
            next(again);
        });
    };
    return module;
});<commit_msg>Make sure the ID is not used when uploading a file<commit_after>define([
    '/file/file-crypto.js',
    '/common/common-hash.js',
    '/bower_components/nthen/index.js',
    '/bower_components/tweetnacl/nacl-fast.min.js',
], function (FileCrypto, Hash, nThen) {
    var Nacl = window.nacl;
    var module = {};

    module.upload = function (file, noStore, common, updateProgress, onComplete, onError, onPending) {
        var u8 = file.blob; // This is not a blob but a uint8array
        var metadata = file.metadata;

        // if it exists, path contains the new pad location in the drive
        var path = file.path;

        var password = file.password;
        var hash, secret, key, id, href;

        var getNewHash = function () {
            hash = Hash.createRandomHash('file', password);
            secret = Hash.getSecrets('file', hash, password);
            key = secret.keys.cryptKey;
            id = secret.channel;
            href = '/file/#' + hash;
        };

        var getValidHash = function (cb) {
            getNewHash();
            common.getFileSize(href, password, function (err, size) {
                if (err || typeof(size) !== "number") {  throw new Error(err || "Invalid size!"); }
                if (size === 0) { return void cb(); }
                getValidHash();
            });
        };

        nThen(function (waitFor) {
            // Generate a hash and check if the resulting id is valid (not already used)
            getValidHash(waitFor());
        }).nThen(function () {
            var next = FileCrypto.encrypt(u8, metadata, key);

            var estimate = FileCrypto.computeEncryptedSize(u8.length, metadata);

            var sendChunk = function (box, cb) {
                var enc = Nacl.util.encodeBase64(box);
                common.uploadChunk(enc, function (e, msg) {
                    cb(e, msg);
                });
            };

            var actual = 0;
            var again = function (err, box) {
                if (err) { throw new Error(err); }
                if (box) {
                    actual += box.length;
                    var progressValue = (actual / estimate * 100);
                    updateProgress(progressValue);

                    return void sendChunk(box, function (e) {
                        if (e) { return console.error(e); }
                        next(again);
                    });
                }

                if (actual !== estimate) {
                    console.error('Estimated size does not match actual size');
                }

                // if not box then done
                common.uploadComplete(id, function (e) {
                    if (e) { return void console.error(e); }
                    var uri = ['', 'blob', id.slice(0,2), id].join('/');
                    console.log("encrypted blob is now available as %s", uri);


                    var title = metadata.name;

                    if (noStore) { return void onComplete(href); }

                    var data = {
                        title: title || "",
                        href: href,
                        path: path,
                        password: password,
                        channel: id
                    };
                    common.setPadTitle(data, function (err) {
                        if (err) { return void console.error(err); }
                        onComplete(href);
                        common.setPadAttribute('fileType', metadata.type, null, href);
                    });
                });
            };

            common.uploadStatus(estimate, function (e, pending) {
                if (e) {
                    console.error(e);
                    onError(e);
                    return;
                }

                if (pending) {
                    return void onPending(function () {
                        // if the user wants to cancel the pending upload to execute that one
                        common.uploadCancel(estimate, function (e) {
                            if (e) {
                                return void console.error(e);
                            }
                            next(again);
                        });
                    });
                }
                next(again);
            });
        });

    };
    return module;
});
{'fraction_non_alphanumeric': 0.10962429233144622, 'fraction_numerical': 0.003216675244467319, 'mean_word_length': 1.2233981693363845, 'pattern_counts': {'":': 0, '<': 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': '4376244', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1875, 'n_words': 537}