1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-02-02 20:36:44 +01:00

Prepend the episode date to the filename when downloading podcasts, and append a numeric suffix until we find a unique filename.

Fixes issue 2997
This commit is contained in:
David Sansome 2012-06-14 17:07:21 +01:00
parent 404deff537
commit d6a8f4e15c
2 changed files with 34 additions and 5 deletions

View File

@ -133,6 +133,37 @@ void PodcastDownloader::FinishAndDelete(Task* task) {
NextTask();
}
QString PodcastDownloader::FilenameForEpisode(const QString& directory,
const PodcastEpisode& episode) const {
const QString file_extension = QFileInfo(episode.url().path()).suffix();
int count = 0;
// The file name contains the publication date and episode title
QString base_filename =
episode.publication_date().date().toString(Qt::ISODate) + "-" +
SanitiseFilenameComponent(episode.title());
// Add numbers on to the end of the filename until we find one that doesn't
// exist.
forever {
QString filename;
if (count == 0) {
filename = QString("%1/%2.%3").arg(
directory, base_filename, file_extension);
} else {
filename = QString("%1/%2 (%3).%4").arg(
directory, base_filename, QString::number(count), file_extension);
}
if (!QFile::exists(filename)) {
return filename;
}
count ++;
}
}
void PodcastDownloader::StartDownloading(Task* task) {
current_task_ = task;
@ -146,12 +177,9 @@ void PodcastDownloader::StartDownloading(Task* task) {
return;
}
const QString file_extension = QFileInfo(task->episode.url().path()).suffix();
const QString directory = download_dir_ + "/" +
SanitiseFilenameComponent(podcast.title());
const QString filename =
SanitiseFilenameComponent(task->episode.title()) + "." + file_extension;
const QString filepath = directory + "/" + filename;
const QString filepath = FilenameForEpisode(directory, task->episode);
// Open the output file
QDir().mkpath(directory);

View File

@ -80,7 +80,8 @@ private:
void NextTask();
void FinishAndDelete(Task* task);
QString FilenameForEpisode(const PodcastEpisode& episode) const;
QString FilenameForEpisode(const QString& directory,
const PodcastEpisode& episode) const;
QString SanitiseFilenameComponent(const QString& text) const;
private: