Merge lp:~mandel/ubuntu-download-manager/better-filenames into lp:ubuntu-download-manager

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 214
Merged at revision: 214
Proposed branch: lp:~mandel/ubuntu-download-manager/better-filenames
Merge into: lp:ubuntu-download-manager
Diff against target: 134 lines (+81/-3)
4 files modified
ubuntu-download-manager-priv/downloads/file_download.cpp (+31/-3)
ubuntu-download-manager-priv/downloads/file_download.h (+1/-0)
ubuntu-download-manager-tests/downloads/test_download.cpp (+47/-0)
ubuntu-download-manager-tests/downloads/test_download.h (+2/-0)
To merge this branch: bzr merge lp:~mandel/ubuntu-download-manager/better-filenames
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Alejandro J. Cura (community) Approve
Diego Sarmentero (community) Approve
Review via email: mp+201924@code.launchpad.net

Commit message

Check if the file is present and use a more human redable filename that does not break extensions.

Description of the change

Check if the file is present and use a more human redable filename that does not break extensions.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

Please move everything after this comment into a separate function called "uniquePath" or something like that:
> // check if the file exists, if it does lets append the uuid to it
Also change that comment since this is not using UUIDs any longer, but numbers instead.

> firstPart = finalPath.left(finalPath.size() - secondPart.size())
Please replace this with fileInfo.baseName, and probably take it out of that if, and remove that else.

review: Needs Fixing
Revision history for this message
Manuel de la Peña (mandel) wrote :

> Please move everything after this comment into a separate function called "uniquePath" or something like that:
> // check if the file exists, if it does lets append the uuid to it
> Also change that comment since this is not using UUIDs any longer, but numbers instead.

Sure, it is not hard to do.

>> firstPart = finalPath.left(finalPath.size() - secondPart.size())
> Please replace this with fileInfo.baseName, and probably take it out of that if, and remove that else.

Sorry but baseName does not do what you expect or what is correct. In this case we are dealing with absolute paths and per docuemtnation of Qt http://qt-project.org/doc/qt-4.8/qfileinfo.html#baseName basename does not return the entire full path of the file before the first '.'

Example:

 QFileInfo fi("/tmp/archive.tar.gz");
 QString base = fi.baseName(); // base = "archive"

Nor completeBaseName does it:

 QFileInfo fi("/tmp/archive.tar.gz");
 QString base = fi.completeBaseName(); // base = "archive.tar"

Nor fileName:

 QFileInfo fi("/tmp/archive.tar.gz");
 QString name = fi.fileName(); // name = "archive.tar.gz"

So the best way to deal with it is the one currently present in the MP.

213. By Manuel de la Peña

Made changes according to the reviews.

214. By Manuel de la Peña

Remove not needed else.

Revision history for this message
Alejandro J. Cura (alecu) wrote :

