Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 파일 서빙, 다운로드, 삭제 API #48

Merged
merged 2 commits into from
Aug 31, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.wafflestudio.csereal.core.resource.mainImage.api

import jakarta.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.core.io.UrlResource
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.net.URLEncoder
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.text.Charsets.UTF_8


@RequestMapping("/file")
@RestController
class FileController(
@Value("\${csereal.upload.path}")
private val uploadPath: String
) {

@GetMapping("/{filename:.+}")
fun serveFile(
@PathVariable filename: String,
@RequestParam(defaultValue = "false") download: Boolean,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 parameter의 역할은 무엇인가요???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

download=true 일때는 다운로드, 디폴트는 서빙(컨텐츠 보여주기)입니다

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 서빙하고 다운로드가 어떻게 다르게 동작하나요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미지를 예로 들면 브라우저에서 그냥 이미지 보여주기 vs 이미지 파일 다운로드 차이입니다!

request: HttpServletRequest
): ResponseEntity<Resource> {
val file = Paths.get(uploadPath, filename)
val resource = UrlResource(file.toUri())

if (resource.exists() || resource.isReadable) {
val contentType: String? = request.servletContext.getMimeType(resource.file.absolutePath)
val headers = HttpHeaders()

headers.contentType =
org.springframework.http.MediaType.parseMediaType(contentType ?: "application/octet-stream")

if (download) {
val originalFilename = filename.substringAfter("_")

val encodedFilename = URLEncoder.encode(originalFilename, UTF_8.toString()).replace("+", "%20")

headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''$encodedFilename")
}

return ResponseEntity.ok()
.headers(headers)
.body(resource)
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build()
}
}

@DeleteMapping("/{filename:.+}")
fun deleteFile(@PathVariable filename: String): ResponseEntity<Any> {
val file = Paths.get(uploadPath, filename)

if (Files.exists(file)) {
Files.delete(file)
return ResponseEntity.status(HttpStatus.NO_CONTENT).build()
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("파일을 찾을 수 없습니다.")
}
}
leeeryboy marked this conversation as resolved.
Show resolved Hide resolved

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class ImageServiceImpl(

val filename = "${timeMillis}_${requestImage.originalFilename}"
val totalFilename = path + filename
val saveFile = Paths.get("$totalFilename.$extension")
val saveFile = Paths.get(totalFilename)
requestImage.transferTo(saveFile)

val totalThumbnailFilename = "${path}thumbnail_$filename"
val thumbnailFile = Paths.get("$totalThumbnailFilename.$extension")
val thumbnailFile = Paths.get(totalThumbnailFilename)
Thumbnailator.createThumbnail(saveFile.toFile(), thumbnailFile.toFile(), 100, 100);

val image = MainImageEntity(
Expand Down Expand Up @@ -91,7 +91,7 @@ class ImageServiceImpl(
@Transactional
override fun createImageURL(image: MainImageEntity?): String? {
return if (image != null) {
"${endpointProperties.backend}/image/${image.filename}"
"${endpointProperties.backend}/file/${image.filename}"
} else null
}

Expand Down