Skip to content

Commit

Permalink
controller: AvatarController: Fix url generation for prod
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Verma <[email protected]>
  • Loading branch information
shank03 committed Oct 11, 2023
1 parent 0ef2dc6 commit 76f2607
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/kotlin/com/mnnit/moticlubs/controller/AvatarController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.mnnit.moticlubs.web.security.PathAuthorization
import io.swagger.v3.oas.annotations.Hidden
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.core.env.Environment
import org.springframework.core.io.UrlResource
import org.springframework.core.io.buffer.DataBuffer
import org.springframework.core.io.buffer.DataBufferUtils
Expand Down Expand Up @@ -126,11 +127,12 @@ class AvatarController(
@Hidden
fun updateUserAvatar(
@RequestPart("file") filePart: Mono<FilePart>,
environment: Environment,
serverRequest: ServerHttpRequest,
): Mono<ResponseEntity<User>> = pathAuthorization
.userAuthorization()
.zipWith(filePart)
.saveImage(serverRequest, ImageType.USER) { uid, url -> userService.updateAvatar(uid, url) }
.saveImage(serverRequest, environment, ImageType.USER) { uid, url -> userService.updateAvatar(uid, url) }
.invalidateStamp {
ResponseStamp.ADMIN.invalidateStamp()
ResponseStamp.USER.withKey("${it.uid}")
Expand All @@ -143,12 +145,13 @@ class AvatarController(
fun updateClubAvatar(
@RequestPart("file") filePart: Mono<FilePart>,
@RequestParam clubId: Long,
environment: Environment,
serverRequest: ServerHttpRequest,
): Mono<ResponseEntity<Club>> = pathAuthorization
.clubAuthorization(clubId)
.map { clubId }
.zipWith(filePart)
.saveImage(serverRequest, ImageType.CLUB) { cid, url -> clubService.updateAvatar(cid, url) }
.saveImage(serverRequest, environment, ImageType.CLUB) { cid, url -> clubService.updateAvatar(cid, url) }
.invalidateStamp { ResponseStamp.CLUB }
.wrapError()

Expand All @@ -158,17 +161,19 @@ class AvatarController(
fun addPostImage(
@RequestPart("file") filePart: Mono<FilePart>,
@RequestParam clubId: Long,
environment: Environment,
serverRequest: ServerHttpRequest,
): Mono<ResponseEntity<ImageUrlDTO>> = pathAuthorization
.clubAuthorization(clubId)
.map { System.currentTimeMillis() }
.zipWith(filePart)
.saveImage(serverRequest, ImageType.POST) { _, url -> Mono.just(ImageUrlDTO(url)) }
.saveImage(serverRequest, environment, ImageType.POST) { _, url -> Mono.just(ImageUrlDTO(url)) }
.invalidateStamp { ResponseStamp.NONE }
.wrapError()

private fun <T> Mono<Tuple2<Long, FilePart>>.saveImage(
serverRequest: ServerHttpRequest,
environment: Environment,
imageType: ImageType,
then: (Long, String) -> Mono<T>,
): Mono<T> = flatMap { tuple ->
Expand All @@ -181,13 +186,17 @@ class AvatarController(
}

val fileName = id.getFileName()
val rawUrl = "${serverRequest.uri.toURL()}"
val rawUrl = if (environment.activeProfiles.contains("prod")) {
"http://localhost:8002/api/v1/avatar/"
} else {
"https://sac.mnnit.ac.in/moticlubs/api/v1/avatar/"
}

val hashPrefix = id.encodeToUrl()
val (path, url) = when (imageType) {
ImageType.USER -> Pair(USER_AVATAR_PATH, rawUrl.replace("user.*".toRegex(), "g/user/$hashPrefix"))
ImageType.CLUB -> Pair(CLUB_AVATAR_PATH, rawUrl.replace("club.*".toRegex(), "g/club/$hashPrefix"))
ImageType.POST -> Pair(POST_AVATAR_PATH, rawUrl.replace("post.*".toRegex(), "g/post/$hashPrefix"))
ImageType.USER -> Pair(USER_AVATAR_PATH, "$rawUrl/g/user/$hashPrefix")
ImageType.CLUB -> Pair(CLUB_AVATAR_PATH, "$rawUrl/g/club/$hashPrefix")
ImageType.POST -> Pair(POST_AVATAR_PATH, "$rawUrl/g/post/$hashPrefix")
}

val folder = File(path)
Expand Down

0 comments on commit 76f2607

Please sign in to comment.