Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Deployment/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ http {

location /storage {
proxy_pass http://docker-storage;
rewrite ^/storage/(.*)$ $1 break;
rewrite ^/storage/(.*)$ /$1 break;
}

location /api {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import org.springframework.context.annotation.ComponentScan
@SpringBootApplication
@ComponentScan("co.nilin.opex")
@EnableOpexErrorHandler
class AccountantApp
class StorageApp

fun main(args: Array<String>) {
runApplication<AccountantApp>(*args)
runApplication<StorageApp>(*args)
}
Original file line number Diff line number Diff line change
@@ -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<String>

@Value("\${app.cors.allowed-patterns}")
private lateinit var patterns: Array<String>

override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**")
.allowedOrigins(*hosts)
.allowedOriginPatterns(*patterns)
.allowedHeaders("*")
.allowedMethods("*")
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,16 +21,17 @@ class FileController(private val storageService: StorageService) {
@PathVariable("uid") uid: String,
@RequestPart("file") file: Mono<FilePart>,
@CurrentSecurityContext securityContext: SecurityContext
): ResponseEntity<String> {
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)
}
}

Expand All @@ -39,7 +42,7 @@ class FileController(private val storageService: StorageService) {
@PathVariable("filename") filename: String,
@CurrentSecurityContext securityContext: SecurityContext
): ResponseEntity<ByteArray> {
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())
Expand Down
3 changes: 3 additions & 0 deletions Storage/storage-app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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