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
3 changes: 3 additions & 0 deletions Deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ services:
- JAVA_OPTS=-Xmx256m -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
- SPRING_PROFILES_DEFAULT=docker
- CONSUL_HOST=consul
- ROOT_DIR=/storage
volumes:
- $DATA/storage-data:/storage
depends_on:
- consul
deploy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.nilin.opex.storage.app.service.StringToHashService
import co.nilin.opex.utility.error.data.OpexError
import co.nilin.opex.utility.error.data.OpexException
import kotlinx.coroutines.reactive.awaitFirstOrNull
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.http.codec.multipart.FilePart
Expand All @@ -17,27 +18,31 @@ import java.nio.file.Paths
import java.util.*

@RestController
class FileController(private val storageService: StorageService, private val stringToHashService: StringToHashService) {
class FileController(
private val storageService: StorageService,
private val stringToHashService: StringToHashService,
@Value("\${app.root-dir}") private val rootDir: String
) {
data class FileUploadResponse(val path: String)

private suspend fun upload(uid: String, file: FilePart?, nameWithoutExtension: String? = null): FileUploadResponse {
if (file == null) throw OpexException(OpexError.BadRequest, "File Not Provided")
val filename = file.filename()
val ext = filename.replace(Regex(".+(?<=\\.)"), "")
if (ext.toLowerCase() !in listOf("jpg", "jpeg", "png", "mp4", "mov", "pdf", "gif"))
if (ext.toLowerCase() !in listOf("jpg", "jpeg", "png", "mp4", "mov"))
throw OpexException(OpexError.BadRequest, "Invalid File Format")
val uri = if (nameWithoutExtension == null) {
"$uid/$filename"
} else {
"$uid/$nameWithoutExtension.$ext"
}
val path = Paths.get("").resolve("/opex-storage/$uri").toString()
val path = Paths.get("").resolve("$rootDir/$uri").toString()
storageService.store(path, file)
return FileUploadResponse("/$uri")
}

private suspend fun download(uid: String, filename: String? = null): ResponseEntity<ByteArray> {
val path = Paths.get("").resolve("/opex-storage/$uid/$filename")
val path = Paths.get("").resolve("$rootDir/$uid/$filename")
if (!storageService.exists(path.toString())) throw OpexException(OpexError.NotFound)
val file = storageService.load(path.toString())
val mimeType = URLConnection.getFileNameMap().getContentTypeFor(path.fileName.toString())
Expand Down
1 change: 1 addition & 0 deletions storage/storage-app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ spring:
prefer-ip-address: true

app:
root-dir: ${ROOT_DIR}
cors:
allowed-hosts: https://opex.dev, http://localhost:3000
allowed-patterns: http://192.168.*
Expand Down