diff --git a/tagify/.github/ISSUE_TEMPLATE/bug-issue.md b/tagify/.github/ISSUE_TEMPLATE/bug-issue.md new file mode 100644 index 0000000..0058f81 --- /dev/null +++ b/tagify/.github/ISSUE_TEMPLATE/bug-issue.md @@ -0,0 +1,22 @@ +--- +name: Bug Issue +about: 'Issue template for bug ' +title: "[BUG]" +labels: bug, feature +assignees: jhl8109 + +--- + +## 어떤 버그인가요? + +> 어떤 버그인지 간결하게 설명해주세요 + +## 어떤 상황에서 발생한 버그인가요? + +> (가능하면) Given-When-Then 형식으로 서술해주세요 + +## 예상 결과 + +> 예상했던 정상적인 결과가 어떤 것이었는지 설명해주세요 + +## 참고할만한 자료(선택) diff --git a/tagify/.github/ISSUE_TEMPLATE/new-feature-issue.md b/tagify/.github/ISSUE_TEMPLATE/new-feature-issue.md new file mode 100644 index 0000000..f16b275 --- /dev/null +++ b/tagify/.github/ISSUE_TEMPLATE/new-feature-issue.md @@ -0,0 +1,20 @@ +--- +name: New Feature Issue +about: Issue template for new feature +title: "[FEAT]" +labels: feature +assignees: jhl8109 + +--- + +## 어떤 기능인가요? + +> 추가하려는 기능에 대해 간결하게 설명해주세요 + +## 작업 상세 내용 + +- [ ] TODO +- [ ] TODO +- [ ] TODO + +## 참고할만한 자료(선택) diff --git a/tagify/.github/PULL_REQUEST_TEMPLATE.md b/tagify/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..435f85d --- /dev/null +++ b/tagify/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,15 @@ +## #️⃣연관된 이슈 + +> ex) #이슈번호, #이슈번호 + +## 📝작업 내용 + +> 이번 PR에서 작업한 내용을 간략히 설명해주세요(이미지 첨부 가능) + +### 스크린샷 (선택) + +## 💬리뷰 요구사항(선택) + +> 리뷰어가 특별히 봐주었으면 하는 부분이 있다면 작성해주세요 +> +> ex) 메서드 XXX의 이름을 더 잘 짓고 싶은데 혹시 좋은 명칭이 있을까요? diff --git a/tagify/.github/workflows/jib.yml b/tagify/.github/workflows/jib.yml new file mode 100644 index 0000000..7a2e006 --- /dev/null +++ b/tagify/.github/workflows/jib.yml @@ -0,0 +1,30 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up JDK 21 + uses: actions/setup-java@v1 + with: + java-version: 21 + + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build and Push Docker Image with Jib + run: ./gradlew jib \ No newline at end of file diff --git a/tagify/.github/workflows/pr-test.yml b/tagify/.github/workflows/pr-test.yml new file mode 100644 index 0000000..72519c4 --- /dev/null +++ b/tagify/.github/workflows/pr-test.yml @@ -0,0 +1,24 @@ +name: PR Test + +on: + pull_request: + branches: [ main ] # master branch에 PR을 보낼 때 실행 + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up JDK 21 + uses: actions/setup-java@v1 + with: + java-version: 21 + + # Gradle wrapper 파일 실행 권한주기 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + # Gradle test를 실행한다 + - name: Test with Gradle + run: ./gradlew --info test \ No newline at end of file diff --git a/tagify/.gitignore b/tagify/.gitignore new file mode 100644 index 0000000..5a979af --- /dev/null +++ b/tagify/.gitignore @@ -0,0 +1,40 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Kotlin ### +.kotlin diff --git a/tagify/README.md b/tagify/README.md new file mode 100644 index 0000000..cbbebd2 --- /dev/null +++ b/tagify/README.md @@ -0,0 +1,22 @@ +# Tagify + +LLM을 기반으로 이미지, pdf 등 파일에 대한 태그를 붙인다. +저장된 태그를 통해 파일을 효율적으로 검색한다. + +## 프로젝트의 목적과 의도 +- TDD를 프로젝트에 적용해본다. 특히, Classist 고전파 스타일로 Mock보다 Pojo 객체를 적극적으로 활용한 테스트 코드를 작성한다. +- AI를 활용해본다. 구체적 모델, 학습, 튜닝에는 의존하지 않으며, AI가 어떻게 서비스에 활용될 수 있는 지 프로세스를 이해하기 위함이다. +- 코틀린 역량을 향상시킨다. 구체적으로 코틀린 언어에 대해 학습하며, 코틀린+스프링 기술을 실습을 통해 체화한다.
+> #### 결과적으로 추구해야할 방향성은 새로운 기술을 용하는 것에 끝나지 않고, 코드 퀄리티 향상에 집중한다. + + +## 예상 기술 스택 +인프라 : AWS (k3s??)
+운영 : git action CI, argoCD
+서버 : Spring Boot 3.x , Kotlin 2.0
+저장소 : Elasticsearch
+ +## 추가 기능 및 향후 방향성 간략 정리 +- [ ] PDF 인식 후 태그 추론 기능을 추가한다. +- [ ] [Core](https://github.com/ZZIBU/Core)에서 개발 중인 파일(이미지 + PDF) 관리 서비스와 통합한다. +- [ ] 이를 통해 파일들을 클라우드 스토리지에 저장할 뿐만 아니라, 태그 기반 파일 검색을 지원한다. diff --git a/tagify/build.gradle.kts b/tagify/build.gradle.kts new file mode 100644 index 0000000..30d0605 --- /dev/null +++ b/tagify/build.gradle.kts @@ -0,0 +1,99 @@ +plugins { + id("org.springframework.boot") version "3.3.0" + id("io.spring.dependency-management") version "1.1.5" + id("com.google.cloud.tools.jib") version "3.4.1" + kotlin("jvm") version "1.9.24" + kotlin("plugin.spring") version "1.9.24" +} + +group = "zzibu.jeho" +version = "0.0.1-SNAPSHOT" +val projectOwner : String by project + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +configurations { + compileOnly { + extendsFrom(configurations.annotationProcessor.get()) + } +} + +repositories { + mavenCentral() + maven { + url = uri("https://repo.spring.io/milestone") + url = uri("https://repo.spring.io/snapshot") + } +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.jetbrains.kotlin:kotlin-reflect") + + // spring ai + implementation("org.springframework.ai:spring-ai-ollama-spring-boot-starter:1.0.0-SNAPSHOT") + + // kotlin-logging + implementation("io.github.oshai:kotlin-logging-jvm:5.1.1") + + // PDFBox + implementation("org.apache.pdfbox:pdfbox:2.0.24") + + // Tesseract (OCR Dependency) + implementation("net.sourceforge.tess4j:tess4j:5.4.0") + + annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") + testImplementation("org.springframework.boot:spring-boot-starter-test") + + // kotest + testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + testImplementation("io.kotest:kotest-runner-junit5:5.4.2") + testImplementation("io.kotest:kotest-assertions-core:5.4.2") + testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.2") + + testRuntimeOnly("org.junit.platform:junit-platform-launcher") + implementation(kotlin("stdlib-jdk8")) +} + +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + } +} + +// Jib 플러그인 설정 +jib { + from { + image = "openjdk:21-jdk-slim" + } + to { + image = "${projectOwner}/${project.name.lowercase()}" + tags = setOf("$version", "latest") + } + container { + labels = mapOf( + "maintainer" to "Jeho Lee ", + "org.opencontainers.image.title" to "tagify", + "org.opencontainers.image.description" to "Tagging for all of your files", + "org.opencontainers.image.version" to "$version", + "org.opencontainers.image.authors" to "Jeho Lee ", + "org.opencontainers.image.url" to "https://github.com/ZZIBU/Tagify" + ) + jvmFlags = listOf( + "-Xms512m", + "-Xmx1024m" + ) + ports = listOf("8080") + } +} + +tasks.withType { + useJUnitPlatform() + exclude("**/Bean*Test.class", "**/*ControllerTest.class") +} diff --git a/tagify/gradle.properties b/tagify/gradle.properties new file mode 100644 index 0000000..b5c5ebe --- /dev/null +++ b/tagify/gradle.properties @@ -0,0 +1 @@ +projectOwner=jhl8109 \ No newline at end of file diff --git a/tagify/gradle/wrapper/gradle-wrapper.jar b/tagify/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e644113 Binary files /dev/null and b/tagify/gradle/wrapper/gradle-wrapper.jar differ diff --git a/tagify/gradle/wrapper/gradle-wrapper.properties b/tagify/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a441313 --- /dev/null +++ b/tagify/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/tagify/gradlew b/tagify/gradlew new file mode 100644 index 0000000..b740cf1 --- /dev/null +++ b/tagify/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/tagify/gradlew.bat b/tagify/gradlew.bat new file mode 100644 index 0000000..25da30d --- /dev/null +++ b/tagify/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/tagify/settings.gradle.kts b/tagify/settings.gradle.kts new file mode 100644 index 0000000..5bf9b1d --- /dev/null +++ b/tagify/settings.gradle.kts @@ -0,0 +1,6 @@ +pluginManagement { + plugins { + kotlin("jvm") version "2.0.0" + } +} +rootProject.name = "Tagify" \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/TagifyApplication.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/TagifyApplication.kt new file mode 100644 index 0000000..93590b1 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/TagifyApplication.kt @@ -0,0 +1,13 @@ +package zzibu.jeho.tagify + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import org.springframework.data.elasticsearch.config.EnableElasticsearchAuditing + +@SpringBootApplication +@EnableElasticsearchAuditing +class TagifyApplication + +fun main(args: Array) { + runApplication(*args) +} diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationConfig.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationConfig.kt new file mode 100644 index 0000000..1195d83 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationConfig.kt @@ -0,0 +1,17 @@ +package zzibu.jeho.tagify.config + +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.PropertySource + +@Configuration +@PropertySource("classpath:application.yml") +@EnableConfigurationProperties(ApplicationProperties::class) +class ApplicationConfig(val applicationProperties: ApplicationProperties) { + + @Bean + fun maxFileSize() : Long{ + return applicationProperties.maxFileSize + } +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationProperties.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationProperties.kt new file mode 100644 index 0000000..05640f7 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ApplicationProperties.kt @@ -0,0 +1,10 @@ +package zzibu.jeho.tagify.config + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.bind.ConstructorBinding +import org.springframework.context.annotation.PropertySource +import org.springframework.stereotype.Component + +@ConfigurationProperties(prefix="app.config") +data class ApplicationProperties (val maxFileSize:Long) +{} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelConfig.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelConfig.kt new file mode 100644 index 0000000..dca9055 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelConfig.kt @@ -0,0 +1,23 @@ +package zzibu.jeho.tagify.config + +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.boot.context.properties.bind.ConstructorBinding +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.PropertySource + +@Configuration +@PropertySource("classpath:model.properties") +@EnableConfigurationProperties(ModelProperties::class) +class ModelConfig(val modelProperties: ModelProperties) { + + @Bean + fun assistantImageMessage() : String{ + return modelProperties.imageMessage + } + + @Bean + fun assistantPdfMessage() : String{ + return modelProperties.pdfMessage + } +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelProperties.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelProperties.kt new file mode 100644 index 0000000..1f96d3c --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/config/ModelProperties.kt @@ -0,0 +1,10 @@ +package zzibu.jeho.tagify.config + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.bind.ConstructorBinding +import org.springframework.context.annotation.PropertySource +import org.springframework.stereotype.Component + +@ConfigurationProperties(prefix="vlm") +data class ModelProperties (val pdfMessage: String , val imageMessage:String) +{} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/controller/TagController.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/controller/TagController.kt new file mode 100644 index 0000000..d9e1ab2 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/controller/TagController.kt @@ -0,0 +1,47 @@ +package zzibu.jeho.tagify.controller + +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.RestController +import org.springframework.web.multipart.MultipartFile +import zzibu.jeho.tagify.service.ImageService +import zzibu.jeho.tagify.service.PdfService + +@RestController +@RequestMapping("/api/v1/tag") +class TagController(private val pdfService: PdfService, + private val imageService: ImageService) { + + @PostMapping("/image") + fun generateTagsFromImage( + @RequestParam("image") image: MultipartFile, + ): ResponseEntity> { + return ResponseEntity( + imageService.generateTagByImage(image), + HttpStatus.OK + ) + } + + @PostMapping("/pdf") + fun generateTagsFromPDF( + @RequestParam("pdf") pdf: MultipartFile, + ): ResponseEntity> { + return ResponseEntity( + pdfService.generateTagByPDF(pdf), + HttpStatus.OK + ) + } + +// @PostMapping("/pdf/text") +// fun generateTagsFromPDF2( +// @RequestParam("pdf") pdf: MultipartFile, +// ): ResponseEntity> { +// return ResponseEntity( +// pdfService.generateTagFromPDFToText(pdf), +// HttpStatus.OK +// ) +// } +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorCode.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorCode.kt new file mode 100644 index 0000000..d3ac192 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorCode.kt @@ -0,0 +1,9 @@ +package zzibu.jeho.tagify.exception + +import org.springframework.http.HttpStatus + +enum class ErrorCode(val status : HttpStatus, val message : String) { + FILE_TOO_LARGE(HttpStatus.BAD_REQUEST, "허용된 파일 크기를 초과합니다."), + INVALID_FILE_TYPE(HttpStatus.BAD_REQUEST, "허용되지 않는 파일 포맷입니다."), + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부에 문제가 발생했습니다."); +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorResponse.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorResponse.kt new file mode 100644 index 0000000..dd0e34d --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/ErrorResponse.kt @@ -0,0 +1,4 @@ +package zzibu.jeho.tagify.exception + +data class ErrorResponse(val code : ErrorCode, val details : String, val timestamp: String, val path : String) +{} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/GlobalExceptionHandler.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/GlobalExceptionHandler.kt new file mode 100644 index 0000000..ea1c697 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/GlobalExceptionHandler.kt @@ -0,0 +1,48 @@ +package zzibu.jeho.tagify.exception + +import jakarta.servlet.http.HttpServletRequest +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ControllerAdvice +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.multipart.MaxUploadSizeExceededException +import org.springframework.web.multipart.MultipartException +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter + + +@ControllerAdvice +class GlobalExceptionHandler { + + @ExceptionHandler(MaxUploadSizeExceededException::class) + fun handleMaxUploadSizeExceededException(ex: MaxUploadSizeExceededException, request: HttpServletRequest) : ResponseEntity { + val errorResponse = ErrorResponse( + ErrorCode.FILE_TOO_LARGE, + ex.message ?: "파일 크기를 확인해주세요.", + System.currentTimeMillis().toLocalDateTime(), + request.requestURI, + ) + return ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST) + } + + @ExceptionHandler(InvalidFileTypeException::class) + fun handleMultipartException(ex: InvalidFileTypeException, request: HttpServletRequest): ResponseEntity { + val errorResponse = ErrorResponse( + ErrorCode.INVALID_FILE_TYPE, + ex.message ?: "파일 타입을 확인해주세요.", + System.currentTimeMillis().toLocalDateTime(), + request.requestURI + ) + return ResponseEntity(errorResponse, HttpStatus.BAD_REQUEST) + } + + private fun Long.toLocalDateTime(): String { + return LocalDateTime.ofInstant( + Instant.ofEpochMilli(this), + ZoneOffset.systemDefault() + ).format(DateTimeFormatter.ISO_DATE) + } + +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/InvalidFileTypeException.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/InvalidFileTypeException.kt new file mode 100644 index 0000000..e0761bc --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/exception/InvalidFileTypeException.kt @@ -0,0 +1,3 @@ +package zzibu.jeho.tagify.exception + +class InvalidFileTypeException(message: String) : RuntimeException(message) diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/service/ImageService.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/service/ImageService.kt new file mode 100644 index 0000000..e3a041f --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/service/ImageService.kt @@ -0,0 +1,58 @@ +package zzibu.jeho.tagify.service + +import org.springframework.ai.chat.messages.Media +import org.springframework.ai.chat.messages.Message +import org.springframework.ai.chat.messages.UserMessage +import org.springframework.ai.chat.model.ChatModel +import org.springframework.ai.chat.model.ChatResponse +import org.springframework.ai.chat.prompt.Prompt +import org.springframework.ai.ollama.api.OllamaOptions +import org.springframework.http.MediaType +import org.springframework.stereotype.Service +import org.springframework.util.MimeTypeUtils +import org.springframework.web.multipart.MaxUploadSizeExceededException +import org.springframework.web.multipart.MultipartFile +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import zzibu.jeho.tagify.util.ConversionUtils +import java.util.List + +@Service +class ImageService( + private val chatModel: ChatModel, + private val assistantImageMessage: String, + private val maxFileSize: Long + ) { + + fun generateTagByImage(image: MultipartFile): kotlin.collections.List { + validateImage(image) + val vlmResponse = sendImageToVLM(image) + val tags = ConversionUtils.jsonToList(vlmResponse) + + return tags + } + + fun sendImageToVLM(image : MultipartFile): String { + val imageData = ConversionUtils.convertToInputStreamResource(image) + + val userMessage = UserMessage( + assistantImageMessage, + List.of(Media(MimeTypeUtils.ALL, imageData)) + ) + + val response: ChatResponse = chatModel.call( + Prompt(List.of(userMessage), OllamaOptions.create().withModel("llava")) + ) + return response.result.output.content.trimIndent() + } + private fun validateImage(image : MultipartFile) : Unit { + if(!isImage(image)) throw InvalidFileTypeException("파일 타입을 확인해주세요") + if(image.size > maxFileSize) throw MaxUploadSizeExceededException(image.size) + } + private fun isImage(file : MultipartFile) : Boolean { + val contentType = file.contentType + return contentType != null && (contentType == MediaType.IMAGE_JPEG_VALUE || + contentType == MediaType.IMAGE_PNG_VALUE || + contentType == MediaType.IMAGE_GIF_VALUE + ) + } +} \ No newline at end of file diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/service/PdfService.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/service/PdfService.kt new file mode 100644 index 0000000..4158e35 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/service/PdfService.kt @@ -0,0 +1,84 @@ +package zzibu.jeho.tagify.service + +import io.github.oshai.kotlinlogging.KotlinLogging +import org.springframework.ai.chat.messages.Media +import org.springframework.ai.chat.messages.UserMessage +import org.springframework.ai.chat.model.ChatModel +import org.springframework.ai.chat.model.ChatResponse +import org.springframework.ai.chat.prompt.Prompt +import org.springframework.ai.ollama.api.OllamaOptions +import org.springframework.http.MediaType +import org.springframework.stereotype.Service +import org.springframework.util.MimeTypeUtils +import org.springframework.web.multipart.MaxUploadSizeExceededException +import org.springframework.web.multipart.MultipartFile +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import zzibu.jeho.tagify.util.ConversionUtils +import java.awt.image.BufferedImage + + +private val logger = KotlinLogging.logger{} + +@Service +class PdfService( + private val chatModel: ChatModel, + private val assistantPdfMessage: String, + private val maxFileSize: Long + ) { + + fun generateTagByPDF(file: MultipartFile): List { + validateFile(file) + val images = ConversionUtils.convertFileToImages(file) + val tags = images.flatMap { image -> + val vlmResponse = sendImageToVLM(image) + ConversionUtils.jsonToList(vlmResponse) + } + return tags + } + + fun generateTagFromPDFToText(file: MultipartFile): List { + validateFile(file) + val ocrTexts = ConversionUtils.convertFileToText(file) + + val vlmResponse = sendTextToVLM(ocrTexts) + val tags = ConversionUtils.jsonToList(vlmResponse) + + return tags + } + fun sendImageToVLM(image: BufferedImage): String { + val imageData = ConversionUtils.convertToInputStreamResource(image) + val userMessage = UserMessage( + assistantPdfMessage, + listOf(Media(MimeTypeUtils.ALL, imageData)) + ) + + val response: ChatResponse = chatModel.call( + Prompt(listOf(userMessage), OllamaOptions.create().withModel("llava")) + ) + return response.result.output.content.trimIndent() + } + + fun sendTextToVLM(text: String): String { + val userMessage = UserMessage(assistantPdfMessage) + + val response: ChatResponse = chatModel.call( + Prompt(listOf(userMessage), OllamaOptions.create().withModel("llava")) + ) + return response.result.output.content.trimIndent() + } + + private fun validateFile(file: MultipartFile) { + if (!isValidFile(file)) throw InvalidFileTypeException("파일 타입을 확인해주세요") + if (file.size > maxFileSize) throw MaxUploadSizeExceededException(file.size) + } + + private fun isValidFile(file: MultipartFile): Boolean { + val contentType = file.contentType + return contentType != null && ( + contentType == MediaType.IMAGE_JPEG_VALUE || + contentType == MediaType.IMAGE_PNG_VALUE || + contentType == MediaType.IMAGE_GIF_VALUE || + contentType == "application/pdf" + ) + } +} diff --git a/tagify/src/main/kotlin/zzibu/jeho/tagify/util/ConversionUtils.kt b/tagify/src/main/kotlin/zzibu/jeho/tagify/util/ConversionUtils.kt new file mode 100644 index 0000000..660c2c2 --- /dev/null +++ b/tagify/src/main/kotlin/zzibu/jeho/tagify/util/ConversionUtils.kt @@ -0,0 +1,91 @@ +package zzibu.jeho.tagify.util + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import net.sourceforge.tess4j.Tesseract +import net.sourceforge.tess4j.TesseractException +import org.apache.pdfbox.pdmodel.PDDocument +import org.apache.pdfbox.rendering.PDFRenderer +import org.springframework.core.io.InputStreamResource +import org.springframework.core.io.Resource +import org.springframework.http.MediaType +import org.springframework.web.multipart.MultipartFile +import java.awt.image.BufferedImage +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.IOException +import javax.imageio.ImageIO + +object ConversionUtils { + + fun jsonToList(jsonString: String): List { + val objectMapper = jacksonObjectMapper() + val map: Map = objectMapper.readValue(jsonString) + return map.values.toList() + } + + @Throws(IOException::class) + fun convertToInputStreamResource(image: BufferedImage): Resource { + val baos = ByteArrayOutputStream() + ImageIO.write(image, "jpg", baos) + val inputStream = ByteArrayInputStream(baos.toByteArray()) + return object : InputStreamResource(inputStream) { + override fun getFilename(): String? { + return "image.jpg" + } + } + } + + @Throws(IOException::class) + fun convertToInputStreamResource(file: MultipartFile): Resource { + return object : InputStreamResource(file.inputStream) { + override fun getFilename(): String? { + return file.originalFilename + } + } + } + + fun convertFileToImages(file: MultipartFile): List { + return if (file.contentType == "application/pdf") { + convertPdfToImages(file) + } else { + listOf(convertToBufferedImage(file)) + } + } + + private fun convertToBufferedImage(file: MultipartFile): BufferedImage { + return ImageIO.read(file.inputStream) + } + + private fun convertPdfToImages(file: MultipartFile): List { + val document = PDDocument.load(file.inputStream) + val pdfRenderer = PDFRenderer(document) + val images = mutableListOf() + + for (pageIndex in 0 until document.numberOfPages) { + val bufferedImage = pdfRenderer.renderImageWithDPI(pageIndex, 300f) // Render at 300 DPI + images.add(bufferedImage) + } + document.close() + return images + } + + @Throws(TesseractException::class) + fun extractTextFromImage(image: BufferedImage): String { + val tesseract = Tesseract() + tesseract.setDatapath("data/tessdata") // Tesseract 데이터 파일 경로 + tesseract.setLanguage("eng") // 사용할 언어 설정 + return tesseract.doOCR(image) + } + + @Throws(IOException::class, TesseractException::class) + fun convertFileToText(file: MultipartFile): String { + val images = convertFileToImages(file) + val textBuilder = StringBuilder() + for (image in images) { + textBuilder.append(extractTextFromImage(image)) + textBuilder.append("\n") + } + return textBuilder.toString() + } +} \ No newline at end of file diff --git a/tagify/src/main/resources/application.yml b/tagify/src/main/resources/application.yml new file mode 100644 index 0000000..070714d --- /dev/null +++ b/tagify/src/main/resources/application.yml @@ -0,0 +1,31 @@ +spring: + application: + name: Tagify + data: + elasticsearch: + clients: + uris: http://localhost:9200 + servlet: + multipart: + max-file-size: 10MB + max-request-size: 10MB + + ai: + ollama: + chat: + enabled: true + options: + model: llava + format: json + keep-alive: 5m + + base-url: http://localhost:11434 + +# required : 배포 이후 logback을 통해 환경별 세부 관리가 필요함 +logging: + level: + root: INFO + +app: + config: + max-file-size: 10485760 # 10 * 1024 * 1024 \ No newline at end of file diff --git a/tagify/src/main/resources/docker-compose.yml b/tagify/src/main/resources/docker-compose.yml new file mode 100644 index 0000000..23b3383 --- /dev/null +++ b/tagify/src/main/resources/docker-compose.yml @@ -0,0 +1,30 @@ +version: '3.8' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:8.4.0 + container_name: elasticsearch + environment: + - discovery.type=single-node + - xpack.security.enabled=false + - xpack.security.http.ssl.enabled=false + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + ports: + - "9200:9200" + networks: + - elk + + kibana: + image: docker.elastic.co/kibana/kibana:8.4.0 + container_name: kibana + environment: + - ELASTICSEARCH_HOSTS=http://elasticsearch:9200 + ports: + - "5601:5601" + depends_on: + - elasticsearch + networks: + - elk + +networks: + elk: + driver: bridge \ No newline at end of file diff --git a/tagify/src/main/resources/model.properties b/tagify/src/main/resources/model.properties new file mode 100644 index 0000000..65271a9 --- /dev/null +++ b/tagify/src/main/resources/model.properties @@ -0,0 +1,6 @@ +vlm.pdf-message=Identify the pdf-converted text and return the emerging tag to the array \ +format : [1 : "tag1", 2: "tag2", 3: "tag3", 4: "tag4", 5: "tag5"] \ + + +vlm.image-message=Look at the image and list the words that come to mind in an array \ +format : [1 : "tag1", 2: "tag2", 3: "tag3", 4: "tag4", 5: "tag5"] \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/TagControllerTest.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/TagControllerTest.kt new file mode 100644 index 0000000..00299c3 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/TagControllerTest.kt @@ -0,0 +1,129 @@ +package zzibu.jeho.tagify + +import com.fasterxml.jackson.databind.ObjectMapper +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.extensions.spring.SpringExtension +import io.kotest.matchers.shouldBe +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.MediaType +import org.springframework.mock.web.MockMultipartFile +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders +import org.springframework.test.web.servlet.result.MockMvcResultMatchers +import org.springframework.test.web.servlet.setup.MockMvcBuilders +import zzibu.jeho.tagify.controller.TagController +import zzibu.jeho.tagify.exception.ErrorCode +import zzibu.jeho.tagify.exception.ErrorResponse +import zzibu.jeho.tagify.exception.GlobalExceptionHandler +import zzibu.jeho.tagify.service.ImageService +import zzibu.jeho.tagify.service.PdfService +import java.nio.file.Files +import java.nio.file.Paths + + +@SpringBootTest +@AutoConfigureMockMvc +class TagControllerTest : BehaviorSpec() { + @Autowired + protected lateinit var mockMvc : MockMvc + + @Autowired + lateinit var pdfService: PdfService + + @Autowired + lateinit var imageService: ImageService + + @Autowired + lateinit var objectMapper: ObjectMapper + + init { + beforeSpec { + mockMvc = MockMvcBuilders + .standaloneSetup(TagController(pdfService,imageService)) + .setControllerAdvice(GlobalExceptionHandler()) + .build() + objectMapper = ObjectMapper() + } + + given("이미지에 대한 태그 생성이 필요할 때") { + val path = Paths.get("src/test/resources/test.jpg") + val imageContent = Files.readAllBytes(path) + val image = MockMultipartFile("image", "test.jpg", "image/jpeg", imageContent) + + `when`("클라이언트가 이미지를 업로드하면") { + val result = mockMvc.perform( + MockMvcRequestBuilders.multipart("/api/v1/tag/image") + .file(image) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andReturn() + + then("이미지 식별을 통해 생성된 태그 목록을 반환한다.") { + val response = objectMapper.readValue(result.response.contentAsString, List::class.java) + + response.size shouldBe 5 + } + } + `when`("클라이언트가 지원되지 않는 이미지 포맷을 업로드하면") { + val unsupportedImageContent = Files.readAllBytes(Paths.get("src/test/resources/unsupported-image.txt")) + val unsupportedImage = MockMultipartFile("image", "unsupported-image.txt", "text/plain", unsupportedImageContent) + + val result = mockMvc.perform( + MockMvcRequestBuilders.multipart("/api/v1/tag/image") + .file(unsupportedImage) + .contentType(MediaType.MULTIPART_FORM_DATA) + ) + .andExpect(MockMvcResultMatchers.status().isBadRequest) + .andReturn() + + then("응답은 에러 메시지를 포함해야 한다") { + val response = objectMapper.readValue(result.response.contentAsString, ErrorResponse::class.java) + response.code shouldBe ErrorCode.INVALID_FILE_TYPE + } + } + } + + given("PDF에 대한 태그 생성이 필요할 때") { + val path = Paths.get("src/test/resources/test.pdf") + val imageContent = Files.readAllBytes(path) + val image = MockMultipartFile("pdf", "test.pdf", "application/pdf", imageContent) + + `when`("클라이언트가 PDF를 업로드하면") { + val result = mockMvc.perform( + MockMvcRequestBuilders.multipart("/api/v1/tag/pdf") + .file(image) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(MockMvcResultMatchers.status().isOk) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andReturn() + + then("PDF 식별을 통해 생성된 태그 목록을 반환한다.") { + val response = objectMapper.readValue(result.response.contentAsString, List::class.java) + + response.size shouldBe 5 + } + } + `when`("클라이언트가 지원되지 않는 PDF 포맷을 업로드하면") { + val unsupportedPdfContent = Files.readAllBytes(Paths.get("src/test/resources/unsupported-pdf.txt")) + val unsupportedPdf = MockMultipartFile("pdf", "unsupported-pdf.txt", "text/plain", unsupportedPdfContent) + + val result = mockMvc.perform( + MockMvcRequestBuilders.multipart("/api/v1/tag/pdf") + .file(unsupportedPdf) + .contentType(MediaType.MULTIPART_FORM_DATA) + ) + .andExpect(MockMvcResultMatchers.status().isBadRequest) + .andReturn() + + then("응답은 에러 메시지를 포함해야 한다") { + val response = objectMapper.readValue(result.response.contentAsString, ErrorResponse::class.java) + response.code shouldBe ErrorCode.INVALID_FILE_TYPE + } + } + } + } + override fun extensions() = listOf(SpringExtension) +} \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/TagifyApplicationTests.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/TagifyApplicationTests.kt new file mode 100644 index 0000000..992a5d3 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/TagifyApplicationTests.kt @@ -0,0 +1,13 @@ +package zzibu.jeho.tagify + +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class TagifyApplicationTests { + +// @Test +// fun contextLoads() { +// } + +} diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanImageServiceTest.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanImageServiceTest.kt new file mode 100644 index 0000000..5d41040 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanImageServiceTest.kt @@ -0,0 +1,88 @@ +package zzibu.jeho.tagify.service + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.extensions.spring.SpringExtension +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import io.micrometer.common.util.StringUtils +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.mock.web.MockMultipartFile +import org.springframework.web.multipart.MaxUploadSizeExceededException +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import java.io.File +import java.nio.file.Files + +@SpringBootTest +class BeanImageServiceTest : BehaviorSpec() { + + @Autowired + lateinit var imageService: ImageService + + @Autowired + var maxFileSize : Long = 0L + + init { + val imagePath = "src/test/resources/test.jpg" // 테스트용 이미지 파일 경로 + val imageFile = File(imagePath) + val imageBytes = Files.readAllBytes(imageFile.toPath()) + + val multipartFile = MockMultipartFile( + "file", + "test.jpg", + "image/jpeg", + imageBytes + ) + + Given("ImageService가 주어졌을 때") { + When("generateTagByImage가 호출되면") { + Then("태그를 생성하고 TagInfo를 저장해야 한다") { + + + val tagInfo = imageService.generateTagByImage(multipartFile) + + tagInfo.size shouldNotBe 0 + } + } + + When("sendImageToVLM이 호출되면") { + Then("chatModel을 호출하고 응답을 반환해야 한다") { + val response = imageService.sendImageToVLM(multipartFile) + + StringUtils.isNotEmpty(response) shouldBe true + } + } + When("잘못된 파일 크기가 주어졌을 때") { + Then("MaxUploadSizeExceededException 예외를 던져야 한다") { + val largeImageFile = MockMultipartFile( + "file", + "large_test.jpg", + "image/jpeg", + ByteArray(maxFileSize.toInt() + 1) + ) + + shouldThrow { + imageService.generateTagByImage(largeImageFile) + } + } + } + + When("잘못된 파일 포맷이 주어졌을 때") { + Then("InvalidFileTypeException 예외를 던져야 한다") { + val textFile = MockMultipartFile( + "file", + "test.txt", + "text/plain", + "test".toByteArray() + ) + + shouldThrow { + imageService.generateTagByImage(textFile) + } + } + } + } + } + override fun extensions() = listOf(SpringExtension) +} \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanPdfServiceTest.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanPdfServiceTest.kt new file mode 100644 index 0000000..a2ac628 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/BeanPdfServiceTest.kt @@ -0,0 +1,89 @@ +package zzibu.jeho.tagify.service + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.extensions.spring.SpringExtension +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import io.micrometer.common.util.StringUtils +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.mock.web.MockMultipartFile +import org.springframework.web.multipart.MaxUploadSizeExceededException +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import zzibu.jeho.tagify.util.ConversionUtils +import java.io.File +import java.nio.file.Files + +@SpringBootTest +class BeanPdfServiceTest : BehaviorSpec() { + + @Autowired + lateinit var pdfService: PdfService + + @Autowired + var maxFileSize: Long = 0L + + init { + val pdfPath = "src/test/resources/test.pdf" // 테스트용 PDF 파일 경로 + val pdfFile = File(pdfPath) + val pdfBytes = Files.readAllBytes(pdfFile.toPath()) + + val multipartFile = MockMultipartFile( + "pdf", + "test.pdf", + "application/pdf", + pdfBytes + ) + + Given("PdfService가 주어졌을 때") { + When("generateTagByPDF가 호출되면") { + Then("태그를 생성하고 TagInfo를 저장해야 한다") { + val tagInfo = pdfService.generateTagByPDF(multipartFile) + + tagInfo.size shouldNotBe 0 + } + } + + When("sendImageToVLM이 호출되면") { + Then("chatModel을 호출하고 응답을 반환해야 한다") { + val images = ConversionUtils.convertFileToImages(multipartFile) + val response = pdfService.sendImageToVLM(images[0]) + + StringUtils.isNotEmpty(response) shouldBe true + } + } + When("잘못된 파일 크기가 주어졌을 때") { + Then("MaxUploadSizeExceededException 예외를 던져야 한다") { + val largePdfFile = MockMultipartFile( + "file", + "large_test.pdf", + "application/pdf", + ByteArray(maxFileSize.toInt() + 1) + ) + + shouldThrow { + pdfService.generateTagByPDF(largePdfFile) + } + } + } + + When("잘못된 파일 포맷이 주어졌을 때") { + Then("InvalidFileTypeException 예외를 던져야 한다") { + val textFile = MockMultipartFile( + "file", + "test.txt", + "text/plain", + "test".toByteArray() + ) + + shouldThrow { + pdfService.generateTagByPDF(textFile) + } + } + } + } + } + + override fun extensions() = listOf(SpringExtension) +} \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/service/ImageServiceTest.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/ImageServiceTest.kt new file mode 100644 index 0000000..c190e23 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/ImageServiceTest.kt @@ -0,0 +1,104 @@ +package zzibu.jeho.tagify.service + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.matchers.shouldBe +import org.springframework.web.multipart.MaxUploadSizeExceededException +import org.springframework.web.multipart.MultipartFile +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import java.io.ByteArrayInputStream + +class ImageServiceTest : BehaviorSpec({ + val chatModel = StubChatModel() + val assistantMessage = """ + Look at the image and list the words that come to mind in an array + format : [1, 2, 3, 4, 5] + """.trimIndent() + val maxFileSize : Long = 10 * 1024 * 1024 + val imageService = ImageService(chatModel , assistantMessage, maxFileSize) + + + Given("ImageService가 주어졌을 때") { + When("generateTagByImage가 호출되면") { + Then("Image를 식별하여 태그를 생성한다") { + val multipartFile = object : MultipartFile { // construct가 없으므로 직접 재정의 + override fun getName() = "file" + override fun getOriginalFilename() = "test.jpg" + override fun getContentType() = "image/jpeg" + override fun isEmpty() = false + override fun getSize() = 10L + override fun getBytes() = "test".toByteArray() + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + val tags = listOf("tag1", "tag2", "tag3") + + val tagInfo = imageService.generateTagByImage(multipartFile) + + tagInfo shouldBe tags + } + } + + When("sendImageToVLM이 호출되면") { + Then("chatModel을 호출하고 응답을 반환해야 한다") { + val multipartFile = object : MultipartFile { + override fun getName() = "file" + override fun getOriginalFilename() = "test.jpg" + override fun getContentType() = "image/jpeg" + override fun isEmpty() = false + override fun getSize() = 10L + override fun getBytes() = "test".toByteArray() + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + val response = imageService.sendImageToVLM(multipartFile) + response shouldBe "{\"1\":\"tag1\",\"2\":\"tag2\",\"3\":\"tag3\"}" + } + } + When("잘못된 파일 크기가 주어졌을 때") { + Then("MaxUploadSizeExceededException 예외를 던져야 한다") { + val multipartFile = object : MultipartFile { + override fun getName() = "file" + override fun getOriginalFilename() = "large_test.jpg" + override fun getContentType() = "image/jpeg" + override fun isEmpty() = false + override fun getSize() = maxFileSize + 1 // 파일 크기를 최대 크기보다 크게 설정 + override fun getBytes() = ByteArray((maxFileSize + 1).toInt()) + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + + shouldThrow { + imageService.generateTagByImage(multipartFile) + } + } + } + + When("잘못된 파일 포맷이 주어졌을 때") { + Then("InvalidFileTypeException 예외를 던져야 한다") { + val multipartFile = object : MultipartFile { + override fun getName() = "file" + override fun getOriginalFilename() = "test.txt" + override fun getContentType() = "text/plain" // 잘못된 파일 포맷 + override fun isEmpty() = false + override fun getSize() = 10L + override fun getBytes() = "test".toByteArray() + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + + shouldThrow { + imageService.generateTagByImage(multipartFile) + } + } + } + } +}) \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/service/PdfServiceTest.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/PdfServiceTest.kt new file mode 100644 index 0000000..ef961a4 --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/PdfServiceTest.kt @@ -0,0 +1,96 @@ +package zzibu.jeho.tagify.service + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.BehaviorSpec +import io.kotest.matchers.shouldBe +import org.springframework.mock.web.MockMultipartFile +import org.springframework.web.multipart.MaxUploadSizeExceededException +import org.springframework.web.multipart.MultipartFile +import zzibu.jeho.tagify.exception.InvalidFileTypeException +import java.awt.image.BufferedImage +import java.io.ByteArrayInputStream +import java.io.File +import java.nio.file.Files + +class PdfServiceTest : BehaviorSpec({ + val chatModel = StubChatModel() + val assistantMessage = """ + Look at the image and list the words that come to mind in an array + format : [1, 2, 3, 4, 5] + """.trimIndent() + val maxFileSize: Long = 10 * 1024 * 1024 + val pdfService = PdfService(chatModel, assistantMessage, maxFileSize) + + Given("PdfService가 주어졌을 때") { + When("generateTagByPDF가 호출되면") { + Then("PDF를 이미지로 변환하고 태그를 생성한다") { + val pdfPath = "src/test/resources/test.pdf" // 테스트용 PDF 파일 경로 + val pdfFile = File(pdfPath) + val pdfBytes = Files.readAllBytes(pdfFile.toPath()) + + val multipartFile = MockMultipartFile( + "file", + "test.pdf", + "application/pdf", + pdfBytes + ) + + val tags = listOf("tag1", "tag2", "tag3") + + val tagInfo = pdfService.generateTagByPDF(multipartFile) + + tagInfo shouldBe tags + } + } + + When("sendImageToVLM이 호출되면") { + Then("chatModel을 호출하고 응답을 반환해야 한다") { + val bufferedImage = BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB) + val response = pdfService.sendImageToVLM(bufferedImage) + response shouldBe "{\"1\":\"tag1\",\"2\":\"tag2\",\"3\":\"tag3\"}" + } + } + + When("잘못된 파일 크기가 주어졌을 때") { + Then("MaxUploadSizeExceededException 예외를 던져야 한다") { + val multipartFile = object : MultipartFile { + override fun getName() = "file" + override fun getOriginalFilename() = "large_test.pdf" + override fun getContentType() = "application/pdf" + override fun isEmpty() = false + override fun getSize() = maxFileSize + 1 // 파일 크기를 최대 크기보다 크게 설정 + override fun getBytes() = ByteArray((maxFileSize + 1).toInt()) + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + + shouldThrow { + pdfService.generateTagByPDF(multipartFile) + } + } + } + + When("잘못된 파일 포맷이 주어졌을 때") { + Then("InvalidFileTypeException 예외를 던져야 한다") { + val multipartFile = object : MultipartFile { + override fun getName() = "file" + override fun getOriginalFilename() = "test.txt" + override fun getContentType() = "text/plain" // 잘못된 파일 포맷 + override fun isEmpty() = false + override fun getSize() = 10L + override fun getBytes() = "test".toByteArray() + override fun getInputStream() = ByteArrayInputStream(getBytes()) + override fun transferTo(dest: java.io.File) { + dest.writeBytes(getBytes()) + } + } + + shouldThrow { + pdfService.generateTagByPDF(multipartFile) + } + } + } + } +}) \ No newline at end of file diff --git a/tagify/src/test/kotlin/zzibu/jeho/tagify/service/StubChatModel.kt b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/StubChatModel.kt new file mode 100644 index 0000000..edcd4ad --- /dev/null +++ b/tagify/src/test/kotlin/zzibu/jeho/tagify/service/StubChatModel.kt @@ -0,0 +1,20 @@ +package zzibu.jeho.tagify.service + +import org.springframework.ai.chat.metadata.ChatResponseMetadata +import org.springframework.ai.chat.model.ChatModel +import org.springframework.ai.chat.model.ChatResponse +import org.springframework.ai.chat.model.Generation +import org.springframework.ai.chat.prompt.ChatOptions +import org.springframework.ai.chat.prompt.Prompt + +class StubChatModel : ChatModel { + override fun call(prompt: Prompt): ChatResponse { + // 간단한 스텁 응답을 반환합니다. + val responseMessage = Generation("{\"1\":\"tag1\",\"2\":\"tag2\",\"3\":\"tag3\"}") + return ChatResponse(listOf(responseMessage)) + } + + override fun getDefaultOptions(): ChatOptions { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/tagify/src/test/resources/test.jpg b/tagify/src/test/resources/test.jpg new file mode 100644 index 0000000..bdf955a Binary files /dev/null and b/tagify/src/test/resources/test.jpg differ diff --git a/tagify/src/test/resources/test.pdf b/tagify/src/test/resources/test.pdf new file mode 100644 index 0000000..7f821f1 Binary files /dev/null and b/tagify/src/test/resources/test.pdf differ diff --git a/tagify/src/test/resources/unsupported-image.txt b/tagify/src/test/resources/unsupported-image.txt new file mode 100644 index 0000000..9a323c0 --- /dev/null +++ b/tagify/src/test/resources/unsupported-image.txt @@ -0,0 +1 @@ +unsupported-file format \ No newline at end of file diff --git a/tagify/src/test/resources/unsupported-pdf.txt b/tagify/src/test/resources/unsupported-pdf.txt new file mode 100644 index 0000000..9a323c0 --- /dev/null +++ b/tagify/src/test/resources/unsupported-pdf.txt @@ -0,0 +1 @@ +unsupported-file format \ No newline at end of file