Looks good, +1

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu-download-manager-priv/downloads/file_download.cpp'
2--- ubuntu-download-manager-priv/downloads/file_download.cpp 2014-01-15 10:59:14 +0000
3+++ ubuntu-download-manager-priv/downloads/file_download.cpp 2014-01-16 15:24:26 +0000
4@@ -474,16 +474,44 @@
5 }
6 } else {
7 finalPath = rootPath() + QDir::separator() + basename;
8-
9- // check if the file exists, if it does lets append the uuid to it
10 if (QFile::exists(finalPath)) {
11- finalPath += downloadId();
12+ finalPath = uniqueFilePath(finalPath);
13 }
14+
15 }
16
17 return finalPath;
18 }
19
20+QString
21+FileDownload::uniqueFilePath(QString path) {
22+ QFileInfo fileInfo(path);
23+
24+ // Split the file into 2 parts - dot+extension, and everything else. For
25+ // example, "path/file.tar.gz" becomes "path/file"+".tar.gz", while
26+ // "path/file" (note lack of extension) becomes "path/file"+"".
27+ auto secondPart = fileInfo.completeSuffix();
28+ auto firstPart = path;
29+
30+ if (!secondPart.isEmpty()) {
31+ secondPart = "." + secondPart;
32+ firstPart = path.left(path.size() - secondPart.size());
33+ }
34+
35+ // Try with an ever-increasing number suffix, until we've reached a file
36+ // that does not yet exist.
37+ for (int ii = 1; ; ii++) {
38+ // Construct the new file name by adding the unique number between the
39+ // first and second part.
40+ auto finalPath = QString("%1 (%2)%3").arg(firstPart).arg(ii).arg(secondPart);
41+ // If no file exists with the new name, return it.
42+ if (!QFile::exists(finalPath)) {
43+ return finalPath;
44+ }
45+ } // for
46+ return path;
47+}
48+
49 void
50 FileDownload::cleanUpCurrentData() {
51 bool success = true;
52
53=== modified file 'ubuntu-download-manager-priv/downloads/file_download.h'
54--- ubuntu-download-manager-priv/downloads/file_download.h 2013-12-20 22:57:15 +0000
55+++ ubuntu-download-manager-priv/downloads/file_download.h 2014-01-16 15:24:26 +0000
56@@ -108,6 +108,7 @@
57 QProcess::ExitStatus exitStatus);
58 void onOnlineStateChanged(bool);
59 QString getSaveFileName();
60+ QString uniqueFilePath(QString path);
61
62 private:
63 bool _downloading = false;
64
65=== modified file 'ubuntu-download-manager-tests/downloads/test_download.cpp'
66--- ubuntu-download-manager-tests/downloads/test_download.cpp 2013-12-20 22:57:15 +0000
67+++ ubuntu-download-manager-tests/downloads/test_download.cpp 2014-01-16 15:24:26 +0000
68@@ -1694,6 +1694,53 @@
69 }
70
71 void
72+TestDownload::testDownloadPresentSeveralFiles_data() {
73+ QTest::addColumn<int>("count");
74+
75+ QTest::newRow("One") << 1;
76+ QTest::newRow("Some") << 3;
77+ QTest::newRow("Several") << 10;
78+ QTest::newRow("Plenti") << 100;
79+}
80+
81+void
82+TestDownload::testDownloadPresentSeveralFiles() {
83+ QFETCH(int, count);
84+
85+ QScopedPointer<FileDownload> download(new FileDownload(_id, _path, true,
86+ _rootPath, _url, _metadata, _headers));
87+
88+ QString filePath = download->filePath();
89+
90+ QScopedPointer<QFile> file(new QFile(filePath));
91+ file->open(QIODevice::ReadWrite | QFile::Append);
92+ file->write("data data data!");
93+ file->close();
94+
95+ QFileInfo fileInfo(filePath);
96+ auto suffix = "." + fileInfo.completeSuffix();
97+ auto prefix = filePath.left(filePath.size() - suffix.size());
98+
99+ // write the rest of the files
100+ for(int index=1; index < count; index++) {
101+ auto otherPath = QString("%1 (%2)%3").arg(prefix).arg(index).arg(suffix);
102+ QScopedPointer<QFile> otherFile(new QFile(otherPath));
103+ otherFile->open(QIODevice::ReadWrite | QFile::Append);
104+ otherFile->write("data data data!");
105+ otherFile->close();
106+ }
107+
108+ QScopedPointer<FileDownload> other(new FileDownload(_id, _path, true,
109+ _rootPath, _url, _metadata, _headers));
110+
111+ QVERIFY(filePath != other->filePath());
112+ if (count > 0) {
113+ auto expectedPath = QString("%1 (%2)%3").arg(prefix).arg(count).arg(suffix);
114+ QCOMPARE(expectedPath, other->filePath());
115+ }
116+}
117+
118+void
119 TestDownload::testProcessingJustOnce() {
120 QCryptographicHash::Algorithm algorithm = HashAlgorithm::getHashAlgo(_algo);
121 QByteArray data(300, 't');
122
123=== modified file 'ubuntu-download-manager-tests/downloads/test_download.h'
124--- ubuntu-download-manager-tests/downloads/test_download.h 2013-12-20 22:57:15 +0000
125+++ ubuntu-download-manager-tests/downloads/test_download.h 2014-01-16 15:24:26 +0000
126@@ -137,6 +137,8 @@
127
128 // filename tests
129 void testDownloadPresent();
130+ void testDownloadPresentSeveralFiles_data();
131+ void testDownloadPresentSeveralFiles();
132
133 // processing signal tests
134 void testProcessingJustOnce();

Subscribers

People subscribed via source and target branches