From: AsamK Date: Fri, 25 Dec 2020 22:07:36 +0000 (+0100) Subject: Use File instead of String X-Git-Tag: v0.7.2~7 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/22f19c406779893d08675c2d06d2b7708cc3f2a8?ds=sidebyside Use File instead of String --- diff --git a/src/main/java/org/asamk/signal/Main.java b/src/main/java/org/asamk/signal/Main.java index 97e3f9b2..6204778d 100644 --- a/src/main/java/org/asamk/signal/Main.java +++ b/src/main/java/org/asamk/signal/Main.java @@ -212,17 +212,19 @@ public class Main { * @return the data directory to be used by signal-cli. */ private static File getDefaultDataPath() { - File dataPath = new File(IOUtils.getDataHomeDir(), "/signal-cli"); + File dataPath = new File(IOUtils.getDataHomeDir(), "signal-cli"); if (dataPath.exists()) { return dataPath; } - File legacySettingsPath = new File(System.getProperty("user.home"), "/.config/signal"); + File configPath = new File(System.getProperty("user.home"), ".config"); + + File legacySettingsPath = new File(configPath, "signal"); if (legacySettingsPath.exists()) { return legacySettingsPath; } - legacySettingsPath = new File(System.getProperty("user.home"), "/.config/textsecure"); + legacySettingsPath = new File(configPath, "textsecure"); if (legacySettingsPath.exists()) { return legacySettingsPath; } diff --git a/src/main/java/org/asamk/signal/commands/UploadStickerPackCommand.java b/src/main/java/org/asamk/signal/commands/UploadStickerPackCommand.java index 77df2b22..f9f5d95b 100644 --- a/src/main/java/org/asamk/signal/commands/UploadStickerPackCommand.java +++ b/src/main/java/org/asamk/signal/commands/UploadStickerPackCommand.java @@ -6,6 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.StickerPackInvalidException; +import java.io.File; import java.io.IOException; public class UploadStickerPackCommand implements LocalCommand { @@ -19,7 +20,7 @@ public class UploadStickerPackCommand implements LocalCommand { @Override public int handleCommand(final Namespace ns, final Manager m) { try { - String path = ns.getString("path"); + File path = new File(ns.getString("path")); String url = m.uploadStickerPack(path); System.out.println(url); return 0; diff --git a/src/main/java/org/asamk/signal/manager/Manager.java b/src/main/java/org/asamk/signal/manager/Manager.java index c679449d..761905df 100644 --- a/src/main/java/org/asamk/signal/manager/Manager.java +++ b/src/main/java/org/asamk/signal/manager/Manager.java @@ -259,22 +259,22 @@ public class Manager implements Closeable { return account.getDeviceId(); } - private String getMessageCachePath() { - return pathConfig.getDataPath() + "/" + account.getUsername() + ".d/msg-cache"; + private File getMessageCachePath() { + return SignalAccount.getMessageCachePath(pathConfig.getDataPath(), account.getUsername()); } - private String getMessageCachePath(String sender) { + private File getMessageCachePath(String sender) { if (sender == null || sender.isEmpty()) { return getMessageCachePath(); } - return getMessageCachePath() + "/" + sender.replace("/", "_"); + return new File(getMessageCachePath(), sender.replace("/", "_")); } private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException { - String cachePath = getMessageCachePath(sender); + File cachePath = getMessageCachePath(sender); IOUtils.createPrivateDirectories(cachePath); - return new File(cachePath + "/" + now + "_" + timestamp); + return new File(cachePath, now + "_" + timestamp); } public static Manager init( @@ -1161,7 +1161,7 @@ public class Manager implements Closeable { * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file * @return if successful, returns the URL to install the sticker pack in the signal app */ - public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException { + public String uploadStickerPack(File path) throws IOException, StickerPackInvalidException { SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path); SignalServiceMessageSender messageSender = createMessageSender(); @@ -1186,12 +1186,11 @@ public class Manager implements Closeable { } private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload( - final String path + final File file ) throws IOException, StickerPackInvalidException { ZipFile zip = null; String rootPath = null; - final File file = new File(path); if (file.getName().endsWith(".zip")) { zip = new ZipFile(file); } else if (file.getName().equals("manifest.json")) { @@ -1788,7 +1787,7 @@ public class Manager implements Closeable { private void retryFailedReceivedMessages( ReceiveMessageHandler handler, boolean ignoreAttachments ) { - final File cachePath = new File(getMessageCachePath()); + final File cachePath = getMessageCachePath(); if (!cachePath.exists()) { return; } @@ -1954,7 +1953,7 @@ public class Manager implements Closeable { cacheFile = getMessageCacheFile(source, now, envelope.getTimestamp()); Files.delete(cacheFile.toPath()); // Try to delete directory if empty - new File(getMessageCachePath()).delete(); + getMessageCachePath().delete(); } catch (IOException e) { logger.warn("Failed to delete cached message file “{}”, ignoring: {}", cacheFile, e.getMessage()); } diff --git a/src/main/java/org/asamk/signal/manager/PathConfig.java b/src/main/java/org/asamk/signal/manager/PathConfig.java index ca750931..d96034df 100644 --- a/src/main/java/org/asamk/signal/manager/PathConfig.java +++ b/src/main/java/org/asamk/signal/manager/PathConfig.java @@ -9,9 +9,9 @@ public class PathConfig { private final File avatarsPath; public static PathConfig createDefault(final File settingsPath) { - return new PathConfig(new File(settingsPath, "/data"), - new File(settingsPath, "/attachments"), - new File(settingsPath, "/avatars")); + return new PathConfig(new File(settingsPath, "data"), + new File(settingsPath, "attachments"), + new File(settingsPath, "avatars")); } private PathConfig(final File dataPath, final File attachmentsPath, final File avatarsPath) { @@ -20,15 +20,15 @@ public class PathConfig { this.avatarsPath = avatarsPath; } - public String getDataPath() { - return dataPath.getPath(); + public File getDataPath() { + return dataPath; } - public String getAttachmentsPath() { - return attachmentsPath.getPath(); + public File getAttachmentsPath() { + return attachmentsPath; } - public String getAvatarsPath() { - return avatarsPath.getPath(); + public File getAvatarsPath() { + return avatarsPath; } } diff --git a/src/main/java/org/asamk/signal/manager/UserAlreadyExists.java b/src/main/java/org/asamk/signal/manager/UserAlreadyExists.java index a07c455b..d506f0c6 100644 --- a/src/main/java/org/asamk/signal/manager/UserAlreadyExists.java +++ b/src/main/java/org/asamk/signal/manager/UserAlreadyExists.java @@ -1,11 +1,13 @@ package org.asamk.signal.manager; +import java.io.File; + public class UserAlreadyExists extends Exception { private final String username; - private final String fileName; + private final File fileName; - public UserAlreadyExists(String username, String fileName) { + public UserAlreadyExists(String username, File fileName) { this.username = username; this.fileName = fileName; } @@ -14,7 +16,7 @@ public class UserAlreadyExists extends Exception { return username; } - public String getFileName() { + public File getFileName() { return fileName; } } diff --git a/src/main/java/org/asamk/signal/storage/SignalAccount.java b/src/main/java/org/asamk/signal/storage/SignalAccount.java index e697efe3..3af52708 100644 --- a/src/main/java/org/asamk/signal/storage/SignalAccount.java +++ b/src/main/java/org/asamk/signal/storage/SignalAccount.java @@ -90,8 +90,8 @@ public class SignalAccount implements Closeable { jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); } - public static SignalAccount load(String dataPath, String username) throws IOException { - final String fileName = getFileName(dataPath, username); + public static SignalAccount load(File dataPath, String username) throws IOException { + final File fileName = getFileName(dataPath, username); final Pair pair = openFileChannel(fileName); try { SignalAccount account = new SignalAccount(pair.first(), pair.second()); @@ -105,11 +105,11 @@ public class SignalAccount implements Closeable { } public static SignalAccount create( - String dataPath, String username, IdentityKeyPair identityKey, int registrationId, ProfileKey profileKey + File dataPath, String username, IdentityKeyPair identityKey, int registrationId, ProfileKey profileKey ) throws IOException { IOUtils.createPrivateDirectories(dataPath); - String fileName = getFileName(dataPath, username); - if (!new File(fileName).exists()) { + File fileName = getFileName(dataPath, username); + if (!fileName.exists()) { IOUtils.createPrivateFile(fileName); } @@ -130,7 +130,7 @@ public class SignalAccount implements Closeable { } public static SignalAccount createLinkedAccount( - String dataPath, + File dataPath, String username, UUID uuid, String password, @@ -141,8 +141,8 @@ public class SignalAccount implements Closeable { ProfileKey profileKey ) throws IOException { IOUtils.createPrivateDirectories(dataPath); - String fileName = getFileName(dataPath, username); - if (!new File(fileName).exists()) { + File fileName = getFileName(dataPath, username); + if (!fileName.exists()) { IOUtils.createPrivateFile(fileName); } @@ -167,23 +167,31 @@ public class SignalAccount implements Closeable { return account; } - public static String getFileName(String dataPath, String username) { - return dataPath + "/" + username; + public static File getFileName(File dataPath, String username) { + return new File(dataPath, username); } - private static File getGroupCachePath(String dataPath, String username) { - return new File(new File(dataPath, username + ".d"), "group-cache"); + private static File getUserPath(final File dataPath, final String username) { + return new File(dataPath, username + ".d"); } - public static boolean userExists(String dataPath, String username) { + public static File getMessageCachePath(File dataPath, String username) { + return new File(getUserPath(dataPath, username), "msg-cache"); + } + + private static File getGroupCachePath(File dataPath, String username) { + return new File(getUserPath(dataPath, username), "group-cache"); + } + + public static boolean userExists(File dataPath, String username) { if (username == null) { return false; } - File f = new File(getFileName(dataPath, username)); + File f = getFileName(dataPath, username); return !(!f.exists() || f.isDirectory()); } - private void load(String dataPath) throws IOException { + private void load(File dataPath) throws IOException { JsonNode rootNode; synchronized (fileChannel) { fileChannel.position(0); @@ -365,8 +373,8 @@ public class SignalAccount implements Closeable { } } - private static Pair openFileChannel(String fileName) throws IOException { - FileChannel fileChannel = new RandomAccessFile(new File(fileName), "rw").getChannel(); + private static Pair openFileChannel(File fileName) throws IOException { + FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel(); FileLock lock = fileChannel.tryLock(); if (lock == null) { logger.info("Config file is in use by another instance, waiting…"); diff --git a/src/main/java/org/asamk/signal/util/IOUtils.java b/src/main/java/org/asamk/signal/util/IOUtils.java index 4d8adea6..59727a9a 100644 --- a/src/main/java/org/asamk/signal/util/IOUtils.java +++ b/src/main/java/org/asamk/signal/util/IOUtils.java @@ -46,11 +46,6 @@ public class IOUtils { return baos.toByteArray(); } - public static void createPrivateDirectories(String directoryPath) throws IOException { - final File file = new File(directoryPath); - createPrivateDirectories(file); - } - public static void createPrivateDirectories(File file) throws IOException { if (file.exists()) { return; @@ -65,8 +60,8 @@ public class IOUtils { } } - public static void createPrivateFile(String path) throws IOException { - final Path file = new File(path).toPath(); + public static void createPrivateFile(File path) throws IOException { + final Path file = path.toPath(); try { Set perms = EnumSet.of(OWNER_READ, OWNER_WRITE); Files.createFile(file, PosixFilePermissions.asFileAttribute(perms)); @@ -75,13 +70,13 @@ public class IOUtils { } } - public static String getDataHomeDir() { + public static File getDataHomeDir() { String dataHome = System.getenv("XDG_DATA_HOME"); if (dataHome != null) { - return dataHome; + return new File(dataHome); } - return System.getProperty("user.home") + "/.local/share"; + return new File(new File(System.getProperty("user.home"), ".local"), "share"); } public static void copyStreamToFile(InputStream input, File outputFile) throws IOException {