001package jmri.jmrit.ctc;
002
003import java.io.File;
004import java.io.IOException;
005import java.nio.file.Files;
006import java.nio.file.Path;
007import java.nio.file.Paths;
008import java.nio.file.StandardCopyOption;
009import jmri.util.FileUtil;
010
011/**
012 * CTC File utility
013
014 * @author Dave Sand Copyright (C) 2019
015 */
016
017public class CTCFiles {
018
019    /**
020     * Verify that the standard file path is valid.
021     * Create the ctc directory if needed.
022     * @param fileName The name of the file
023     * @return the requested file object or null if the path is not valid.
024     */
025    public static File getFile(String fileName) {
026        // Verify that preference:ctc exists
027        File chkdir = new File(getFileLocation());
028        if (!chkdir.exists()) {
029            if (!chkdir.mkdir()) {
030                log.error("Create preference:ctc failed");  // NOI18N
031                return null;
032            }
033        }
034        return new File(getFullName(fileName));
035    }
036
037    public static String getFileLocation() {
038        return new File(FileUtil.getUserFilesPath(), "ctc").getAbsolutePath();  // NOI18N
039    }
040
041    /**
042     * Create the full file name with path
043     * @param fileName The name of the file.
044     * @return the full path and name.
045     */
046    public static String getFullName(String fileName) {
047        return new File(getFileLocation(), fileName).getAbsolutePath();
048    }
049
050    public static boolean fileExists(String fileName) {
051        File file = getFile(fileName);
052        return (file == null ? false : file.exists());
053    }
054
055    public static boolean copyFile(String sourceFileName, String destFileName, boolean replace) {
056        File sourceFile = getFile(sourceFileName);
057        File destFile = getFile(destFileName);
058        if (destFile.exists() && !replace) {
059            log.error("Copy file {} failed: new file {} already exists", sourceFileName,  destFileName);
060            return false;
061        }
062        try {
063            if (Files.copy(sourceFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING) == null) return false;
064        } catch(IOException ex) {
065            log.error("Copy file {} to {} failed, exception: ", sourceFileName,  destFileName, ex);
066            return false;
067        }
068        return true;
069    }
070
071    public static boolean renameFile(String oldFileName, String newFileName, boolean replace) {
072        File oldFile = getFile(oldFileName);
073        File newFile = getFile(newFileName);
074        if (newFile.exists() && !replace) {
075            log.error("Rename file {} failed: new file {} already exists", oldFileName,  newFileName);
076            return false;
077        }
078        try {
079            if (Files.move(oldFile.toPath(), oldFile.toPath().resolveSibling(newFileName), StandardCopyOption.REPLACE_EXISTING) == null) return false;
080        } catch(IOException ex) {
081            log.error("Rename file {} to {} failed, exception: ", oldFileName,  newFileName, ex);
082            return false;
083        }
084        return true;
085    }
086
087    public static boolean deleteFile(String fileName) {
088        File file = getFile(fileName);
089        if (!file.exists()) {
090            return true;
091        }
092        try {
093            Files.delete(file.toPath());
094        } catch(IOException ex) {
095            log.error("Delete file {} failed, exception: ", fileName, ex);
096            return false;
097        }
098        return true;
099    }
100
101    public static String addExtensionIfMissing(String path, String missingExtension) {
102        String filenameOnly = getFilenameOnly(path);
103        if (filenameOnly.indexOf('.') >= 0) return path;
104        return path + missingExtension;
105    }
106
107    public static String changeExtensionTo(String path, String newExtension) {
108        return addExtensionIfMissing(removeFileExtension(path), newExtension);
109    }
110
111    public static String removeFileExtension(String filename) {
112        final int lastIndexOf = filename.lastIndexOf('.');
113        return lastIndexOf >= 1 ? filename.substring(0, lastIndexOf) : filename;
114    }
115
116    public static String getFilenameOnly(String path) {
117        // Paths.get(path) can return null per the Paths documentation
118        Path file = Paths.get(path);
119        if (file != null){
120            Object fileName = file.getFileName();
121            if (fileName!=null) {
122                return fileName.toString();
123            }
124        }
125        return "";
126    }
127
128
129    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CTCFiles.class);
130}