Newer
Older
bremer / src / main / kotlin / controller / view / AudioViewController.kt
yhornisse on 2 Jul 2023 1 KB add project
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.controller.view

import jakarta.servlet.http.HttpServletResponse
import net.piedpiper.bremer.service.app.AudioAppService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import java.nio.file.Files

@Controller("bremer.controller.AudioController")
@RequestMapping("bremer/audio")
class AudioViewController(
    @Qualifier("bremer.service.AudioAppService")
    private val audioAppService: AudioAppService
) {

    @GetMapping("{slug}")
    fun download(
        @PathVariable("slug") slug: String,
        response: HttpServletResponse
    ) {
        val file = audioAppService.getAudioFile(slug)
        response.outputStream.use {
            response.setContentLengthLong(file.length())
            response.contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE
            Files.copy(file.toPath(), it)
        }
    }
}