Skip to content

Commit

Permalink
✨ Support uploading AssignmentReview file
Browse files Browse the repository at this point in the history
Signed-off-by: loheagn <loheagn@icloud.com>
  • Loading branch information
loheagn committed Apr 20, 2022
1 parent a7f1616 commit 8a9ded0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/auth/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fun User.authRead(entity: IEntity): Boolean {
FileType.Assignment -> authRead(Assignment.id(entity.involvedId))
FileType.CourseResource -> authRead(Course.id(entity.involvedId))
FileType.ExperimentResource -> authRead(Experiment.id(entity.involvedId))
FileType.AssignmentReview -> authRead(Assignment.id(entity.involvedId))
}

is PeerAppeal ->
Expand Down Expand Up @@ -70,6 +71,7 @@ fun User.authWrite(entity: IEntity): Boolean {
FileType.Assignment -> authWrite(Assignment.id(entity.involvedId))
FileType.CourseResource -> authWrite(Course.id(entity.involvedId))
FileType.ExperimentResource -> authWrite(Experiment.id(entity.involvedId))
FileType.AssignmentReview -> authWrite(Experiment.id(Assignment.id(entity.involvedId).experimentId))
}

is PeerAppeal ->
Expand Down Expand Up @@ -111,6 +113,7 @@ fun User.authAdmin(entity: IEntity): Boolean {
FileType.Assignment -> authWrite(Assignment.id(entity.involvedId))
FileType.CourseResource -> authWrite(Course.id(entity.involvedId))
FileType.ExperimentResource -> authWrite(Experiment.id(entity.involvedId))
FileType.AssignmentReview -> authWrite(Experiment.id(Assignment.id(entity.involvedId).experimentId))
}

is PeerAppeal ->
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/cn/edu/buaa/scs/model/File.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed interface FileType {
"Assignment" -> Assignment
"CourseResource" -> CourseResource
"ExperimentResource" -> ExperimentResource
"AssignmentReview" -> AssignmentReview
else -> throw BadRequestException("Unknown file type: $name")
}
}
Expand Down Expand Up @@ -55,6 +56,11 @@ sealed interface FileType {
override val name: String
get() = "ExperimentResource"
}

object AssignmentReview : FileType {
override val name: String
get() = "AssignmentReview"
}
}

interface File : Entity<File>, IEntity {
Expand Down
26 changes: 24 additions & 2 deletions src/main/kotlin/cn/edu/buaa/scs/service/Assignment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ class AssignmentService(val call: ApplicationCall) : IService, FileService.IFile
val ApplicationCall.assignmentReview: AssignmentReviewService
get() = AssignmentReviewService.getSvc(this) { AssignmentReviewService(this) }

class AssignmentReviewService(val call: ApplicationCall) : IService {
class AssignmentReviewService(val call: ApplicationCall) : IService, FileService.IFileManageService {

companion object : IService.Caller<AssignmentReviewService>()
companion object : IService.Caller<AssignmentReviewService>() {
private const val bucketName = "scs-reviewed-assignments"
}

fun post(assignmentId: Int, fileId: Int): AssignmentReview {
val assignment = Assignment.id(assignmentId)
Expand All @@ -190,6 +192,26 @@ class AssignmentReviewService(val call: ApplicationCall) : IService {
}
return assignmentReview
}

private val s3 = S3(bucketName)

override fun manager(): S3 {
return s3
}

override fun fixName(originalName: String?, ownerId: String, involvedId: Int): Pair<String, String> {
val subStoreName = "${originalName ?: ""}-${UUID.randomUUID()}.${originalName?.getFileExtension() ?: ""}"
val storeName = "assignment-${involvedId}/$subStoreName"
return Pair(originalName!!, storeName)
}

override fun checkPermission(ownerId: String, involvedId: Int): Boolean {
return true
}

override fun storePath(): String {
return bucketName
}
}

fun Assignment.Companion.id(id: Int): Assignment {
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/cn/edu/buaa/scs/service/File.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FileService(val call: ApplicationCall) : IService {
return "https://scs.buaa.edu.cn/scsos/public/$fileName"
}

interface IFileManageService {
sealed interface IFileManageService {

fun manager(): S3

Expand Down Expand Up @@ -279,6 +279,8 @@ class FileService(val call: ApplicationCall) : IService {
call.user().assertRead(Course.id(involvedId))
FileType.ExperimentResource ->
call.user().assertRead(Experiment.id(involvedId))
FileType.AssignmentReview ->
Unit
}
// get files
val service = fileType.manageService()
Expand Down Expand Up @@ -307,13 +309,15 @@ class FileService(val call: ApplicationCall) : IService {
FileType.Assignment -> call.assignment
FileType.CourseResource -> call.courseResource
FileType.ExperimentResource -> call.experiment
FileType.AssignmentReview -> call.assignmentReview
}

private fun FileType.getInvolvedEntity(involvedId: Int): IEntity =
when (this) {
FileType.Assignment -> Assignment.id(involvedId)
FileType.CourseResource -> Course.id(involvedId)
FileType.ExperimentResource -> Experiment.id(involvedId)
FileType.AssignmentReview -> Assignment.id(involvedId)
}
}

Expand Down

0 comments on commit 8a9ded0

Please sign in to comment.