Add temporary file class

This commit is contained in:
Jonas Kvinge 2024-08-25 17:58:27 +02:00
parent 64aa15842c
commit 07c182d5b8
4 changed files with 131 additions and 0 deletions

View File

@ -43,6 +43,7 @@ set(SOURCES
core/localredirectserver.cpp core/localredirectserver.cpp
core/mimedata.cpp core/mimedata.cpp
core/potranslator.cpp core/potranslator.cpp
core/temporaryfile.cpp
utilities/strutils.cpp utilities/strutils.cpp
utilities/envutils.cpp utilities/envutils.cpp
utilities/colorutils.cpp utilities/colorutils.cpp

View File

@ -0,0 +1,73 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <QString>
#include <QFile>
#include <QRandomGenerator>
#include "core/logging.h"
#include "temporaryfile.h"
TemporaryFile::TemporaryFile(const QString &filename_pattern) {
int i = 0;
do {
filename_ = GenerateFilename(filename_pattern);
++i;
} while (QFile::exists(filename_) && i < 100);
if (QFile::exists(filename_)) {
qLog(Error) << "Could not get a filename from pattern" << filename_pattern;
filename_.clear();
}
else {
qLog(Debug) << "Temporary file" << filename_ << "available";
}
}
TemporaryFile::~TemporaryFile() {
if (!filename_.isEmpty() && QFile::exists(filename_)) {
qLog(Debug) << "Deleting temporary file" << filename_;
if (!QFile::remove(filename_)) {
qLog(Debug) << "Could not delete temporary file" << filename_;
}
}
}
QString TemporaryFile::GenerateFilename(const QString &filename_pattern) const {
static const QString random_chars = QStringLiteral("abcdefghijklmnopqrstuvwxyz0123456789");
QString filename = filename_pattern;
Q_FOREVER {
const qint64 i = filename.indexOf(QLatin1Char('X'));
if (i == -1) break;
const qint64 index = QRandomGenerator::global()->bounded(0, random_chars.length());
const QChar random_char = random_chars.at(index);
filename[i] = random_char;
}
return filename;
}

39
src/core/temporaryfile.h Normal file
View File

@ -0,0 +1,39 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TEMPORARYFILE_H
#define TEMPORARYFILE_H
#include <QString>
class TemporaryFile {
public:
explicit TemporaryFile(const QString &filename_pattern);
~TemporaryFile();
QString filename() const { return filename_; }
private:
QString GenerateFilename(const QString &filename_pattern) const;
private:
QString filename_;
};
#endif // TEMPORARYFILE_H

View File

@ -24,6 +24,7 @@
#include <QByteArray> #include <QByteArray>
#include <QString> #include <QString>
#include <QDateTime> #include <QDateTime>
#include <QRegularExpression>
#include <QtDebug> #include <QtDebug>
#include "test_utils.h" #include "test_utils.h"
@ -35,6 +36,7 @@
#include "utilities/colorutils.h" #include "utilities/colorutils.h"
#include "utilities/transliterate.h" #include "utilities/transliterate.h"
#include "core/logging.h" #include "core/logging.h"
#include "core/temporaryfile.h"
TEST(UtilitiesTest, PrettyTimeDelta) { TEST(UtilitiesTest, PrettyTimeDelta) {
@ -244,3 +246,19 @@ TEST(UtilitiesTest, ReplaceMessage) {
ASSERT_EQ(Utilities::ReplaceMessage(QStringLiteral("%title% - %artist%"), song, QLatin1String("")), song.title() + QStringLiteral(" - ") + song.artist()); ASSERT_EQ(Utilities::ReplaceMessage(QStringLiteral("%title% - %artist%"), song, QLatin1String("")), song.title() + QStringLiteral(" - ") + song.artist());
} }
TEST(UtilitiesTest, TemporaryFile) {
QString filename_pattern = QStringLiteral("/tmp/test-XXXX.jpg");
TemporaryFile temp_file(filename_pattern);
EXPECT_FALSE(temp_file.filename().isEmpty());
EXPECT_FALSE(temp_file.filename() == filename_pattern);
static const QRegularExpression regex_temp_filename(QStringLiteral("^\\/tmp\\/test-....\\.jpg$"));
EXPECT_TRUE(regex_temp_filename.match(temp_file.filename()).hasMatch());
}