Unverified Commit c691574f authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

catch URLMalformedExceptions

parent 7cd3a77e
Pipeline #16648 passed with stages
in 6 minutes and 5 seconds
......@@ -4,6 +4,7 @@ import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
......@@ -26,7 +27,11 @@ class PreviewImageHandler(private val sftpClient: SftpClient) {
* @return true if URL points to Vimeo
*/
fun isVimeoUrl(url: String): Boolean {
return URL(url).host.toLowerCase() == "vimeo.com"
return try {
URL(url).host.toLowerCase() == "vimeo.com"
} catch (ex: MalformedURLException) {
false
}
}
/**
......@@ -42,8 +47,8 @@ class PreviewImageHandler(private val sftpClient: SftpClient) {
}
private fun get(urlAsString: String): String? {
val url = URL(urlAsString)
return try {
val url = URL(urlAsString)
val tempFile = Files.createTempFile("", ".jpg")
val outputStream = FileOutputStream(tempFile.toFile())
with(url.openConnection() as HttpURLConnection) {
......@@ -60,6 +65,9 @@ class PreviewImageHandler(private val sftpClient: SftpClient) {
} catch (ex: FileNotFoundException) {
log.error("Can't find temporary file: ${ex.message}")
null
} catch (ex: MalformedURLException) {
log.warn("No valid URL!")
null
}
}
......@@ -71,13 +79,18 @@ class PreviewImageHandler(private val sftpClient: SftpClient) {
* @return Path to local file
*/
fun getFromYoutube(videoURL: String): String? {
val url = URL(videoURL)
val id = if (url.host.endsWith("youtube.com")) {
URL(videoURL).query.split("&").firstOrNull {
it.startsWith("v=")
}?.substring(2)
} else {
url.path.substring(1)
val id = try {
val url = URL(videoURL)
if (url.host.endsWith("youtube.com")) {
URL(videoURL).query.split("&").firstOrNull {
it.startsWith("v=")
}?.substring(2)
} else {
url.path.substring(1)
}
} catch (ex: MalformedURLException) {
log.warn("No valid URL!")
null
}
return if (id != null) {
get("https://img.youtube.com/vi/$id/hqdefault.jpg")
......@@ -95,8 +108,17 @@ class PreviewImageHandler(private val sftpClient: SftpClient) {
* @return Path to local file
*/
fun getFromVimeo(videoURL: String, width: Int): String? {
val id = URL(videoURL).path.split("/").last()
return get("https://i.vimeocdn.com/vimeo/${id}_$width.jpg")
val id = try {
URL(videoURL).path.split("/").last()
} catch (ex: MalformedURLException) {
log.warn("No valid URL!")
null
}
return if (id != null) {
get("https://i.vimeocdn.com/vimeo/${id}_$width.jpg")
} else {
null
}
}
/**
......
......@@ -18,6 +18,12 @@
package org.memobase
import com.beust.klaxon.Klaxon
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.nio.charset.Charset
import java.nio.file.Paths
import java.util.stream.Stream
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.riot.Lang
import org.apache.jena.riot.RDFDataMgr
......@@ -35,12 +41,6 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.memobase.rdf.NS
import org.memobase.testing.EmbeddedSftpServer
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.nio.charset.Charset
import java.nio.file.Paths
import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestKafkaTopology {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment