<commit_before>#include "FileIO.hpp"

FileIO::FileIO()
{
}

bool FileIO::write(const QString& source, const QString& data)
{
	if (source.isEmpty())
		return false;

	QFile file(source);
	if (!file.open(QFile::WriteOnly | QFile::Truncate))
		return false;

	QTextStream out(&file);
	out << data;
	file.close();

	return true;
}

bool FileIO::decodeAndWrite(const QString& source, const QString& data)
{
	if (source.isEmpty())
		return false;

	QFile file(source);
	if (!file.open(QFile::WriteOnly | QFile::Truncate))
		return false;

	QByteArray bytes;
	bytes.append(data);
	file.write(QByteArray::fromBase64(bytes));
	file.close();

	return true;
}

bool FileIO::setUserAvatar(int userLocalId, const QString &data)
{
	if(!m_tmpAvatarDir.isValid()) {
		return false;
	}

	QString avatar_path = this->getAvatarPath(userLocalId);
	return this->decodeAndWrite(avatar_path, data);
}

bool FileIO::hasAvatar(int userLocalId)
{
	return QFile::exists(this->getAvatarPath(userLocalId));
}

QString FileIO::getAvatarPath(int userLocalId)
{
	if(!m_tmpAvatarDir.isValid()) {
		return 0;
	}

	if(userLocalId == -2) {
		return this->m_tmpAvatarDir.path();
	}

    // Used QString::number() to get around a bug in windows that generates invalid characters
    return m_tmpAvatarDir.path().append("/").append(QString::number(userLocalId));
}

QUrl FileIO::getAvatarUrl(int userLocalId)
{
	return QUrl::fromLocalFile(this->getAvatarPath(userLocalId));
}

QString FileIO::getImagePath(const QString &name)
{
	if(!m_tmpImageDir.isValid()) {
		return 0;
	}

	return m_tmpImageDir.path().append("/").append(name);
}

QString FileIO::decodeImage(const QString &data)
{
	if(data.isEmpty()) {
		return "";
	}

	if(!m_tmpImageDir.isValid()) {
		return 0;
	}

	QString image_name(this->getMd5Hash(data));
	QString image_path(this->getImagePath(image_name));

	// Check to see if file exist already
	if(QFile::exists(image_path)) {
		return QUrl::fromLocalFile(image_path).toString();
	}

	// If file is not exist yet, create it
	if(this->decodeAndWrite(image_path, data)) {
		return QUrl::fromLocalFile(image_path).toString();
	}

	return 0;
}

QString FileIO::encodeImage(const QString &source)
{
	if (source.isEmpty())
		return 0;

    QFile file(source);
	if (!file.open(QFile::ReadOnly))
		return 0;

    QByteArray bytes(file.readAll());
	file.close();

    return QString(bytes.toBase64());
}

QString FileIO::encodeImage(const QUrl &source)
{
    return this->encodeImage(source.toLocalFile());
}

QString FileIO::cropEncodeImage(const QUrl &source)
{
    QByteArray bytes;
    QBuffer buffer(&bytes, this);
    QImage image(source.toLocalFile());
    // Resize image
    QImage(image.scaled(128, 128, Qt::KeepAspectRatioByExpanding)).save(&buffer, "JPG");
    buffer.open(QIODevice::WriteOnly);

    return bytes.toBase64();<commit_msg>* Use smooth transformation in Avatar resizing<commit_after>#include "FileIO.hpp"

FileIO::FileIO()
{
}

bool FileIO::write(const QString& source, const QString& data)
{
	if (source.isEmpty())
		return false;

	QFile file(source);
	if (!file.open(QFile::WriteOnly | QFile::Truncate))
		return false;

	QTextStream out(&file);
	out << data;
	file.close();

	return true;
}

bool FileIO::decodeAndWrite(const QString& source, const QString& data)
{
	if (source.isEmpty())
		return false;

	QFile file(source);
	if (!file.open(QFile::WriteOnly | QFile::Truncate))
		return false;

	QByteArray bytes;
	bytes.append(data);
	file.write(QByteArray::fromBase64(bytes));
	file.close();

	return true;
}

bool FileIO::setUserAvatar(int userLocalId, const QString &data)
{
	if(!m_tmpAvatarDir.isValid()) {
		return false;
	}

	QString avatar_path = this->getAvatarPath(userLocalId);
	return this->decodeAndWrite(avatar_path, data);
}

bool FileIO::hasAvatar(int userLocalId)
{
	return QFile::exists(this->getAvatarPath(userLocalId));
}

QString FileIO::getAvatarPath(int userLocalId)
{
	if(!m_tmpAvatarDir.isValid()) {
		return 0;
	}

	if(userLocalId == -2) {
		return this->m_tmpAvatarDir.path();
	}

    // Used QString::number() to get around a bug in windows that generates invalid characters
    return m_tmpAvatarDir.path().append("/").append(QString::number(userLocalId));
}

QUrl FileIO::getAvatarUrl(int userLocalId)
{
	return QUrl::fromLocalFile(this->getAvatarPath(userLocalId));
}

QString FileIO::getImagePath(const QString &name)
{
	if(!m_tmpImageDir.isValid()) {
		return 0;
	}

	return m_tmpImageDir.path().append("/").append(name);
}

QString FileIO::decodeImage(const QString &data)
{
	if(data.isEmpty()) {
		return "";
	}

	if(!m_tmpImageDir.isValid()) {
		return 0;
	}

	QString image_name(this->getMd5Hash(data));
	QString image_path(this->getImagePath(image_name));

	// Check to see if file exist already
	if(QFile::exists(image_path)) {
		return QUrl::fromLocalFile(image_path).toString();
	}

	// If file is not exist yet, create it
	if(this->decodeAndWrite(image_path, data)) {
		return QUrl::fromLocalFile(image_path).toString();
	}

	return 0;
}

QString FileIO::encodeImage(const QString &source)
{
	if (source.isEmpty())
		return 0;

    QFile file(source);
	if (!file.open(QFile::ReadOnly))
		return 0;

    QByteArray bytes(file.readAll());
	file.close();

    return QString(bytes.toBase64());
}

QString FileIO::encodeImage(const QUrl &source)
{
    return this->encodeImage(source.toLocalFile());
}

QString FileIO::cropEncodeImage(const QUrl &source)
{
    QByteArray bytes;
    QBuffer buffer(&bytes, this);
    QImage image(source.toLocalFile());
    // Resize image
    QImage(image.scaled(128, 128, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)).save(&buffer, "JPG");
    buffer.open(QIODevice::WriteOnly);

    return bytes.toBase64();
{'fraction_non_alphanumeric': 0.14960491659350308, 'fraction_numerical': 0.007023705004389816, 'mean_word_length': 5.259340659340659, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '4058924', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2011, 'n_words': 394}