Skip to content

Commit

Permalink
feat: format size in download info
Browse files Browse the repository at this point in the history
  • Loading branch information
thetric committed Feb 21, 2018
1 parent e4c91fb commit 5bd39d1
Showing 1 changed file with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

/**
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 5bd39d1

Please sign in to comment.