Skip to content

Commit

Permalink
refactor(backend): move FileStorageService to non Reactive API
Browse files Browse the repository at this point in the history
Related to #231
  • Loading branch information
davinkevin committed Aug 3, 2024
1 parent 3a95ca1 commit 8ccafda
Show file tree
Hide file tree
Showing 22 changed files with 362 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CoverService(
cover
.findCoverOlderThan(date)
.asSequence()
.filter { file.coverExists(it.podcast.title, it.item.id, it.extension).hasElement().block()!! }
.forEach { file.deleteCover(it).block() }
.filter { file.coverExists(it.podcast.title, it.item.id, it.extension) != null }
.forEach { file.deleteCover(it) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ItemHandler(
}

private fun findCoverURIOf(item: Item, host: URI): URI {
val coverPath = fileService.coverExists(item).block()
val coverPath = fileService.coverExists(item)
?: return item.cover.url

val fileDescriptor = FileDescriptor(item.podcast.title, coverPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ItemService(
log.info("Deletion of items older than {}", date)
val items = repository.findAllToDelete(date)

items.forEach { file.deleteItem(it).block() }
items.forEach { file.deleteItem(it) }

repository.updateAsDeleted(items.map { it.id })
}
Expand All @@ -58,7 +58,7 @@ class ItemService(
?: return null

if (item.isDownloaded() && item.fileName != Path("")) {
file.deleteItem(DeleteItemRequest(item.id, item.fileName!!, item.podcast.title)).block()
file.deleteItem(DeleteItemRequest(item.id, item.fileName!!, item.podcast.title))
}

return repository.resetById(id)!!
Expand All @@ -77,11 +77,11 @@ class ItemService(
fun upload(podcastId: UUID, filePart: FilePart): Item {
val filename = Paths.get(filePart.filename().replace("[^a-zA-Z0-9.-]".toRegex(), "_"))

val path = file.cache(filePart, filename).block()!!
val path = file.cache(filePart, filename)
val podcast = podcastRepository.findById(podcastId)!!

file.upload(podcast.title, path).block()
val metadata = file.metadata(podcast.title, path).block()!!
file.upload(podcast.title, path)
val metadata = file.metadata(podcast.title, path)!!

val (_, p2, p3) = filePart.filename().split(" - ")
val title = p3.substringBeforeLast(".")
Expand Down Expand Up @@ -122,7 +122,7 @@ class ItemService(
val item = repository.deleteById(itemId)

if (item !== null) {
file.deleteItem(item).block()
file.deleteItem(item)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.github.davinkevin.podcastserver.entity.Status
import com.github.davinkevin.podcastserver.messaging.MessagingTemplate
import com.github.davinkevin.podcastserver.service.storage.FileStorageService
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import java.nio.file.Files
import java.nio.file.Path
import java.time.Clock
Expand Down Expand Up @@ -73,8 +75,11 @@ abstract class AbstractDownloader(
override fun finishDownload() {
itemDownloadManager.removeACurrentDownload(downloadingInformation.item.id)

file.upload(downloadingInformation.item.podcast.title, target)
.then(file.metadata(downloadingInformation.item.podcast.title, target))
val upload = Mono.defer { file.upload(downloadingInformation.item.podcast.title, target).toMono() }
val metadata = Mono.defer { file.metadata(downloadingInformation.item.podcast.title, target).toMono() }

upload
.then(metadata)
.flatMap { (mimeType, size) ->
downloadRepository.finishDownload(
id = downloadingInformation.item.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ class FfmpegDownloader(

val multiDownloads = downloadingInformation.urls.map { download(it.toASCIIString()) }

Result.runCatching {
if (multiDownloads.any { it.isFailure }) {
val cause = multiDownloads.first { it.isFailure }.exceptionOrNull()
throw RuntimeException("Error during download of a part", cause)
}
if (multiDownloads.any { it.isFailure }) {
val cause = multiDownloads.first { it.isFailure }.exceptionOrNull()
throw RuntimeException("Error during download of a part", cause)
}

ffmpegService.concat(
target,
*multiDownloads.map { it.getOrNull()!! }.toTypedArray()
)
runCatching {
ffmpegService.concat(target, *multiDownloads.map { it.getOrNull()!! }.toTypedArray())
}

multiDownloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PodcastHandler(

log.debug("the url of the podcast cover is {}", podcast.cover.url)

val uri = when(val coverPath = fileService.coverExists(podcast).block()) {
val uri = when(val coverPath = fileService.coverExists(podcast)) {
is Path -> fileService.toExternalUrl(FileDescriptor(podcast.title, coverPath), host)
else -> podcast.cover.url
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PodcastService(
cover = cover
)

fileService.downloadPodcastCover(podcast).block()
fileService.downloadPodcastCover(podcast)

return podcast
}
Expand All @@ -70,7 +70,6 @@ class PodcastService(
coverRepository.save(newCover).block()!!.also {
fileService
.downloadPodcastCover(p.copy(cover = Cover(it.id, it.url, it.height, it.width)))
.block()
}
else Cover(oldCover.id, oldCover.url, oldCover.height, oldCover.width)

Expand All @@ -90,7 +89,7 @@ class PodcastService(
from = p.title,
to = updatePodcast.title
)
fileService.movePodcast(movePodcastDetails).block()
fileService.movePodcast(movePodcastDetails)
}

return podcast
Expand All @@ -99,7 +98,7 @@ class PodcastService(
fun deleteById(id: UUID) {
val request = repository.deleteById(id) ?: return

fileService.deletePodcast(request).block()
fileService.deletePodcast(request)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.reactive.ReactorClientHttpConnector
import org.springframework.web.reactive.function.client.WebClient
import reactor.netty.http.client.HttpClient
import org.springframework.web.client.RestClient
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3AsyncClient
Expand All @@ -24,7 +22,7 @@ class FileStorageConfig {

@Bean
fun fileStorageService(
webClientBuilder: WebClient.Builder,
rcb: RestClient.Builder,
properties: StorageProperties,
): FileStorageService {
val s3conf = S3Configuration.builder()
Expand All @@ -38,6 +36,7 @@ class FileStorageConfig {
.serviceConfiguration(s3conf)
.endpointOverride(properties.url)
.region(Region.AWS_GLOBAL)
.asyncConfiguration { }
.build()

val preSignerBuilder = S3Presigner.builder()
Expand All @@ -51,12 +50,8 @@ class FileStorageConfig {

val preSigner = if (properties.isInternal) requestSpecificPreSigner else externalPreSigner

val wcb = webClientBuilder
.clone()
.clientConnector(ReactorClientHttpConnector(HttpClient.create().followRedirect(true)))

return FileStorageService(
wcb = wcb,
rcb = rcb.clone(),
bucket = bucketClient,
preSignerBuilder = preSigner,
properties = properties,
Expand All @@ -65,7 +60,7 @@ class FileStorageConfig {

@Bean
fun createBucket(file: FileStorageService) = CommandLineRunner {
file.initBucket().blockOptional()
file.initBucket()
}
}

Expand Down
Loading

0 comments on commit 8ccafda

Please sign in to comment.