From d18f716bdf21d55d1e01604089cf5c483c921037 Mon Sep 17 00:00:00 2001 From: Eugene Livis Date: Fri, 12 Mar 2021 16:53:06 -0500 Subject: [PATCH] Relativizing paths --- .../autopsy/report/ReportBranding.java | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/report/ReportBranding.java b/Core/src/org/sleuthkit/autopsy/report/ReportBranding.java index bc31409a12..e34b4f2269 100644 --- a/Core/src/org/sleuthkit/autopsy/report/ReportBranding.java +++ b/Core/src/org/sleuthkit/autopsy/report/ReportBranding.java @@ -21,6 +21,8 @@ package org.sleuthkit.autopsy.report; import org.sleuthkit.autopsy.report.infrastructure.ReportGenerator; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.logging.Level; import org.sleuthkit.autopsy.coreutils.Logger; import org.openide.util.NbBundle; @@ -47,6 +49,7 @@ public final class ReportBranding implements ReportBrandingProviderI { private static final String DEFAULT_REPORT_FOOTER = NbBundle .getMessage(ReportBranding.class, "ReportBranding.defaultReportFooter.text"); 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 Logger logger = Logger.getLogger(ReportBranding.class.getName()); @@ -109,6 +112,14 @@ public final class ReportBranding implements ReportBrandingProviderI { 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 public String getAgencyLogoPath() { @@ -118,21 +129,50 @@ public final class ReportBranding implements ReportBrandingProviderI { * logo that uses a static variable. */ String curPath = ModuleSettings.getConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP); + + //if has been set, validate it's correct, if not set, return null - if (curPath != null && 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; + if (curPath != null) { + + // check if the path is an absolute path (starts with either drive letter or "/") + 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; } + /** + * 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 - 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. - // Should use static variable instead - ModuleSettings.setConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP, path); + ModuleSettings.setConfigSetting(MODULE_NAME, AGENCY_LOGO_PATH_PROP, relativePath.toString()); } @Override