Skip to content

Commit ae78a1d

Browse files
committed
fix(amule): Only remove file through amule if it's being downloaded
1 parent d62ac5d commit ae78a1d

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

app/src/main/kotlin/amarr/torrent/TorrentService.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import jamule.AmuleClient
99
import jamule.model.AmuleTransferringFile
1010
import jamule.model.DownloadCommand
1111
import jamule.model.FileStatus
12+
import kotlin.io.path.Path
1213

1314
class TorrentService(
1415
private val amuleClient: AmuleClient,
@@ -111,9 +112,20 @@ class TorrentService(
111112
}
112113

113114
@OptIn(ExperimentalStdlibApi::class)
114-
fun deleteTorrent(hashes: List<String>, deleteFiles: String?) = hashes.forEach { hash ->
115-
amuleClient.sendDownloadCommand(hash.hexToByteArray(), DownloadCommand.DELETE)
116-
categoryStore.delete(hash)
115+
fun deleteTorrent(hashes: List<String>, deleteFiles: String?) {
116+
val downloadingFiles = amuleClient
117+
.getDownloadQueue()
118+
.getOrThrow()
119+
hashes.forEach { hash ->
120+
if (downloadingFiles.any { it.fileHashHexString == hash }) {
121+
amuleClient.sendDownloadCommand(hash.hexToByteArray(), DownloadCommand.DELETE)
122+
} else if (deleteFiles == "true") {
123+
deleteSharedFileByHash(hash)
124+
} else {
125+
log.error("File with hash $hash not found in downloading files")
126+
}
127+
categoryStore.delete(hash)
128+
}
117129
}
118130

119131
@OptIn(ExperimentalStdlibApi::class)
@@ -140,6 +152,14 @@ class TorrentService(
140152
)
141153
}
142154

155+
private fun deleteSharedFileByHash(hash: String) = amuleClient
156+
.getSharedFiles()
157+
.getOrThrow()
158+
.firstOrNull { it.fileHashHexString == hash }
159+
?.filePath
160+
?.let { Path(it).toFile().delete() }
161+
?: log.error("File with hash $hash not found in shared files")
162+
143163
private fun nonAmarrLink(url: String): Exception {
144164
log.error(
145165
"The provided link does not appear to be an Amarr link: {}. " +

app/src/test/kotlin/amarr/torrent/TorrentApiTest.kt

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import io.ktor.server.testing.*
1515
import io.mockk.clearAllMocks
1616
import io.mockk.every
1717
import io.mockk.mockk
18+
import io.mockk.verify
1819
import jamule.AmuleClient
1920
import jamule.model.AmuleTransferringFile
2021
import jamule.model.DownloadCommand
2122
import jamule.model.FileStatus
2223
import kotlinx.serialization.json.Json
24+
import java.nio.file.Files
2325

2426
class TorrentApiTest : StringSpec({
2527
val amuleClient = mockk<AmuleClient>()
@@ -117,21 +119,69 @@ class TorrentApiTest : StringSpec({
117119
}
118120
}
119121

120-
"should delete torrent" {
122+
"should delete torrent when downloading" {
121123
testApplication {
122124
application {
123125
torrentApi(amuleClient, categoryStore, finishedPath)
124126
configureForTest()
125127
}
128+
categoryStore.store("test", testMagnetLink.amuleHexHash())
126129
every {
127130
amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand.DELETE)
128131
} returns Result.success(Unit)
132+
every {
133+
amuleClient.getDownloadQueue()
134+
} returns Result.success(
135+
listOf(
136+
MockTransferringFile(
137+
fileHashHexString = testMagnetLink.amuleHexHash(),
138+
fileName = testMagnetLink.name,
139+
sizeFull = testMagnetLink.size,
140+
)
141+
)
142+
)
143+
client.submitForm(formParameters = Parameters.build {
144+
append("hashes", testMagnetLink.amuleHexHash())
145+
append("deleteFiles", "true")
146+
}, url = "/api/v2/torrents/delete").apply {
147+
this.status shouldBe HttpStatusCode.OK
148+
}
149+
verify { amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand.DELETE) }
150+
categoryStore.getCategory(testMagnetLink.amuleHexHash()) shouldBe null
151+
}
152+
}
153+
154+
"should delete file when not downloading" {
155+
testApplication {
156+
application {
157+
torrentApi(amuleClient, categoryStore, finishedPath)
158+
configureForTest()
159+
}
160+
categoryStore.store("test", testMagnetLink.amuleHexHash())
161+
every {
162+
amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand.DELETE)
163+
} returns Result.success(Unit)
164+
val randomTemporaryFile = Files.createTempFile("test", "test")
165+
every { amuleClient.getSharedFiles() } returns Result.success(
166+
listOf(
167+
MockTransferringFile(
168+
fileHashHexString = testMagnetLink.amuleHexHash(),
169+
fileName = testMagnetLink.name,
170+
sizeFull = testMagnetLink.size,
171+
filePath = randomTemporaryFile.toAbsolutePath().toString()
172+
)
173+
)
174+
)
175+
every { amuleClient.getDownloadQueue() } returns Result.success(emptyList())
129176
client.submitForm(formParameters = Parameters.build {
130177
append("hashes", testMagnetLink.amuleHexHash())
131-
append("deleteFiles", "test")
178+
append("deleteFiles", "true")
132179
}, url = "/api/v2/torrents/delete").apply {
133180
this.status shouldBe HttpStatusCode.OK
134181
}
182+
verify(exactly = 0) { amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand.DELETE) }
183+
categoryStore.getCategory(testMagnetLink.amuleHexHash()) shouldBe null
184+
Files.exists(randomTemporaryFile) shouldBe false
135185
}
136186
}
137187

0 commit comments

Comments
 (0)