Skip to content

Commit f53f98d

Browse files
committed
Handle large files.
New Gradle wrapper.
1 parent e1161bb commit f53f98d

File tree

7 files changed

+44
-42
lines changed

7 files changed

+44
-42
lines changed

build.gradle.kts

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
33
val gcsVersion = "2.36.1"
44
val logstashVersion = "7.4"
55
val tokenValidationVersion = "4.1.4"
6+
val googleCloudVersion = "5.3.0"
67

78
repositories {
89
mavenCentral()
@@ -13,30 +14,26 @@ plugins {
1314
kotlin("jvm") version kotlinVersion
1415
kotlin("plugin.spring") version kotlinVersion
1516
id("org.springframework.boot") version "3.2.4"
17+
id("io.spring.dependency-management") version "1.1.5"
1618
idea
1719
}
1820

19-
apply(plugin = "io.spring.dependency-management")
20-
2121
dependencies {
2222
implementation(kotlin("stdlib"))
2323

2424
implementation("org.springframework.boot:spring-boot-starter-web")
2525
implementation("org.springframework.boot:spring-boot-starter-actuator")
2626
implementation("org.springframework.boot:spring-boot-starter-webflux")
2727
implementation("org.springframework.boot:spring-boot-starter-validation")
28+
implementation("org.springframework.boot:spring-boot-starter-validation")
29+
implementation("com.google.cloud:spring-cloud-gcp-starter-storage:$googleCloudVersion")
2830
implementation("org.projectreactor:reactor-spring:1.0.1.RELEASE")
2931

30-
implementation("io.micrometer:micrometer-registry-prometheus")
31-
implementation("io.micrometer:micrometer-tracing-bridge-brave")
32-
3332
implementation("ch.qos.logback:logback-classic")
3433
implementation("net.logstash.logback:logstash-logback-encoder:$logstashVersion")
3534
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
3635
implementation("no.nav.security:token-validation-spring:$tokenValidationVersion")
3736

38-
implementation("com.google.cloud:google-cloud-storage:$gcsVersion")
39-
4037
testImplementation("org.springframework.boot:spring-boot-starter-test") {
4138
exclude(group = "org.junit.vintage", module = "org.junit.vintage")
4239
}

gradle/wrapper/gradle-wrapper.jar

1.99 KB
Binary file not shown.
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
4+
networkTimeout=10000
45
zipStoreBase=GRADLE_USER_HOME
56
zipStorePath=wrapper/dists

gradlew

+14-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
# Darwin, MinGW, and NonStop.
5656
#
5757
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
58+
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5959
# within the Gradle project.
6060
#
6161
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,10 +80,10 @@ do
8080
esac
8181
done
8282

83-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84-
85-
APP_NAME="Gradle"
83+
# This is normally unused
84+
# shellcheck disable=SC2034
8685
APP_BASE_NAME=${0##*/}
86+
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
8787

8888
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
8989
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
@@ -143,12 +143,16 @@ fi
143143
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144144
case $MAX_FD in #(
145145
max*)
146+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
147+
# shellcheck disable=SC3045
146148
MAX_FD=$( ulimit -H -n ) ||
147149
warn "Could not query maximum file descriptor limit"
148150
esac
149151
case $MAX_FD in #(
150152
'' | soft) :;; #(
151153
*)
154+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
155+
# shellcheck disable=SC3045
152156
ulimit -n "$MAX_FD" ||
153157
warn "Could not set maximum file descriptor limit to $MAX_FD"
154158
esac
@@ -205,6 +209,12 @@ set -- \
205209
org.gradle.wrapper.GradleWrapperMain \
206210
"$@"
207211

212+
# Stop when "xargs" is not available.
213+
if ! command -v xargs >/dev/null 2>&1
214+
then
215+
die "xargs is not available"
216+
fi
217+
208218
# Use "xargs" to parse quoted args.
209219
#
210220
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.

gradlew.bat

+9-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@rem limitations under the License.
1515
@rem
1616

17-
@if "%DEBUG%" == "" @echo off
17+
@if "%DEBUG%"=="" @echo off
1818
@rem ##########################################################################
1919
@rem
2020
@rem Gradle startup script for Windows
@@ -25,7 +25,8 @@
2525
if "%OS%"=="Windows_NT" setlocal
2626

2727
set DIRNAME=%~dp0
28-
if "%DIRNAME%" == "" set DIRNAME=.
28+
if "%DIRNAME%"=="" set DIRNAME=.
29+
@rem This is normally unused
2930
set APP_BASE_NAME=%~n0
3031
set APP_HOME=%DIRNAME%
3132

