package com.njcn.rdms.module.project.controller.admin.workreport.common;

import com.njcn.rdms.module.project.service.workreport.export.WorkReportExportFile;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public final class WorkReportExportResponseUtils {

    private WorkReportExportResponseUtils() {
    }

    public static void write(HttpServletResponse response, WorkReportExportFile file) throws IOException {
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"
                + URLEncoder.encode(file.filename(), StandardCharsets.UTF_8).replace("+", "%20"));
        response.setContentType(file.contentType());
        response.setContentLength(file.content().length);
        response.getOutputStream().write(file.content());
    }
}
