package com.ruoyi.file.service;

import com.ruoyi.common.exception.ASSERT;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.file.model.FileSystemFilter;
import com.ruoyi.file.model.FileModel;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileSystemService
{
    public List<FileModel> ls(FileSystemFilter filter)
    {
        File[] files;
        if(StringUtils.isEmpty(filter.getFilePath()))
        {
            files = File.listRoots();
        }
        else
        {
            File file = new File(filter.getFilePath());
            ASSERT.EXP(file.exists(), "路径不存在: " + filter.getFilePath());
            ASSERT.EXP(file.isDirectory(), "路径非目录: " + filter.getFilePath());
            files = file.listFiles(filter);
        }

        List<FileModel> list = new ArrayList<>();
        if(null != files)
        {
            for(File f : files)
            {
                list.add(new FileModel(f));
            }
        }
        return list;
    }
}
