package up6.biz;

import up6.FileTool;
import up6.PathTool;
import up6.model.FileInf;

import java.io.File;

import com.google.gson.Gson;

public class FolderSchema {

    public FolderSchema() {}

    /**
     * 创建目录信息
     * pathSvr/folder.txt
     * 结构：
     * folder,id,pid,pidRoot,pathSvr,pathRel,lenLoc
     * @param dir
     */
    public void create(FileInf dir)
    {
        String file = dir.schemaFile();
        Gson g = new Gson();
        String val = g.toJson(dir);
        FileTool.writeAll(file, val+"\n");
    }

    /**
     * 向层级结构信息文件添加一条记录
     * @param f
     */
    public void addFile(FileInf f) {
        String root = f.pathSvr.replaceAll(f.pathRel, "");
        root = PathTool.getParent(root);
        root = PathTool.getParent(root);
        String file = PathTool.combin(root , "schema.txt");
        Gson g = new Gson();
        String val = g.toJson(f);

        //结构文件不存在
        File fe = new File(file);
        if(!fe.exists()) return;

        FileTool.appendLine(file, val);
    }
}