diff --git a/Deployment/nginx.conf b/Deployment/nginx.conf index e5a94cc6e..debca9b7b 100644 --- a/Deployment/nginx.conf +++ b/Deployment/nginx.conf @@ -50,7 +50,7 @@ http { location /storage { proxy_pass http://docker-storage; - rewrite ^/storage/(.*)$ $1 break; + rewrite ^/storage/(.*)$ /$1 break; } location /api { diff --git a/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/StorageApp.kt b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/StorageApp.kt index 5bbe48a4c..f4bf3fd65 100644 --- a/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/StorageApp.kt +++ b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/StorageApp.kt @@ -8,8 +8,8 @@ import org.springframework.context.annotation.ComponentScan @SpringBootApplication @ComponentScan("co.nilin.opex") @EnableOpexErrorHandler -class AccountantApp +class StorageApp fun main(args: Array) { - runApplication(*args) + runApplication(*args) } diff --git a/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/config/CorsConfig.kt b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/config/CorsConfig.kt new file mode 100644 index 000000000..988235929 --- /dev/null +++ b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/config/CorsConfig.kt @@ -0,0 +1,25 @@ +package co.nilin.opex.storage.app.config + +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Configuration +import org.springframework.web.reactive.config.CorsRegistry +import org.springframework.web.reactive.config.WebFluxConfigurer + +@Configuration +class CorsConfig : WebFluxConfigurer { + + @Value("\${app.cors.allowed-hosts}") + private lateinit var hosts: Array + + @Value("\${app.cors.allowed-patterns}") + private lateinit var patterns: Array + + override fun addCorsMappings(registry: CorsRegistry) { + registry.addMapping("/**") + .allowedOrigins(*hosts) + .allowedOriginPatterns(*patterns) + .allowedHeaders("*") + .allowedMethods("*") + } + +} diff --git a/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/controller/FileController.kt b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/controller/FileController.kt index 3ed610b3b..b8f8d6d8d 100644 --- a/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/controller/FileController.kt +++ b/Storage/storage-app/src/main/kotlin/co/nilin/opex/storage/app/controller/FileController.kt @@ -1,6 +1,8 @@ package co.nilin.opex.storage.app.controller import co.nilin.opex.storage.app.service.StorageService +import co.nilin.opex.utility.error.data.OpexError +import co.nilin.opex.utility.error.data.OpexException import kotlinx.coroutines.reactive.awaitFirstOrNull import org.springframework.http.MediaType import org.springframework.http.ResponseEntity @@ -19,16 +21,17 @@ class FileController(private val storageService: StorageService) { @PathVariable("uid") uid: String, @RequestPart("file") file: Mono, @CurrentSecurityContext securityContext: SecurityContext - ): ResponseEntity { - if (securityContext.authentication.name != uid) return ResponseEntity.status(401).build() + ): Any { + if (securityContext.authentication.name != uid) throw OpexException(OpexError.UnAuthorized) file.awaitFirstOrNull().apply { - if (this == null) return ResponseEntity.badRequest().build() + data class Response(val uri: String) + if (this == null) throw OpexException(OpexError.BadRequest, "File Not Provided") val ext = this.filename().replace(Regex(".+(?=\\..+)"), "") - if (ext !in listOf(".jpg", ".png", ".mp4", ".mov")) return ResponseEntity.badRequest() - .body("Invalid File Format") + if (ext !in listOf(".jpg", ".jpeg", ".png", ".mp4", ".mov")) + throw OpexException(OpexError.BadRequest, "Invalid File Format") val path = Paths.get("").resolve("/opex-storage/$uid/${this.filename()}").toString() storageService.store(path, this) - return ResponseEntity.ok().build() + return Response(path) } } @@ -39,7 +42,7 @@ class FileController(private val storageService: StorageService) { @PathVariable("filename") filename: String, @CurrentSecurityContext securityContext: SecurityContext ): ResponseEntity { - if (securityContext.authentication.name != uid) return ResponseEntity.status(401).build() + if (securityContext.authentication.name != uid) throw OpexException(OpexError.UnAuthorized) val path = Paths.get("").resolve("/opex-storage/$uid/$filename") val file = storageService.load(path.toString()) val mimeType = URLConnection.getFileNameMap().getContentTypeFor(path.fileName.toString()) diff --git a/Storage/storage-app/src/main/resources/application.yml b/Storage/storage-app/src/main/resources/application.yml index 8d865aaad..4b6ce48e6 100644 --- a/Storage/storage-app/src/main/resources/application.yml +++ b/Storage/storage-app/src/main/resources/application.yml @@ -22,5 +22,8 @@ spring: prefer-ip-address: true app: + cors: + allowed-hosts: https://opex.dev, http://localhost:3000 + allowed-patterns: http://192.168.* auth: cert-url: lb://opex-auth/auth/realms/opex/protocol/openid-connect/certs