package up6.biz;

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

import java.io.File;

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();
        String val = String.format("folder,%s,%s,%s,%s,%s,%d,0\r\n",
                dir.id,
                dir.pid,
                dir.pidRoot,
                dir.pathSvr.replace('\\', '/'),
                dir.pathRel.replace('\\', '/'),
                dir.lenLoc
        );
        FileTool.writeAll(file, val);
    }

    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");
        String val = String.format("file,%s,%s,%s,%s,%s,%d,%s,0",
                f.id,
                f.pid,
                f.pidRoot,
                f.pathSvr.replace('\\', '/'),
                f.pathRel.replace('\\', '/'),
                f.lenLoc,
                f.fastdfs_id);

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

        FileTool.appendLine(file, val);
    }
}
