Skip to content

Commit

Permalink
Update AddModuleWatermark
Browse files Browse the repository at this point in the history
  • Loading branch information
jaakkonakaza committed Aug 20, 2024
1 parent 1392796 commit 71e9214
Showing 1 changed file with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,63 @@ import com.intellij.openapi.project.Project
import fi.aalto.cs.apluscourses.model.component.Module
import fi.aalto.cs.apluscourses.model.people.User
import fi.aalto.cs.apluscourses.services.course.CourseManager
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import java.io.IOException
import java.io.UncheckedIOException
import java.nio.file.Files
import java.nio.file.Path
import java.security.MessageDigest
import java.util.Locale
import java.util.function.Supplier
import java.util.stream.Collectors

object AddModuleWatermark {
private const val ENCODING_LINE = "# -*- coding: utf-8 -*-"
private const val ENCODING_LINE_WITH_UNICODE =
"#\u200b\u200c\u200b\u200b\u200b\u200b\u200c\u200c\u200b\u200c\u200b\u200c\u200c\u200b *-* coding: utf-8 *-*"

private fun createWatermark(userId: Long): String {
return java.lang.Long.toBinaryString(userId)
.replace('1', '\u200b')
.replace('0', '\u200c')
}

@Throws(IOException::class)
private fun addWatermark(path: Path, user: User?) {
val userId = user?.aplusId ?: 0
val studentId = user?.studentId ?: "---"
val studentName = user?.userName ?: "---"

val watermark = createWatermark(userId)
val newLines = Files.lines(path).use { lineStream ->
lineStream.filter { line ->
line != ENCODING_LINE && line != ENCODING_LINE_WITH_UNICODE
}.collect(Collectors.toCollection { mutableListOf<String>() })
}

val date = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())

Files.lines(path).use { lineStream ->
val newLinesMutable = lineStream.map<String> { line: String ->
if (line == ENCODING_LINE || line == ENCODING_LINE_WITH_UNICODE) {
return@map ""
} else if (line.trim { it <= ' ' }.startsWith("#")) {
return@map line.replaceFirst("#".toRegex(), "#$watermark")
}
line
}.collect(
Collectors.toCollection<String, java.util.ArrayList<String>>(
Supplier<java.util.ArrayList<String>> { ArrayList() })
newLines.addAll(
0, listOf(
ENCODING_LINE,
"# Nimi: $studentName",
"# Opiskelijanumero: $studentId",
"# Data Structures and Algorithms Y CS-A1141",
"# Date: ${date.dayOfMonth}/${date.monthNumber}/${date.year}"
)
newLinesMutable.add(0, ENCODING_LINE)
newLinesMutable.add(1, "# Nimi: $studentName")
newLinesMutable.add(2, "# Opiskelijanumero: $studentId")
Files.write(path, newLinesMutable)
}
)

val hash = generateHash(userId.toString())

newLines.addAll(
listOf(
"",
"# Do not remove or modify this",
"# Unique hash: $hash"
)
)

Files.write(path, newLines)
}

private fun generateHash(input: String): String {
val md = MessageDigest.getInstance("SHA-256")
val bytes = md.digest(input.toByteArray())
return bytes.joinToString("") { "%02x".format(it) }
}

/**
Expand Down

0 comments on commit 71e9214

Please sign in to comment.