@@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
4041

4142
set JAVA_EXE=java.exe
4243
%JAVA_EXE% -version >NUL 2>&1
43-
if "%ERRORLEVEL%" == "0" goto execute
44+
if %ERRORLEVEL% equ 0 goto execute
4445

4546
echo.
4647
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
7576

7677
:end
7778
@rem End local scope for the variables with windows NT shell
78-
if "%ERRORLEVEL%"=="0" goto mainEnd
79+
if %ERRORLEVEL% equ 0 goto mainEnd
7980

8081
:fail
8182
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
8283
rem the _cmd.exe /c_ return code!
83-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84-
exit /b 1
84+
set EXIT_CODE=%ERRORLEVEL%
85+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87+
exit /b %EXIT_CODE%
8588

8689
:mainEnd
8790
if "%OS%"=="Windows_NT" endlocal

src/main/kotlin/no/nav/klage/controller/DocumentController.kt

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package no.nav.klage.controller
22

33

4+
import jakarta.servlet.http.HttpServletResponse
45
import no.nav.klage.getLogger
56
import no.nav.klage.service.DocumentService
67
import no.nav.security.token.support.core.api.ProtectedWithClaims
7-
import org.springframework.core.io.FileSystemResource
8-
import org.springframework.core.io.Resource
8+
import org.springframework.http.HttpHeaders
99
import org.springframework.http.HttpStatus
1010
import org.springframework.http.MediaType
1111
import org.springframework.http.ResponseEntity
1212
import org.springframework.web.bind.annotation.*
1313
import org.springframework.web.multipart.MultipartFile
14-
import java.io.FileNotFoundException
15-
import java.nio.file.Path
1614

1715
@RestController
1816
@ProtectedWithClaims(issuer = "azuread")
@@ -25,19 +23,16 @@ class KabalController(private val documentService: DocumentService) {
2523
}
2624

2725
@GetMapping("{id}")
28-
fun getDocument(@PathVariable("id") id: String): ResponseEntity<Resource> {
26+
fun getDocument(
27+
@PathVariable("id") id: String,
28+
response: HttpServletResponse,
29+
) {
2930
logger.debug("Get document requested with id {}", id)
30-
var path: Path? = null
31-
return try {
32-
path = documentService.getDocumentAsPath(id)
33-
ResponseEntity.ok()
34-
.contentType(MediaType.APPLICATION_OCTET_STREAM)
35-
.body(FileSystemResource(path))
36-
} catch (fnfe: FileNotFoundException) {
37-
ResponseEntity.notFound().build()
38-
} finally {
39-
path?.toFile()?.delete()
40-
}
31+
32+
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=file.pdf")
33+
response.contentType = MediaType.APPLICATION_PDF_VALUE
34+
35+
documentService.getDocumentAsBlob(id).downloadTo(response.outputStream)
4136
}
4237

4338
@PostMapping

src/main/kotlin/no/nav/klage/service/DocumentService.kt

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package no.nav.klage.service
22

3+
import com.google.cloud.storage.Blob
34
import com.google.cloud.storage.BlobId
45
import com.google.cloud.storage.BlobInfo
56
import com.google.cloud.storage.StorageOptions
@@ -8,8 +9,6 @@ import org.springframework.beans.factory.annotation.Value
89
import org.springframework.stereotype.Service
910
import org.springframework.web.multipart.MultipartFile
1011
import java.io.FileNotFoundException
11-
import java.nio.file.Files
12-
import java.nio.file.Path
1312
import java.util.*
1413

1514
@Service
@@ -23,20 +22,17 @@ class DocumentService {
2322
@Value("\${GCS_BUCKET}")
2423
private lateinit var bucket: String
2524

26-
fun getDocumentAsPath(id: String): Path {
25+
fun getDocumentAsBlob(id: String): Blob {
2726
logger.debug("Getting document with id {}", id)
2827

2928
val blob = getGcsStorage().get(bucket, id.toPath())
29+
3030
if (blob == null || !blob.exists()) {
3131
logger.warn("Document not found: {}", id)
3232
throw FileNotFoundException()
3333
}
3434

35-
val pathToTmpFile = Files.createTempFile(null, null)
36-
37-
blob.downloadTo(pathToTmpFile)
38-
39-
return pathToTmpFile
35+
return blob
4036
}
4137

4238
fun deleteDocument(id: String): Boolean {

0 commit comments

Comments
 (0)