Skip to content

Commit

Permalink
use blocks with transferTo (#1157)
Browse files Browse the repository at this point in the history
transfer blocks do not close streams, so replace with use blocks
  • Loading branch information
jsarabia authored Jul 8, 2024
1 parent 5e82a9b commit 25378d3
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ class InitializeArtwork @Inject constructor(
}
}

private fun copyBibleArtworkContainers(artwork: List<String> = listOf("en_art_wa.zip", "en_art_sp.zip")) {
private fun copyBibleArtworkContainers(
artwork: List<String> = listOf("en_art_wa.zip", "en_art_sp.zip")
) {
for (art in artwork) {
if (!File(directoryProvider.resourceContainerDirectory, art).exists()) {
log.info("Copying bible artwork")
ClassLoader.getSystemResourceAsStream("content/$art")
.transferTo(
ClassLoader
.getSystemResourceAsStream("content/$art").use { ifs ->
File(
directoryProvider.resourceContainerDirectory.absolutePath,
art
).outputStream()
)
).outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
} else {
log.info("Artwork not initialized but $art exists in rc directory")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ class InitializeMarker @Inject constructor(
private fun importOtterMarker(): Completable {
val pluginsDir = directoryProvider.audioPluginDirectory
val jar = File(pluginsDir, "OratureMarker.jar")
ClassLoader.getSystemResourceAsStream("plugins/jars/markerapp")
?.transferTo(FileOutputStream(jar))
ClassLoader
.getSystemResourceAsStream("plugins/jars/markerapp")
?.use { ifs ->
FileOutputStream(jar).use { ofs ->
ifs.transferTo(ofs)
}
}
return pluginRepository.insert(
AudioPluginData(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class InitializePlugins @Inject constructor(
private fun copyOcenaudioPlugin() {
if (!File(directoryProvider.audioPluginDirectory, "ocenaudio.yaml").exists()) {
log.info("Copying ocenaudio plugin")
ClassLoader.getSystemResourceAsStream("plugins/ocenaudio.yaml")
.transferTo(
ClassLoader
.getSystemResourceAsStream("plugins/ocenaudio.yaml")?.use { ifs ->
File(
directoryProvider.audioPluginDirectory.absolutePath,
"ocenaudio.yaml"
).outputStream()
)
).outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
} else {
log.info("Ocenaudio plugin not initialized but ocenaudio.yaml exists in plugins directory")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ class InitializeRecorder @Inject constructor(
private fun importOtterRecorder(): Completable {
val pluginsDir = directoryProvider.audioPluginDirectory
val jar = File(pluginsDir, "OratureRecorder.jar")
ClassLoader.getSystemResourceAsStream("plugins/jars/recorderapp")
?.transferTo(FileOutputStream(jar))
ClassLoader
.getSystemResourceAsStream("plugins/jars/recorderapp")
?.use { ifs ->
FileOutputStream(jar).use { ofs ->
ifs.transferTo(ofs)
}
}
return pluginRepository.insert(
AudioPluginData(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class InitializeVersification @Inject constructor(
directoryProvider.versificationDirectory.listFiles()?.forEach { file ->
if (file.extension == "json") {
logger.info("Inserting versification: ${file.name}")
versificationRepository.insertVersification(file.nameWithoutExtension, file).blockingAwait()
versificationRepository.insertVersification(file.nameWithoutExtension, file)
.blockingAwait()
}
}
}.subscribeOn(Schedulers.io())
Expand All @@ -60,20 +61,24 @@ class InitializeVersification @Inject constructor(
if (!File(directoryProvider.versificationDirectory, ULB_VERSIFICATION_FILE).exists()) {
directoryProvider.versificationDirectory.mkdirs()
logger.info("Copying ulb versification")
ClassLoader.getSystemResourceAsStream(ULB_VERSIFICATION_RESOURCE_PATH)
.transferTo(
ClassLoader
.getSystemResourceAsStream(ULB_VERSIFICATION_RESOURCE_PATH)?.use { ifs ->
File(
directoryProvider.versificationDirectory.absolutePath,
ULB_VERSIFICATION_FILE
).outputStream()
)
ClassLoader.getSystemResourceAsStream(ULB_VERSIFICATION_RESOURCE_PATH)
.transferTo(
).outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
ClassLoader
.getSystemResourceAsStream(ULB_VERSIFICATION_RESOURCE_PATH)?.use { ifs ->
File(
directoryProvider.versificationDirectory.absolutePath,
UFW_VERSIFICATION_FILE
).outputStream()
)
).outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ class ImportProjectUseCase @Inject constructor() {
it.import(file, callback, options)
}
.onErrorReturn {
logger.error("Failed to import project file: $file. See exception detail below.", it)
logger.error(
"Failed to import project file: $file. See exception detail below.",
it
)
ImportResult.FAILED
}
}
Expand All @@ -97,7 +100,12 @@ class ImportProjectUseCase @Inject constructor() {
getEmbeddedSource(language)
}
.subscribeOn(Schedulers.io())
.doOnError { logger.error("Failed to get embedded source file for ${language.slug}", it) }
.doOnError {
logger.error(
"Failed to get embedded source file for ${language.slug}",
it
)
}
.flatMap { sourceFile ->
import(sourceFile, null, null)
}
Expand All @@ -108,13 +116,21 @@ class ImportProjectUseCase @Inject constructor() {
val resourceName = glSources.find { it.languageCode == language.slug }?.name
val pathToSource = SOURCE_PATH_TEMPLATE.format(resourceName)

val sourceFile = javaClass.classLoader.getResource(pathToSource).openStream().use { input ->
val tempFile = File.createTempFile(resourceName, ".zip", directoryProvider.tempDirectory)
tempFile.outputStream().use { output ->
input.transferTo(output)
val sourceFile = javaClass
.classLoader
.getResource(pathToSource)
.openStream()
.use { input ->
val tempFile = File.createTempFile(
resourceName,
".zip",
directoryProvider.tempDirectory
)
tempFile.outputStream().use { output ->
input.transferTo(output)
}
tempFile
}
tempFile
}

return sourceFile
}
Expand All @@ -136,6 +152,7 @@ class ImportProjectUseCase @Inject constructor() {
ProjectFormat.RESOURCE_CONTAINER -> {
rcImporterProvider.get().getSourceMetadata(file)
}

else -> Maybe.empty()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ class SourceProjectExporter @Inject constructor(
rc,
tempFile.outputStream()
)
rc.accessor.write("metadata.json") {
tempFile.inputStream().transferTo(it)
rc.accessor.write("metadata.json") { ofs ->
tempFile.inputStream().use { ifs -> ifs.transferTo(ofs) }
}
tempFile.deleteIfExists()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ abstract class RCImporter(

return Single
.fromCallable {
stream.transferTo(outFile.outputStream())
outFile.outputStream().use { ofs ->
stream.transferTo(ofs)
}
}
.flatMap {
import(outFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ class ProjectFilesAccessor(
val ext = it.substringAfterLast(".")
when (dir) {
RcConstants.SOURCE_DIR -> OratureFileFormat.isSupported(ext)
RcConstants.SOURCE_AUDIO_DIR -> AudioFileFormat.isSupported (ext) || AudioMetadataFileFormat.isSupported(ext)
RcConstants.SOURCE_AUDIO_DIR -> AudioFileFormat.isSupported(ext) || AudioMetadataFileFormat.isSupported(
ext
)

else -> false
}
}
Expand All @@ -196,7 +199,11 @@ class ProjectFilesAccessor(

if (!outFile.exists()) {
val stream = fileReader.stream(path)
stream.transferTo(outFile.outputStream())
stream.use { ifs ->
outFile.outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
}
}
}
Expand Down Expand Up @@ -302,7 +309,11 @@ class ProjectFilesAccessor(
fun copySelectedTakesFile(fileReader: IFileReader) {
val outFile = projectDir.resolve(RcConstants.SELECTED_TAKES_FILE)
if (!outFile.exists()) {
fileReader.stream(RcConstants.SELECTED_TAKES_FILE).transferTo(outFile.outputStream())
fileReader.stream(RcConstants.SELECTED_TAKES_FILE).use { ifs ->
outFile.outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
}
}

Expand Down Expand Up @@ -372,7 +383,7 @@ class ProjectFilesAccessor(
fun writeTakeCheckingStatus(
fileWriter: IFileWriter,
workbook: Workbook,
takeFilter: (String) -> Boolean = { true }
takeFilter: (String) -> Boolean = { true }
): Completable {
return fetchTakes(workbook)
.filter { takeFilter(it.name) }
Expand Down Expand Up @@ -462,7 +473,11 @@ class ProjectFilesAccessor(
}
}

fun getChapterContent(projectSlug: String, chapterNumber: Int, showVerseNumber: Boolean = true): List<Content> {
fun getChapterContent(
projectSlug: String,
chapterNumber: Int,
showVerseNumber: Boolean = true
): List<Content> {
val chapterContent = arrayListOf<Content>()

ResourceContainer.load(sourceMetadata.path).use { rc ->
Expand Down Expand Up @@ -493,7 +508,7 @@ class ProjectFilesAccessor(
chapterContent.add(content)

// the rest of bridged verses should be marked bridged
for (i in vm.startingVerse+1..vm.endingVerse) {
for (i in vm.startingVerse + 1..vm.endingVerse) {
chapterContent.add(
Content(
sort = chapterContent.size,
Expand All @@ -516,7 +531,11 @@ class ProjectFilesAccessor(
return chapterContent
}

fun getChapterText(projectSlug: String, chapterNumber: Int, showVerseNumber: Boolean = true): List<String> {
fun getChapterText(
projectSlug: String,
chapterNumber: Int,
showVerseNumber: Boolean = true
): List<String> {
val chapterText = arrayListOf<String>()

ResourceContainer.load(sourceMetadata.path).use { rc ->
Expand Down Expand Up @@ -559,7 +578,8 @@ class ProjectFilesAccessor(
val chap = chapters.find { it.number == chapterNumber }
chap?.let {
for (i in startVerse..endVerse) {
val verse = it.getChildMarkers(VMarker::class.java).find { it.startingVerse == i }
val verse =
it.getChildMarkers(VMarker::class.java).find { it.startingVerse == i }
verse?.let {
chunkText.add("${it.verseNumber}. ${it.getText()}")
}
Expand Down Expand Up @@ -732,7 +752,10 @@ class ProjectFilesAccessor(
}
}

private fun deletedTakeFilePaths(workbook: Workbook, workbookRepository: IWorkbookRepository): List<String> {
private fun deletedTakeFilePaths(
workbook: Workbook,
workbookRepository: IWorkbookRepository
): List<String> {
val deletedTakes = workbookRepository
.getSoftDeletedTakes(workbook.source)
.blockingGet()
Expand Down Expand Up @@ -803,15 +826,20 @@ class ProjectFilesAccessor(
fun copyChunkFile(fileReader: IFileReader) {
val outFile = projectDir.resolve(RcConstants.CHUNKS_FILE)
if (!outFile.exists() && fileReader.exists(RcConstants.CHUNKS_FILE)) {
fileReader.stream(RcConstants.CHUNKS_FILE).transferTo(outFile.outputStream())
fileReader.stream(RcConstants.CHUNKS_FILE).use { ifs ->
outFile.outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
}
}

fun getProjectMode(): ProjectMode? {
val file = projectDir.resolve(RcConstants.PROJECT_MODE_FILE)
return if (file.exists() && file.length() > 0) {
val mapper = ObjectMapper(JsonFactory()).registerKotlinModule()
val serialized: SerializableProjectMode = mapper.readValue(file, object : TypeReference<SerializableProjectMode>() {})
val serialized: SerializableProjectMode =
mapper.readValue(file, object : TypeReference<SerializableProjectMode>() {})
serialized.mode
} else {
null
Expand All @@ -836,7 +864,11 @@ class ProjectFilesAccessor(
fun copyProjectModeFile(fileReader: IFileReader) {
val modeFile = projectDir.resolve(RcConstants.PROJECT_MODE_FILE)
if (fileReader.exists(RcConstants.PROJECT_MODE_FILE)) {
fileReader.stream(RcConstants.PROJECT_MODE_FILE).transferTo(modeFile.outputStream())
fileReader.stream(RcConstants.PROJECT_MODE_FILE).use { ifs ->
modeFile.outputStream().use { ofs ->
ifs.transferTo(ofs)
}
}
}
}

Expand Down

0 comments on commit 25378d3

Please sign in to comment.