Merge lp:~mandel/ubuntu-download-manager/throw-error-exists into lp:ubuntu-download-manager

Proposed by Manuel de la Peña
Status: Merged
Approved by: Roberto Alsina
Approved revision: 136
Merged at revision: 146
Proposed branch: lp:~mandel/ubuntu-download-manager/throw-error-exists
Merge into: lp:ubuntu-download-manager
Prerequisite: lp:~mandel/ubuntu-download-manager/use-standard-dirs
Diff against target: 231 lines (+139/-10)
6 files modified
libubuntudownloadmanager/group_download.cpp (+8/-9)
libubuntudownloadmanager/single_download.cpp (+16/-0)
ubuntu-download-manager-tests/test_download.cpp (+62/-1)
ubuntu-download-manager-tests/test_download.h (+5/-0)
ubuntu-download-manager-tests/test_group_download.cpp (+46/-0)
ubuntu-download-manager-tests/test_group_download.h (+2/-0)
To merge this branch: bzr merge lp:~mandel/ubuntu-download-manager/throw-error-exists
Reviewer Review Type Date Requested Status
Diego Sarmentero (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+187527@code.launchpad.net

Commit message

Deal with the presence of the download file in a nicer way.

Description of the change

Deal with the presence of the download file in a nicer way. This branch takes care of two possible cases:

1. Local file and not confined app => returns a dbus error.
2. All other cases (either confined or not but with the local path) => appends uuid at the end of the file.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
134. By Manuel de la Peña

Merged use-standard-dirs into throw-error-exists.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
135. By Manuel de la Peña

Merged use-standard-dirs into throw-error-exists.

136. By Manuel de la Peña

Merged with trunk.

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

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libubuntudownloadmanager/group_download.cpp'
2--- libubuntudownloadmanager/group_download.cpp 2013-09-27 21:56:44 +0000
3+++ libubuntudownloadmanager/group_download.cpp 2013-09-30 11:43:31 +0000
4@@ -78,15 +78,6 @@
5 QUrl url(download.getUrl());
6 QString hash = download.getHash();
7
8- // before we create the download ensure that the url is valid
9- // if not we set the error and stop the loop
10- if (!url.isValid()) {
11- q->setIsValid(false);
12- q->setLastError(QString("Invalid URL: '%1'").arg(
13- url.toString()));
14- break;
15- }
16-
17 SingleDownload* singleDownload;
18 QVariantMap downloadMetadata = QVariantMap(metadata);
19 downloadMetadata[LOCAL_PATH_KEY] = download.getLocalFile();
20@@ -112,6 +103,14 @@
21 headers));
22 }
23
24+ // check that the download is valid, if it is not set to invalid
25+ // and use the last error from the download
26+ if (!singleDownload->isValid()) {
27+ q->setIsValid(false);
28+ q->setLastError(singleDownload->lastError());
29+ break;
30+ }
31+
32 singleDownload->allowGSMDownload(isGSMDownloadAllowed);
33 _downloads.append(singleDownload);
34 _downloadsProgress[singleDownload->url()] =
35
36=== modified file 'libubuntudownloadmanager/single_download.cpp'
37--- libubuntudownloadmanager/single_download.cpp 2013-09-25 09:12:24 +0000
38+++ libubuntudownloadmanager/single_download.cpp 2013-09-30 11:43:31 +0000
39@@ -28,6 +28,7 @@
40 #include "./single_download.h"
41 #include "./network_reply.h"
42
43+
44 #define DATA_FILE_NAME "data.download"
45 #define METADATA_FILE_NAME "metadata"
46 #define METADATA_COMMAND_KEY "post-download-command"
47@@ -417,8 +418,23 @@
48 if (!q->isConfined() && metadata.contains(LOCAL_PATH_KEY)) {
49 qDebug() << "App is not confined and provided a local path";
50 finalPath = metadata[LOCAL_PATH_KEY].toString();
51+
52+ // in this case and because the app is not confined we are
53+ // going to check if the file exists, if it does we will
54+ // raise an error
55+ if (QFile::exists(finalPath)) {
56+ q->setIsValid(false);
57+ q->setLastError(QString("File already exists at: '%1'").arg(
58+ finalPath));
59+ }
60 } else {
61 finalPath = q->rootPath() + QDir::separator() + basename;
62+
63+ // check if the file exists, if it does lets append the uuid to it
64+ if (QFile::exists(finalPath)) {
65+ finalPath += q->downloadId().toString().replace(
66+ QRegExp("[-{}]"), "");
67+ }
68 }
69
70 return finalPath;
71
72=== modified file 'ubuntu-download-manager-tests/test_download.cpp'
73--- ubuntu-download-manager-tests/test_download.cpp 2013-09-27 23:43:33 +0000
74+++ ubuntu-download-manager-tests/test_download.cpp 2013-09-30 11:43:31 +0000
75@@ -54,7 +54,6 @@
76 delete _reqFactory;
77 if (_processFactory)
78 delete _processFactory;
79-
80 }
81
82 void
83@@ -1750,3 +1749,65 @@
84 QSharedPointer<ProcessFactory>(_processFactory));
85 QVERIFY(download->isValid());
86 }
87+
88+void
89+TestDownload::testInvalidFilePresent() {
90+ // create a file so that we get an error
91+ QString filePath = testDirectory() + QDir::separator() + "test_file.jpg";
92+ QFile* file = new QFile(filePath);
93+ file->open(QIODevice::ReadWrite | QFile::Append);
94+ file->write("data data data!");
95+ file->close();
96+
97+ QVariantMap metadata;
98+ metadata["local-path"] = filePath;
99+
100+ SingleDownload* download = new SingleDownload(_id, _path, false,
101+ _rootPath, _url, metadata, _headers,
102+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
103+ QSharedPointer<RequestFactory>(_reqFactory),
104+ QSharedPointer<ProcessFactory>(_processFactory));
105+ QVERIFY(!download->isValid());
106+}
107+
108+void
109+TestDownload::testValidFileNotPresent() {
110+ QString filePath = testDirectory() + QDir::separator() + "test_file.jpg";
111+
112+ QVariantMap metadata;
113+ metadata["local-path"] = filePath;
114+
115+ SingleDownload* download = new SingleDownload(_id, _path, false,
116+ _rootPath, _url, metadata, _headers,
117+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
118+ QSharedPointer<RequestFactory>(_reqFactory),
119+ QSharedPointer<ProcessFactory>(_processFactory));
120+ QVERIFY(download->isValid());
121+}
122+
123+void
124+TestDownload::testDownloadPresent() {
125+ // create a download and get the filename to use, then write it
126+ // and create the same download and assert that the filename is diff
127+
128+ SingleDownload* download = new SingleDownload(_id, _path, true,
129+ _rootPath, _url, _metadata, _headers,
130+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
131+ QSharedPointer<RequestFactory>(_reqFactory),
132+ QSharedPointer<ProcessFactory>(_processFactory));
133+
134+ QString filePath = download->filePath();
135+
136+ QFile* file = new QFile(filePath);
137+ file->open(QIODevice::ReadWrite | QFile::Append);
138+ file->write("data data data!");
139+ file->close();
140+
141+ download = new SingleDownload(_id, _path, true,
142+ _rootPath, _url, _metadata, _headers,
143+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
144+ QSharedPointer<RequestFactory>(_reqFactory),
145+ QSharedPointer<ProcessFactory>(_processFactory));
146+
147+ QVERIFY(filePath != download->filePath());
148+}
149
150=== modified file 'ubuntu-download-manager-tests/test_download.h'
151--- ubuntu-download-manager-tests/test_download.h 2013-09-25 10:30:57 +0000
152+++ ubuntu-download-manager-tests/test_download.h 2013-09-30 11:43:31 +0000
153@@ -130,6 +130,11 @@
154 void testInvalidHashAlgorithm();
155 void testValidHashAlgorithm_data();
156 void testValidHashAlgorithm();
157+ void testInvalidFilePresent();
158+ void testValidFileNotPresent();
159+
160+ // filename tests
161+ void testDownloadPresent();
162
163 private:
164 QUuid _id;
165
166=== modified file 'ubuntu-download-manager-tests/test_group_download.cpp'
167--- ubuntu-download-manager-tests/test_group_download.cpp 2013-09-27 23:43:33 +0000
168+++ ubuntu-download-manager-tests/test_group_download.cpp 2013-09-30 11:43:31 +0000
169@@ -856,3 +856,49 @@
170
171 QVERIFY(group->isValid());
172 }
173+
174+void
175+TestGroupDownload::testInvalidFilePresent() {
176+ QString filePath = testDirectory() + QDir::separator() + "test_file.jpg";
177+ QFile* file = new QFile(filePath);
178+ file->open(QIODevice::ReadWrite | QFile::Append);
179+ file->write("data data data!");
180+ file->close();
181+
182+ QList<GroupDownloadStruct> downloadsStruct;
183+ downloadsStruct.append(GroupDownloadStruct("http://one.ubuntu.com",
184+ "local_file", ""));
185+ downloadsStruct.append(GroupDownloadStruct("http://ubuntu.com",
186+ filePath, ""));
187+ downloadsStruct.append(GroupDownloadStruct("http://reddit.com",
188+ "other_reddit_local_file", ""));
189+
190+ GroupDownload* group = new GroupDownload(_id, _path, false, _rootPath,
191+ downloadsStruct, "md5", _isGSMDownloadAllowed, _metadata, _headers,
192+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
193+ QSharedPointer<DownloadFactory>(_downloadFactory),
194+ QSharedPointer<FileManager>(_fileManager));
195+
196+ QVERIFY(!group->isValid());
197+}
198+
199+void
200+TestGroupDownload::testValidFileNotPresent() {
201+ QString filePath = testDirectory() + QDir::separator() + "test_file.jpg";
202+
203+ QList<GroupDownloadStruct> downloadsStruct;
204+ downloadsStruct.append(GroupDownloadStruct("http://one.ubuntu.com",
205+ "local_file", ""));
206+ downloadsStruct.append(GroupDownloadStruct("http://ubuntu.com",
207+ filePath, ""));
208+ downloadsStruct.append(GroupDownloadStruct("http://reddit.com",
209+ "other_reddit_local_file", ""));
210+
211+ GroupDownload* group = new GroupDownload(_id, _path, false, _rootPath,
212+ downloadsStruct, "md5", _isGSMDownloadAllowed, _metadata, _headers,
213+ QSharedPointer<SystemNetworkInfo>(_networkInfo),
214+ QSharedPointer<DownloadFactory>(_downloadFactory),
215+ QSharedPointer<FileManager>(_fileManager));
216+
217+ QVERIFY(group->isValid());
218+}
219
220=== modified file 'ubuntu-download-manager-tests/test_group_download.h'
221--- ubuntu-download-manager-tests/test_group_download.h 2013-09-25 10:30:57 +0000
222+++ ubuntu-download-manager-tests/test_group_download.h 2013-09-30 11:43:31 +0000
223@@ -75,6 +75,8 @@
224 void testInvalidHashAlgorithm();
225 void testValidHashAlgorithm_data();
226 void testValidHashAlgorithm();
227+ void testInvalidFilePresent();
228+ void testValidFileNotPresent();
229
230 private:
231 QUuid _id;

Subscribers

People subscribed via source and target branches