package com.lanyuanxiaoyao.service.uploader.controller;

import cn.hutool.core.io.IoUtil;
import com.lanyuanxiaoyao.service.uploader.configuration.UploaderConfiguration;
import com.lanyuanxiaoyao.service.uploader.service.filesystem.UploadAndDownloadService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanyuanxiaoyao
 * @date 2024-03-01
 */
@RestController
@RequestMapping("file")
public class FileController {
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    private final UploadAndDownloadService service;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    public FileController(UploadAndDownloadService service) {
        this.service = service;
    }

    @RequestMapping("upload/{filename}")
    public void upload(@PathVariable("filename") String filename, HttpServletRequest request) throws Exception {
        service.upload(filename, request.getInputStream());
    }

    @RequestMapping("download/{filename}")
    public void download(@PathVariable("filename") String filename, HttpServletResponse response) throws Exception {
        service.download(filename, response.getOutputStream());
    }
}
