<commit_before>import fs from 'fs';
import path from 'path';

import _ from 'underscore';
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { SyncedCron } from 'meteor/littledata:synced-cron';
import archiver from 'archiver';

import { settings } from '../../settings';
import { Subscriptions, Rooms, Users, Uploads, Messages, UserDataFiles, ExportOperations, Avatars } from '../../models';
import { FileUpload } from '../../file-upload';
import * as Mailer from '../../mailer';

let zipFolder = '/tmp/zipFiles';
if (settings.get('UserData_FileSystemZipPath') != null) {
	if (settings.get('UserData_FileSystemZipPath').trim() !== '') {
		zipFolder = settings.get('UserData_FileSystemZipPath');
	}
}

let processingFrequency = 2;
if (settings.get('UserData_ProcessingFrequency') > 0) {
	processingFrequency = settings.get('UserData_ProcessingFrequency');
}

const startFile = function(fileName, content) {
	fs.writeFileSync(fileName, content);
};

const writeToFile = function(fileName, content) {
	fs.appendFileSync(fileName, content);
};

const createDir = function(folderName) {
	if (!fs.existsSync(folderName)) {
		fs.mkdirSync(folderName, { recursive: true });
	}
};

const loadUserSubscriptions = function(exportOperation) {
	exportOperation.roomList = [];

	const exportUserId = exportOperation.userId;
	const cursor = Subscriptions.findByUserId(exportUserId);
	cursor.forEach((subscription) => {
		const roomId = subscription.rid;
		const roomData = Rooms.findOneById(roomId);
		const roomName = roomData && roomData.name && subscription.t !== 'd' ? roomData.name : roomId;
		const [userId] = subscription.t === 'd' ? roomId.uids.filter((uid) => uid !== exportUserId) : [null];
		const fileName = exportOperation.fullExport ? roomId : roomName;
		const fileType = exportOperation.fullExport ? 'json' : 'html';
		const targetFile = `${ fileName }.${ fileType }`;

		exportOperation.roomList.push({
			roomId,
			roomName,<commit_msg>[FIX] Error during data export for DMs (#17577)<commit_after>import fs from 'fs';
import path from 'path';

import _ from 'underscore';
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { SyncedCron } from 'meteor/littledata:synced-cron';
import archiver from 'archiver';

import { settings } from '../../settings';
import { Subscriptions, Rooms, Users, Uploads, Messages, UserDataFiles, ExportOperations, Avatars } from '../../models';
import { FileUpload } from '../../file-upload';
import * as Mailer from '../../mailer';

let zipFolder = '/tmp/zipFiles';
if (settings.get('UserData_FileSystemZipPath') != null) {
	if (settings.get('UserData_FileSystemZipPath').trim() !== '') {
		zipFolder = settings.get('UserData_FileSystemZipPath');
	}
}

let processingFrequency = 2;
if (settings.get('UserData_ProcessingFrequency') > 0) {
	processingFrequency = settings.get('UserData_ProcessingFrequency');
}

const startFile = function(fileName, content) {
	fs.writeFileSync(fileName, content);
};

const writeToFile = function(fileName, content) {
	fs.appendFileSync(fileName, content);
};

const createDir = function(folderName) {
	if (!fs.existsSync(folderName)) {
		fs.mkdirSync(folderName, { recursive: true });
	}
};

const loadUserSubscriptions = function(exportOperation) {
	exportOperation.roomList = [];

	const exportUserId = exportOperation.userId;
	const cursor = Subscriptions.findByUserId(exportUserId);
	cursor.forEach((subscription) => {
		const roomId = subscription.rid;
		const roomData = Rooms.findOneById(roomId);
		const roomName = roomData && roomData.name && subscription.t !== 'd' ? roomData.name : roomId;
		const [userId] = subscription.t === 'd' ? roomData.uids.filter((uid) => uid !== exportUserId) : [null];
		const fileName = exportOperation.fullExport ? roomId : roomName;
		const fileType = exportOperation.fullExport ? 'json' : 'html';
		const targetFile = `${ fileName }.${ fileType }`;

		exportOperation.roomList.push({
			roomId,
			roomName,
{'fraction_non_alphanumeric': 0.14965139442231076, 'fraction_numerical': 0.004233067729083665, 'mean_word_length': 5.257009345794392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1456973', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1259, 'n_words': 284}