<commit_before>if (Meteor.isServer) {
    domain = Npm.require('domain');
    fs = Npm.require('fs');
    Future = Npm.require('fibers/future');
    mkdirp = Npm.require('mkdirp');
    stream = Npm.require('stream');
    zlib = Npm.require('zlib');


    // Create the temporary upload dir
    Meteor.startup(function () {
        createTempDir();
    });

    Meteor.methods({
        /**
         * Completes the file transfer
         * @param fileId
         * @param storeName
         */
        ufsComplete: function (fileId, storeName) {
            check(fileId, String);
            check(storeName, String);

            var store = UploadFS.getStore(storeName);

            // Check arguments
            if (!store) {
                throw new Meteor.Error(404, 'store "' + storeName + '" does not exist');
            }

            // Check that file exists and is owned by current user
            if (store.getCollection().find({_id: fileId, userId: this.userId}).count() < 1) {
                throw new Meteor.Error(404, 'file "' + fileId + '" does not exist');
            }

            var fut = new Future();
            var tmpFile = UploadFS.getTempFilePath(fileId);

            // Get the temporary file
            var readStream = fs.createReadStream(tmpFile, {
                flags: 'r',
                encoding: null,
                autoClose: true
            });

            // Save the file in the store
            store.write(readStream, fileId, function (err, file) {
                if (err) {
                    // Delete the temporary file
                    Meteor.setTimeout(function () {
                        fs.unlink(tmpFile);
                    }, 500);
                    fut.throw(err);
                } else {
                    fut.return(file);
                }
            });

            return fut.wait();
        },

        /**
         * Saves a chunk of file
         * @param chunk
         * @param fileId
         * @param storeName
         * @return {*}
         */
        ufsWrite: function (chunk, fileId, storeName) {
            check(fileId, String);
            check(storeName, String);

            // Check arguments
            if (!(chunk instanceof Uint8Array)) {
                throw new Meteor.Error(400, 'chunk is not an Uint8Array');
            }
            if (chunk.length <= 0) {
                throw new Meteor.Error(400, 'chunk is empty');
            }

            var store = UploadFS.getStore(storeName);
            if (!store) {
                throw new Meteor.Error(404, 'store ' + storeName + ' does not exist');
            }
<commit_msg>Remove temp file after successful Store write and not only in case of error<commit_after>if (Meteor.isServer) {
    domain = Npm.require('domain');
    fs = Npm.require('fs');
    Future = Npm.require('fibers/future');
    mkdirp = Npm.require('mkdirp');
    stream = Npm.require('stream');
    zlib = Npm.require('zlib');


    // Create the temporary upload dir
    Meteor.startup(function () {
        createTempDir();
    });

    Meteor.methods({
        /**
         * Completes the file transfer
         * @param fileId
         * @param storeName
         */
        ufsComplete: function (fileId, storeName) {
            check(fileId, String);
            check(storeName, String);

            var store = UploadFS.getStore(storeName);

            // Check arguments
            if (!store) {
                throw new Meteor.Error(404, 'store "' + storeName + '" does not exist');
            }

            // Check that file exists and is owned by current user
            if (store.getCollection().find({_id: fileId, userId: this.userId}).count() < 1) {
                throw new Meteor.Error(404, 'file "' + fileId + '" does not exist');
            }

            var fut = new Future();
            var tmpFile = UploadFS.getTempFilePath(fileId);

            // Get the temporary file
            var readStream = fs.createReadStream(tmpFile, {
                flags: 'r',
                encoding: null,
                autoClose: true
            });

            // Save the file in the store
            store.write(readStream, fileId, function (err, file) {
                if (err) {
                    // Delete the temporary file
                    Meteor.setTimeout(function () {
                        fs.unlink(tmpFile);
                    }, 500);
                    fut.throw(err);
                } else {
                    fut.return(file);
                    fs.unlink(tmpFile);
                }
            });

            return fut.wait();
        },

        /**
         * Saves a chunk of file
         * @param chunk
         * @param fileId
         * @param storeName
         * @return {*}
         */
        ufsWrite: function (chunk, fileId, storeName) {
            check(fileId, String);
            check(storeName, String);

            // Check arguments
            if (!(chunk instanceof Uint8Array)) {
                throw new Meteor.Error(400, 'chunk is not an Uint8Array');
            }
            if (chunk.length <= 0) {
                throw new Meteor.Error(400, 'chunk is empty');
            }

            var store = UploadFS.getStore(storeName);
            if (!store) {
                throw new Meteor.Error(404, 'store ' + storeName + ' does not exist');
            }

{'fraction_non_alphanumeric': 0.11353550295857988, 'fraction_numerical': 0.008136094674556213, 'mean_word_length': 1.3620087336244542, 'pattern_counts': {'":': 0, '<': 7, '<?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': '600294', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1391, 'n_words': 372}