<commit_before>/**
 * File system connector is a fs facade
 *
 * @module lib/data_connector/file_system_connector
 * @author  <NAME>, <NAME>
 */

/**
 * Callback for error handling.
 *
 * @callback errorCallback
 * @param {Error} error - The error of the search operation.
 */

/**
 * Callback for file and error handling.
 *
 * @callback fileErrorCallback
 * @param {Error} error - The error of the search operation.
 * @param {string} currentFilename - The path plus file name to a file.
 */

/**
 * Callback for file handling.
 *
 * @callback fileCallback
 * @param {string} currentFilename - The path plus file name to a file.
 */

'use strict';

var path = require('path');
var fs = require('fs-extra');
var asyncLib = require('async');
var config = require('../config/config');
var PromiseLib = require("promise");

var fileSystemConnector = {};

/**
 * Saves the article content in the file. The content wraps the actual text in a HTML body and puts the title in the HTML head. If the article is temporary the files get copied from the temp folder to the perm folder.
 *
 * @param {string| Object} articleId
 * @param {string} content - text of the article as html
 * @param {boolean} isTemporary - Is true if the article is still temporary
 * @returns {Promise<module:lib/data_connector/models~ArticleSchema|Error>}
 */
fileSystemConnector.saveContent = function (articleId, content, isTemporary) {

    var pathContainer = new PathContainer(articleId);

    return new PromiseLib(function (resolve, reject) {
        asyncLib.series([pathContainer.loadPaths.bind(pathContainer),
            function (cb) {
                fs.exists(pathContainer.fullNameOfOldArticleContentFile, function (exists) {
                    if (exists) {
                        return fs.remove(pathContainer.fullNameOfOldArticleContentFile, cb);
                    }

                    cb();
                });
            },
            function (cb) {
                fs.exists(pathContainer.fullNameOfCurrentArticleContentFile, function (exists) {
                    if (exists) {
                        return fs.move(pathContainer.fullNameOfCurrentArticleContentFile, pathContainer.fullNameOfOldArticleContentFile, cb);
                    }

                    cb();
                });
            },
            function (cb) {
                if (!isTemporary) {
                    return cb();
                }
                fs.remove(pathContainer.permFolderPath, function () {
                    fs.move(pathContainer.tempFolderPath, pathContainer.permFolderPath, cb);
                });
            },
            function (cb) {
                fs.writeFile(pathContainer.fullNameOfCurrentArticleContentFile, content, cb);
            }
        ], function (err) {
            if (err) {
                return reject(err);
            }
            resolve(err);
        });

    });
};

/**
 * "readArticleContent" reads the content file of the article with the given ID and returns the extracted body content
 *
 * @function readArticleContent
 * @static
 *
 * @param {string} articleId
 * @returns {Promise<String|Error>}
 */
fileSystemConnector.readArticleContent = function (articleId) {
    articleId += '';
    var pathContainer = new PathContainer(articleId);
    return new PromiseLib(function (resolve, reject) {
        pathContainer.loadPaths(function (err, permFolder) {
            var contentFilePath = pathContainer.fullNameOfCurrentArticleContentFile;
            fs.readFile(contentFilePath, function (err, contentBuffer) {
                if (err) {
                    return reject(err);
                }
                resolve(fileSystemConnector.extractHTMLBodyContent(contentBuffer.toString()));
            });
        });
    });
};

/**
 * Reads the archived (old) version of the content of the article with the given ID. The result contains the content extracted from the HTML.
 *
 * @function readOldArticleContentAndTitle
 * @static
 *
 * @param {string} articleId
 * @returns {Promise<module:lib/search_engine_connector~ArticleSchema|Error>}
 */
