Relativizing paths

This commit is contained in:
Eugene Livis 2021-03-12 16:53:06 -05:00
parent d05ce7d363
commit d18f716bdf

View File

@ -21,6 +21,8 @@ package org.sleuthkit.autopsy.report;
import org.sleuthkit.autopsy.report.infrastructure.ReportGenerator; import org.sleuthkit.autopsy.report.infrastructure.ReportGenerator;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level; import java.util.logging.Level;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.openide.util.NbBundle; import org.openide.util.NbBundle;
@ -47,6 +49,7 @@ public final class ReportBranding implements ReportBrandingProviderI {
private static final String DEFAULT_REPORT_FOOTER = NbBundle private static final String DEFAULT_REPORT_FOOTER = NbBundle
.getMessage(ReportBranding.class, "ReportBranding.defaultReportFooter.text"); .getMessage(ReportBranding.class, "ReportBranding.defaultReportFooter.text");
private final String reportsBrandingDir; //dir with extracted reports branding resources private final String reportsBrandingDir; //dir with extracted reports branding resources
private final Path userConfigDir = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath());
private static final String MODULE_NAME = ReportBranding.class.getSimpleName(); private static final String MODULE_NAME = ReportBranding.class.getSimpleName();
private static final Logger logger = Logger.getLogger(ReportBranding.class.getName()); private static final Logger logger = Logger.getLogger(ReportBranding.class.getName());
@ -109,6 +112,14 @@ public final class ReportBranding implements ReportBrandingProviderI {
generatorLogoPath = path; generatorLogoPath = path;
} }
/**
* Read logo path from preferences file. Reverses the path relativization performed
* in setAgencyLogoPath(). If the stored path starts with either / or drive letter,
* it is a full path, and is returned to the caller. Otherwise, append current user
* directory to the saved relative path. See JIRA-7348.
*
* @return Full path to the logo file.
*/
@Override @Override
public String getAgencyLogoPath() { public String getAgencyLogoPath() {
@ -118,21 +129,50 @@ public final class ReportBranding implements ReportBrandingProviderI {
* logo that uses a static variable. * logo that uses a static variable.
*/ */
String curPath = ModuleSettings.getConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP); String curPath = ModuleSettings.getConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP);
//if has been set, validate it's correct, if not set, return null //if has been set, validate it's correct, if not set, return null
if (curPath != null && new File(curPath).canRead() == false) { if (curPath != null) {
//use default
logger.log(Level.INFO, "Custom report branding for agency logo is not valid: {0}", curPath); //NON-NLS // check if the path is an absolute path (starts with either drive letter or "/")
curPath = null; Path driveLetterOrNetwork = Paths.get(curPath).getRoot();
if (driveLetterOrNetwork != null) {
// absolute path
return curPath;
}
// Path is a relative path. Reverse path relativization performed in setAgencyLogoPath()
Path absolutePath = userConfigDir.resolve(curPath);
curPath = absolutePath.toString();
if (new File(curPath).canRead() == false) {
//use default
logger.log(Level.INFO, "Custom report branding for agency logo is not valid: {0}", curPath); //NON-NLS
curPath = null;
}
} }
return curPath; return curPath;
} }
/**
* Save logo path. If the path is inside user directory (e.g.
* "C:\Users\USER_NAME\AppData\Roaming\autopsy"), trim that off and save it
* as a relative path (i.e it will not start with a / or drive letter). Otherwise,
* full path is saved. See JIRA-7348.
*
* @param fullPath Full path to the logo file.
*/
@Override @Override
public void setAgencyLogoPath(String path) { public void setAgencyLogoPath(String fullPath) {
Path relativePath = Paths.get(fullPath);
// check if the path is within user directory
if (Paths.get(fullPath).startsWith(userConfigDir)) {
// relativize the path
relativePath = userConfigDir.relativize(relativePath);
}
// Use properties to persist the logo to use. // Use properties to persist the logo to use.
// Should use static variable instead ModuleSettings.setConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP, relativePath.toString());
ModuleSettings.setConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP, path);
} }
@Override @Override