Newer
Older
bremer / src / main / kotlin / controller / view / AudioViewController.kt
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.controller.view

import jakarta.servlet.http.HttpServletResponse
import net.piedpiper.bremer.service.AudioService
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.AudioService")
    private val audioService: AudioService
) {

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