From 5bd39d1b4e05d4fc042994c53d78a17d88280214 Mon Sep 17 00:00:00 2001 From: thetric Date: Wed, 21 Feb 2018 16:42:20 +0100 Subject: [PATCH] feat: format size in download info --- .../cli/sync/ItemDownloadingItemVisitor.kt | 93 ++++++++++--------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/com/github/thetric/iliasdownloader/cli/sync/ItemDownloadingItemVisitor.kt b/src/main/kotlin/com/github/thetric/iliasdownloader/cli/sync/ItemDownloadingItemVisitor.kt index a74b0b1..bfb0de5 100644 --- a/src/main/kotlin/com/github/thetric/iliasdownloader/cli/sync/ItemDownloadingItemVisitor.kt +++ b/src/main/kotlin/com/github/thetric/iliasdownloader/cli/sync/ItemDownloadingItemVisitor.kt @@ -10,13 +10,13 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.StandardCopyOption import java.nio.file.attribute.FileTime +import java.text.NumberFormat import java.time.LocalDateTime import java.time.ZoneId -import java.util.ArrayList -import java.util.Objects +import java.util.* private val SYSTEM_ZONE = ZoneId.systemDefault() -private val BYTES_PER_MEBIBYTE: Long = 1_048_576 +private const val BYTES_PER_MEBIBYTE: Long = 1_048_576 private val log = KotlinLogging.logger {} /** @@ -27,14 +27,24 @@ class ItemDownloadingItemVisitor( private val iliasService: IliasService, maxFileSizeInMiB: Long ) : IliasItemVisitor { - private val downloadSizeLimitInBytes: Long + private val downloadSizeLimitInBytes: Long = if (maxFileSizeInMiB > 0) { + toBytes(maxFileSizeInMiB) + } else java.lang.Long.MAX_VALUE - init { - this.downloadSizeLimitInBytes = if (maxFileSizeInMiB > 0) toBytes(maxFileSizeInMiB) else java.lang.Long.MAX_VALUE + override fun handleFolder(folder: CourseFolder): IliasItemVisitor.VisitResult { + log.debug { "Found folder \'${folder.name}\'" } + return IliasItemVisitor.VisitResult.CONTINUE } - private fun toBytes(maxFileSizeInMiB: Long): Long { - return (maxFileSizeInMiB * BYTES_PER_MEBIBYTE) + override fun handleFile(file: CourseFile): IliasItemVisitor.VisitResult { + log.debug { "Found file ${file.name}" } + val filePath = resolvePathAndCreateMissingDirs(file) + if (needsToSync(filePath, file)) { + log.info { "Downloading file \'${file.name}\' (${formatBytes(file.size)} Bytes)" } + syncAndSaveFile(filePath, file) + log.info { "Saved to ${filePath.toUri()}" } + } + return IliasItemVisitor.VisitResult.CONTINUE } private fun resolvePathAndCreateMissingDirs(iliasItem: IliasItem): Path { @@ -43,7 +53,6 @@ class ItemDownloadingItemVisitor( return parentItemPath.resolve(sanitizeFileName(iliasItem.name)) } - // @Memoized(maxCacheSize = 5) private fun resolvePathOfParent(parentItem: IliasItem?): Path { if (parentItem == null) { return basePath @@ -64,55 +73,49 @@ class ItemDownloadingItemVisitor( return itemPath } - /** - * Removes all illegal characters from the given file name that are forbidden under Windows with NTFS. - * For more information see [Wikipedia](https://en.wikipedia.org/wiki/NTFS). - * - * @param fileName - * @return file name without invalid NTFS characters - */ - private fun sanitizeFileName(fileName: String): String { - return fileName.replace("""[\\/:*?"<>|]""".toRegex(), "") - } - - override fun handleFolder(folder: CourseFolder): IliasItemVisitor.VisitResult { - log.debug { "Found folder \'${folder.name}\'" } - return IliasItemVisitor.VisitResult.CONTINUE - } - override fun handleFile(file: CourseFile): IliasItemVisitor.VisitResult { - log.debug { "Found file ${file.name}" } - val filePath = resolvePathAndCreateMissingDirs(file) - if (needsToSync(filePath, file)) { - log.info { "Downloading file \'${file.name}\' (${file.size} Bytes)" } - syncAndSaveFile(filePath, file) - log.info { "Saved to ${filePath.toUri()}" } - } - return IliasItemVisitor.VisitResult.CONTINUE - } private fun needsToSync(path: Path, file: CourseFile): Boolean { return (Files.notExists(path) || isFileModified(path, file)) && isUnderFileLimit(file) } - private fun isFileModified(path: Path, file: CourseFile): Boolean { - val lastModifiedTime = Files.getLastModifiedTime(path) - return lastModifiedTime != toFileTime(file.modified) - } - private fun isUnderFileLimit(file: CourseFile): Boolean { return file.size < downloadSizeLimitInBytes } private fun syncAndSaveFile(path: Path, file: CourseFile) { - val inputStream = iliasService.getContentAsStream(file) - Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING) + iliasService.getContentAsStream(file).use { + Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING) + } Files.setLastModifiedTime(path, toFileTime(file.modified)) } +} +/** + * Removes all illegal characters from the given file name that are forbidden under Windows with NTFS. + * For more information see [Wikipedia](https://en.wikipedia.org/wiki/NTFS). + * + * @param fileName + * @return file name without invalid NTFS characters + */ +private fun sanitizeFileName(fileName: String): String { + return fileName.replace("""[\\/:*?"<>|]""".toRegex(), "") +} - private fun toFileTime(dateTime: LocalDateTime): FileTime { - Objects.requireNonNull(dateTime, "dateTime") - return FileTime.from(dateTime.toInstant(SYSTEM_ZONE.rules.getOffset(dateTime))) - } +private fun toFileTime(dateTime: LocalDateTime): FileTime { + Objects.requireNonNull(dateTime, "dateTime") + return FileTime.from(dateTime.toInstant(SYSTEM_ZONE.rules.getOffset(dateTime))) +} + +private fun toBytes(maxFileSizeInMiB: Long): Long { + return (maxFileSizeInMiB * BYTES_PER_MEBIBYTE) +} + +private fun isFileModified(path: Path, file: CourseFile): Boolean { + val lastModifiedTime = Files.getLastModifiedTime(path) + return lastModifiedTime != toFileTime(file.modified) +} + +private fun formatBytes(bytes: Int): String { + return NumberFormat.getIntegerInstance().format(bytes) }