diff --git a/src/main/kotlin/com/mnnit/moticlubs/controller/AvatarController.kt b/src/main/kotlin/com/mnnit/moticlubs/controller/AvatarController.kt index 763b299..3345cd1 100644 --- a/src/main/kotlin/com/mnnit/moticlubs/controller/AvatarController.kt +++ b/src/main/kotlin/com/mnnit/moticlubs/controller/AvatarController.kt @@ -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 @@ -126,11 +127,12 @@ class AvatarController( @Hidden fun updateUserAvatar( @RequestPart("file") filePart: Mono, + environment: Environment, serverRequest: ServerHttpRequest, ): Mono> = 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}") @@ -143,12 +145,13 @@ class AvatarController( fun updateClubAvatar( @RequestPart("file") filePart: Mono, @RequestParam clubId: Long, + environment: Environment, serverRequest: ServerHttpRequest, ): Mono> = 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() @@ -158,17 +161,19 @@ class AvatarController( fun addPostImage( @RequestPart("file") filePart: Mono, @RequestParam clubId: Long, + environment: Environment, serverRequest: ServerHttpRequest, ): Mono> = 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 Mono>.saveImage( serverRequest: ServerHttpRequest, + environment: Environment, imageType: ImageType, then: (Long, String) -> Mono, ): Mono = flatMap { tuple -> @@ -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)