Skip to content

Commit 1e005a2

Browse files
Fixed use cases
1 parent bb17292 commit 1e005a2

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

Demos/Ktor/src/main/kotlin/com/groupdocs/ui/conversion/ktor/di/ModulesInjection.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ object ModulesInjection {
2828
}
2929
val usecaseBeans = module {
3030
singleOf(::GetLocalFilesUseCase)
31-
singleOf(::AreFilesSupportedUseCase)
3231
singleOf(::SaveStreamToFilesDirectoryUseCase)
3332
}
3433
val managerBeans = module {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.groupdocs.ui.conversion.ktor.usecase
2+
3+
import com.groupdocs.ui.conversion.ktor.status.InternalServerException
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.withContext
6+
import org.koin.core.component.KoinComponent
7+
import java.nio.file.Files
8+
import java.nio.file.Path
9+
10+
class GetLocalFilesUseCase : KoinComponent {
11+
suspend operator fun invoke(directory: Path): List<LocalStorageEntry> = withContext(Dispatchers.IO) {
12+
if (Files.notExists(directory)) {
13+
throw GetLocalFilesException("Directory does not exist: ${directory.fileName}")
14+
}
15+
try {
16+
val entries = mutableListOf<LocalStorageEntry>()
17+
Files.newDirectoryStream(directory).use { directoryStream ->
18+
directoryStream.forEach { path ->
19+
val entry: LocalStorageEntry = when (Files.isDirectory(path)) {
20+
true -> {
21+
LocalStorageEntry.Directory(
22+
name = path.fileName.toString(),
23+
parentPath = directory
24+
)
25+
}
26+
else -> {
27+
LocalStorageEntry.File(
28+
name = path.fileName.toString(),
29+
parentPath = directory,
30+
size = Files.size(path)
31+
)
32+
}
33+
}
34+
entries.add(entry)
35+
}
36+
}
37+
entries
38+
} catch (e: Exception) {
39+
throw GetLocalFilesException("can't get content of $directory", e)
40+
}
41+
}
42+
}
43+
44+
sealed class LocalStorageEntry(val name: String, val parentPath: Path) {
45+
class File(name: String, val size: Long, parentPath: Path) : LocalStorageEntry(name, parentPath)
46+
class Directory(name: String, parentPath: Path) : LocalStorageEntry(name, parentPath)
47+
48+
val fullPath get() = parentPath.resolve(name)
49+
}
50+
51+
class GetLocalFilesException(message: String, e: Throwable? = null) : InternalServerException(message, e)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.groupdocs.ui.conversion.ktor.usecase
2+
3+
import com.groupdocs.ui.conversion.ktor.manager.PathManager
4+
import com.groupdocs.ui.conversion.ktor.status.InternalServerException
5+
import org.koin.core.component.KoinComponent
6+
import java.io.BufferedOutputStream
7+
import java.io.FileOutputStream
8+
import java.io.InputStream
9+
import java.nio.file.Files
10+
11+
class SaveStreamToFilesDirectoryUseCase(
12+
private val pathManager: PathManager
13+
) : KoinComponent {
14+
operator fun invoke(fileName: String, inputStream: InputStream, rewrite: Boolean) {
15+
val filePathAsString = pathManager.filesDirectory.resolve(fileName).toAbsolutePath().normalize().toString()
16+
val filePath = pathManager.assertPathIsInsideFilesDirectory(filePathAsString)
17+
18+
if (Files.exists(filePath) && !rewrite) {
19+
throw SaveStreamToFilesDirectoryUseCaseException("File is already exist and rewriting is not allowed!")
20+
}
21+
BufferedOutputStream(FileOutputStream(filePath.toFile())).use { outputStream ->
22+
inputStream.copyTo(outputStream)
23+
}
24+
}
25+
26+
}
27+
28+
class SaveStreamToFilesDirectoryUseCaseException(message: String, e: Throwable? = null) : InternalServerException(message, e)

0 commit comments

Comments
 (0)