-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
284eaab
commit 766ecb6
Showing
2 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.taonity.vkforwarderbot | ||
|
||
import mu.KotlinLogging | ||
import org.springframework.stereotype.Component | ||
import java.io.InputStreamReader | ||
import java.util.stream.Collectors | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
private val LOGGER = KotlinLogging.logger {} | ||
|
||
@Component | ||
class CurlService( | ||
private val cacheService: CacheService | ||
) { | ||
fun downloadVideoInCache(videoUrl: String, videoBaseName: String) : String? { | ||
val videoName = "${videoBaseName}.mp4" | ||
|
||
LOGGER.debug { "Start downloading the video $videoUrl with $videoBaseName name" } | ||
|
||
val process = ProcessBuilder( | ||
"bash.exe", | ||
"-c", | ||
"curl --silent --output ${cacheService.cacheDirPath}/${videoName} '${videoUrl}'", | ||
) | ||
.start() | ||
waitForVideoToDownload(process) | ||
|
||
val curlErrorLog = process.errorReader().lines().collect(Collectors.joining()) | ||
|
||
if(curlErrorLog.isNotEmpty()) { | ||
LOGGER.error { curlErrorLog } | ||
return null | ||
} | ||
|
||
return cacheService.listFilesInCache().stream() | ||
.filter { cachedVideoName -> cachedVideoName.contains(videoName) } | ||
.findAny() | ||
.getOrNull() | ||
} | ||
|
||
private fun waitForVideoToDownload(process: Process) { | ||
// TODO: find a better way to do the waiting | ||
InputStreamReader(process.inputStream).use { inputStreamReader -> | ||
while (inputStreamReader.read() >= 0) { | ||
} | ||
} | ||
|
||
LOGGER.info { "Finish downloading the video" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters