-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadDirectory.kt
80 lines (63 loc) · 2.7 KB
/
DownloadDirectory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.github.old_horizon.selenium
import com.codeborne.selenide.Driver
import com.github.kittinunf.fuel.core.ResponseResultOf
import com.github.kittinunf.fuel.httpDelete
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.jackson.responseObject
import com.github.kittinunf.result.Result
import org.openqa.selenium.remote.SessionId
import java.io.ByteArrayInputStream
import java.io.File
import java.io.InputStream
import java.net.URL
interface DownloadDirectory {
fun listFiles(): List<String>
fun deleteFiles()
fun exists(name: String): Boolean
fun inputStream(name: String): InputStream
fun deleteFile(name: String)
companion object {
fun of(driver: Driver): DownloadDirectory =
driver.config().remote()
?.let { RemoteDownloadDirectory(URL(it), driver.sessionId) }
?: LocalDownloadDirectory(driver.browserDownloadsFolder()!!.toFile())
}
}
class LocalDownloadDirectory(private val dir: File) : DownloadDirectory {
override fun listFiles(): List<String> = dir.list()!!.toList()
override fun deleteFiles() {
dir.deleteRecursively()
dir.mkdir()
}
override fun exists(name: String): Boolean = dir.resolve(name).exists()
override fun inputStream(name: String): InputStream = dir.resolve(name).inputStream()
override fun deleteFile(name: String) {
dir.resolve(name).delete()
}
}
class RemoteDownloadDirectory(private val remoteUrl: URL, private val sessionId: SessionId) : DownloadDirectory {
override fun listFiles(): List<String> = buildUrl("/").httpGet()
.responseObject<FilesJson>()
.let(::handleResult).files
.map { it.name }
override fun deleteFiles() {
buildUrl("/").httpDelete().response().let(::handleResult)
}
override fun exists(name: String): Boolean = listFiles().contains(name)
override fun inputStream(name: String): InputStream =
buildUrl("/$name").httpGet().response().let(::handleResult).let(::ByteArrayInputStream)
override fun deleteFile(name: String) {
buildUrl("/$name").httpDelete().response().let(::handleResult)
}
private fun buildUrl(path: String): String =
"${remoteUrl.protocol}://${remoteUrl.host}:${remoteUrl.port}/grid/admin/Downloads/$sessionId$path"
private fun <T : Any> handleResult(responseResult: ResponseResultOf<T>): T {
val (_, _, result) = responseResult
return when (result) {
is Result.Success<T> -> result.get()
is Result.Failure -> throw result.getException()
}
}
data class FilesJson(val files: List<FileJson>)
data class FileJson(val name: String)
}