Skip to content

Commit 4099165

Browse files
Merge pull request #2043 from nextcloud/backport/2040/stable-2.24.0
[stable-2.24.0] Improve download file remote operation
2 parents d8ff493 + f84b5bf commit 4099165

4 files changed

Lines changed: 298 additions & 201 deletions

File tree

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,170 @@
11
/*
22
* Nextcloud Android Library
33
*
4-
* SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-FileCopyrightText: 2021-2026 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
56
* SPDX-FileCopyrightText: 2021 Tobias Kaminsky <tobias@kaminsky.me>
67
* SPDX-License-Identifier: MIT
78
*/
89
package com.owncloud.android.lib.resources.files
910

1011
import com.owncloud.android.AbstractIT
12+
import org.junit.Assert.assertEquals
13+
import org.junit.Assert.assertFalse
1114
import org.junit.Assert.assertSame
1215
import org.junit.Assert.assertTrue
1316
import org.junit.Test
1417
import java.io.File
1518

19+
@Suppress("Detekt.MagicNumber")
1620
class DownloadFileRemoteOperationIT : AbstractIT() {
21+
private val cacheDir get() = context.externalCacheDir?.absolutePath
22+
1723
@Test
1824
fun download() {
1925
val filePath = createFile("download")
2026
val remotePath = "/download.jpg"
2127
assertTrue(
22-
@Suppress("Detekt.MagicNumber")
2328
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", 1464818400)
2429
.execute(client)
2530
.isSuccess
2631
)
2732

2833
assertTrue(
29-
DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath)
34+
DownloadFileRemoteOperation(remotePath, cacheDir)
3035
.execute(nextcloudClient)
3136
.isSuccess
3237
)
3338

3439
val oldFile = File(filePath)
35-
val newFile = File(context.externalCacheDir?.absolutePath + remotePath)
40+
val newFile = File(cacheDir + remotePath)
3641
assertSame(oldFile.length(), newFile.length())
3742
}
43+
44+
@Test
45+
fun downloadLargeFile() {
46+
val filePath = createFile("large_download", 1000)
47+
val remotePath = "/large_download.txt"
48+
assertTrue(
49+
UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME)
50+
.execute(client)
51+
.isSuccess
52+
)
53+
54+
assertTrue(
55+
DownloadFileRemoteOperation(remotePath, cacheDir)
56+
.execute(nextcloudClient)
57+
.isSuccess
58+
)
59+
60+
val originalFile = File(filePath)
61+
val downloadedFile = File(cacheDir + remotePath)
62+
assertEquals(originalFile.length(), downloadedFile.length())
63+
}
64+
65+
@Test
66+
fun downloadNonExistentFile() {
67+
val result =
68+
DownloadFileRemoteOperation("/nonexistent_file_12345.txt", cacheDir)
69+
.execute(nextcloudClient)
70+
71+
assertFalse(result.isSuccess)
72+
}
73+
74+
@Test
75+
fun downloadAndVerifyMetadata() {
76+
val filePath = createFile("metadata_download")
77+
val remotePath = "/metadata_download.jpg"
78+
assertTrue(
79+
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME)
80+
.execute(client)
81+
.isSuccess
82+
)
83+
84+
val operation = DownloadFileRemoteOperation(remotePath, cacheDir)
85+
assertTrue(operation.execute(nextcloudClient).isSuccess)
86+
87+
assertTrue("ETag should not be empty after download", operation.etag.isNotEmpty())
88+
assertTrue("Modification timestamp should be positive after download", operation.modificationTimestamp > 0)
89+
}
90+
91+
@Test
92+
fun downloadMultipleFiles() {
93+
val filePath1 = createFile("multi_download1")
94+
val remotePath1 = "/multi_download1.jpg"
95+
val filePath2 = createFile("multi_download2")
96+
val remotePath2 = "/multi_download2.jpg"
97+
98+
assertTrue(
99+
UploadFileRemoteOperation(filePath1, remotePath1, "image/jpg", RANDOM_MTIME)
100+
.execute(client)
101+
.isSuccess
102+
)
103+
assertTrue(
104+
UploadFileRemoteOperation(filePath2, remotePath2, "image/jpg", RANDOM_MTIME)
105+
.execute(client)
106+
.isSuccess
107+
)
108+
109+
assertTrue(
110+
DownloadFileRemoteOperation(remotePath1, cacheDir)
111+
.execute(nextcloudClient)
112+
.isSuccess
113+
)
114+
assertTrue(
115+
DownloadFileRemoteOperation(remotePath2, cacheDir)
116+
.execute(nextcloudClient)
117+
.isSuccess
118+
)
119+
120+
val downloaded1 = File(cacheDir + remotePath1)
121+
val downloaded2 = File(cacheDir + remotePath2)
122+
assertTrue(downloaded1.exists())
123+
assertTrue(downloaded2.exists())
124+
assertEquals(File(filePath1).length(), downloaded1.length())
125+
assertEquals(File(filePath2).length(), downloaded2.length())
126+
}
127+
128+
@Test
129+
fun downloadAndVerifyContent() {
130+
val filePath = createFile("content_download", 50)
131+
val remotePath = "/content_download.txt"
132+
assertTrue(
133+
UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME)
134+
.execute(client)
135+
.isSuccess
136+
)
137+
138+
assertTrue(
139+
DownloadFileRemoteOperation(remotePath, cacheDir)
140+
.execute(nextcloudClient)
141+
.isSuccess
142+
)
143+
144+
val originalFile = File(filePath)
145+
val downloadedFile = File(cacheDir + remotePath)
146+
assertTrue(downloadedFile.exists())
147+
assertTrue(originalFile.readBytes().contentEquals(downloadedFile.readBytes()))
148+
}
149+
150+
@Test
151+
fun downloadedFileExistsAtExpectedPath() {
152+
val filePath = createFile("path_check")
153+
val remotePath = "/path_check.jpg"
154+
assertTrue(
155+
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME)
156+
.execute(client)
157+
.isSuccess
158+
)
159+
160+
assertTrue(
161+
DownloadFileRemoteOperation(remotePath, cacheDir)
162+
.execute(nextcloudClient)
163+
.isSuccess
164+
)
165+
166+
val expectedFile = File(cacheDir + remotePath)
167+
assertTrue("Downloaded file should exist at expected path", expectedFile.exists())
168+
assertTrue("Downloaded file should not be empty", expectedFile.length() >= 0)
169+
}
38170
}

library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java

Lines changed: 0 additions & 195 deletions
This file was deleted.

0 commit comments

Comments
 (0)