fileSystemConnector.readOldArticleContentAndTitle = function (articleId) {
    articleId += '';
    return new PromiseLib(function (resolve, reject) {
        getOldFolderForArticle(articleId, function (err, oldFolder) {
            var contentFilePath = path.join(oldFolder, config.articleContentFileName);
            fs.readFile(contentFilePath, function (err, contentBuffer) {<commit_msg>fixed old version bug<commit_after>/**
 * File system connector is a fs facade
 *
 * @module lib/data_connector/file_system_connector
 * @author  <NAME>, <NAME>
 */

/**
 * Callback for error handling.
 *
 * @callback errorCallback
 * @param {Error} error - The error of the search operation.
 */

/**
 * Callback for file and error handling.
 *
 * @callback fileErrorCallback
 * @param {Error} error - The error of the search operation.
 * @param {string} currentFilename - The path plus file name to a file.
 */

/**
 * Callback for file handling.
 *
 * @callback fileCallback
 * @param {string} currentFilename - The path plus file name to a file.
 */

'use strict';

var path = require('path');
var fs = require('fs-extra');
var asyncLib = require('async');
var config = require('../config/config');
var PromiseLib = require("promise");

var fileSystemConnector = {};

/**
 * Saves the article content in the file. The content wraps the actual text in a HTML body and puts the title in the HTML head. If the article is temporary the files get copied from the temp folder to the perm folder.
 *
 * @param {string| Object} articleId
 * @param {string} content - text of the article as html
 * @param {boolean} isTemporary - Is true if the article is still temporary
 * @returns {Promise<module:lib/data_connector/models~ArticleSchema|Error>}
 */
fileSystemConnector.saveContent = function (articleId, content, isTemporary) {

    var pathContainer = new PathContainer(articleId);

    return new PromiseLib(function (resolve, reject) {
        asyncLib.series([pathContainer.loadPaths.bind(pathContainer),
            function (cb) {
                fs.exists(pathContainer.fullNameOfOldArticleContentFile, function (exists) {
                    if (exists) {
                        return fs.remove(pathContainer.fullNameOfOldArticleContentFile, cb);
                    }

                    cb();
                });
            },
            function (cb) {
                fs.exists(pathContainer.fullNameOfCurrentArticleContentFile, function (exists) {
                    if (exists) {
                        return fs.move(pathContainer.fullNameOfCurrentArticleContentFile, pathContainer.fullNameOfOldArticleContentFile, cb);
                    }

                    cb();
                });
            },
            function (cb) {
                if (!isTemporary) {
                    return cb();
                }
                fs.remove(pathContainer.permFolderPath, function () {
                    fs.move(pathContainer.tempFolderPath, pathContainer.permFolderPath, cb);
                });
            },
            function (cb) {
                fs.writeFile(pathContainer.fullNameOfCurrentArticleContentFile, content, cb);
            }
        ], function (err) {
            if (err) {
                return reject(err);
            }
            resolve(err);
        });

    });
};

/**
 * "readArticleContent" reads the content file of the article with the given ID and returns the extracted body content
 *
 * @function readArticleContent
 * @static
 *
 * @param {string} articleId
 * @returns {Promise<String|Error>}
 */
fileSystemConnector.readArticleContent = function (articleId) {
    articleId += '';
    var pathContainer = new PathContainer(articleId);
    return new PromiseLib(function (resolve, reject) {
        pathContainer.loadPaths(function (err, permFolder) {
            var contentFilePath = pathContainer.fullNameOfCurrentArticleContentFile;
            fs.readFile(contentFilePath, function (err, contentBuffer) {
                if (err) {
                    return reject(err);
                }
                resolve(fileSystemConnector.extractHTMLBodyContent(contentBuffer.toString()));
            });
        });
    });
};

/**
 * Reads the archived (old) version of the content of the article with the given ID. The result contains the content extracted from the HTML.
 *
 * @function readOldArticleContentAndTitle
 * @static
 *
 * @param {string} articleId
 * @returns {Promise<module:lib/search_engine_connector~ArticleSchema|Error>}
 */
fileSystemConnector.readOldArticleContentAndTitle = function (articleId) {
    articleId += '';
    return new PromiseLib(function (resolve, reject) {
        getOldFolderForArticle(articleId, function (err, oldFolder) {
            var contentFilePath = path.join(oldFolder, articleId + config.articleContentFileName);
            fs.readFile(contentFilePath, function (err, contentBuffer) {
{'fraction_non_alphanumeric': 0.10057790620137809, 'fraction_numerical': 0.0, 'mean_word_length': 2.4852827265685513, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7486713', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2246, 'n_words': 660}