package com.pack.modeler.manager;

import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.File;

public class FileStorageManager {
    private static volatile FileStorageManager instance;

    private static String PATH_TO_STORAGE = "D:/TmpStorage/";
    private static String DOWNLOAD_LINK = "";

    public static FileStorageManager getInstance() {
        FileStorageManager localInstance = instance;
        if (localInstance == null) {
            synchronized (FileStorageManager.class) {
                localInstance = instance;
                if (localInstance == null) {
                    instance = localInstance = new FileStorageManager();
                }
            }
        }
        return localInstance;
    }

    public String getStoragePath(){
        File f = new File(PATH_TO_STORAGE);
        if(!f.exists())
            f.mkdirs();

        return PATH_TO_STORAGE;
    }

    public String getModelStoragePath(){
        File f = new File(PATH_TO_STORAGE + "models/");
        if(!f.exists())
            f.mkdirs();

        return PATH_TO_STORAGE + "models/";
    }

    public void setLinkPath(){
        DOWNLOAD_LINK = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/readFile/").toUriString();
    }

    public String getLinkPath(){
        return DOWNLOAD_LINK;
    }
}