package com.luojia_channel.modules.file.utils;

import com.luojia_channel.common.exception.FileException;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
public class GeneratePathUtil {
    public String getBucketName(String fileType) {
        return switch (fileType) {
            case "image" -> "images";
            case "video" -> "videos";
            default -> throw new FileException("无效的文件类型");
        };
    }

    // 分片对象路径格式：fileMd5/chunk_{number}
    public String getChunkObjectName(String fileMd5, int chunkNumber) {
        return String.format("%s/chunk_%d", fileMd5, chunkNumber);
    }

    // year/month/day/fileMd5/suffix
    public String getObjectName(String fileName, String fileMd5) {
        String suffix = fileName.contains(".") ?
                fileName.substring(fileName.lastIndexOf(".")) : "";
        LocalDate now = LocalDate.now();
        return String.format("%d/%02d/%02d/%s%s",
                now.getYear(),
                now.getMonthValue(),
                now.getDayOfMonth(),
                fileMd5,
                suffix);
    }
}
