/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.file.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.constant.AuthConstant;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.file.config.FileStorageProperties;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 本地文件访问控制器
 *
 * @author Chill
 */
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/file")
public class LocalFileController {

	private final FileStorageProperties fileStorageProperties;

	@PreAuth(AuthConstant.PERMIT_ALL)
	@GetMapping("/static")
	public void staticFile(@RequestParam("objectKey") String objectKey, HttpServletResponse response) {
		Path objectPath = resolveObjectPath(objectKey);
		if (!Files.exists(objectPath) || !Files.isRegularFile(objectPath)) {
			throw new ServiceException("文件不存在");
		}
		try (InputStream inputStream = Files.newInputStream(objectPath)) {
			String contentType = Files.probeContentType(objectPath);
			response.setContentType(StringUtil.isBlank(contentType) ? MediaType.APPLICATION_OCTET_STREAM_VALUE : contentType);
			StreamUtils.copy(inputStream, response.getOutputStream());
		} catch (Exception exception) {
			throw new ServiceException("读取本地文件异常");
		}
	}

	private Path resolveObjectPath(String objectKey) {
		if (StringUtil.isBlank(objectKey)) {
			throw new ServiceException("文件key不能为空");
		}
		Path rootPath = Path.of(fileStorageProperties.getLocal().getRootPath()).toAbsolutePath().normalize();
		Path objectPath = rootPath.resolve(objectKey).normalize();
		if (!objectPath.startsWith(rootPath)) {
			throw new ServiceException("文件key不合法");
		}
		return objectPath;
	}
}
