]> nmode's Git Repositories - signal-cli/commitdiff
Use File instead of String
authorAsamK <asamk@gmx.de>
Fri, 25 Dec 2020 22:07:36 +0000 (23:07 +0100)
committerAsamK <asamk@gmx.de>
Sat, 26 Dec 2020 17:08:01 +0000 (18:08 +0100)
src/main/java/org/asamk/signal/Main.java
src/main/java/org/asamk/signal/commands/UploadStickerPackCommand.java
src/main/java/org/asamk/signal/manager/Manager.java
src/main/java/org/asamk/signal/manager/PathConfig.java
src/main/java/org/asamk/signal/manager/UserAlreadyExists.java
src/main/java/org/asamk/signal/storage/SignalAccount.java
src/main/java/org/asamk/signal/util/IOUtils.java

index 97e3f9b2e2760081f333f0626eb6b1695a79d532..6204778dd593469c43a6c69f9d8d831214e0056c 100644 (file)
@@ -212,17 +212,19 @@ public class Main {
      * @return the data directory to be used by signal-cli.
      */
     private static File getDefaultDataPath() {
      * @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;
         }
 
         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;
         }
 
         if (legacySettingsPath.exists()) {
             return legacySettingsPath;
         }
 
-        legacySettingsPath = new File(System.getProperty("user.home"), "/.config/textsecure");
+        legacySettingsPath = new File(configPath, "textsecure");
         if (legacySettingsPath.exists()) {
             return legacySettingsPath;
         }
         if (legacySettingsPath.exists()) {
             return legacySettingsPath;
         }
index 77df2b22cde7fa870eda0f2f05d7cf9a89b221c4..f9f5d95bb15894150bedfa1e3f5b5df750c3a980 100644 (file)
@@ -6,6 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.StickerPackInvalidException;
 
 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 {
 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 {
     @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;
             String url = m.uploadStickerPack(path);
             System.out.println(url);
             return 0;
index c679449d493843a445ffe82d1a70bb551ad1d6df..761905dfeb59e98028390d7da4243eec2df56eb7 100644 (file)
@@ -259,22 +259,22 @@ public class Manager implements Closeable {
         return account.getDeviceId();
     }
 
         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();
         }
 
         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 {
     }
 
     private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
-        String cachePath = getMessageCachePath(sender);
+        File cachePath = getMessageCachePath(sender);
         IOUtils.createPrivateDirectories(cachePath);
         IOUtils.createPrivateDirectories(cachePath);
-        return new File(cachePath + "/" + now + "_" + timestamp);
+        return new File(cachePath, now + "_" + timestamp);
     }
 
     public static Manager init(
     }
 
     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
      */
      * @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();
         SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
 
         SignalServiceMessageSender messageSender = createMessageSender();
@@ -1186,12 +1186,11 @@ public class Manager implements Closeable {
     }
 
     private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(
     }
 
     private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(
-            final String path
+            final File file
     ) throws IOException, StickerPackInvalidException {
         ZipFile zip = null;
         String rootPath = null;
 
     ) 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")) {
         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
     ) {
     private void retryFailedReceivedMessages(
             ReceiveMessageHandler handler, boolean ignoreAttachments
     ) {
-        final File cachePath = new File(getMessageCachePath());
+        final File cachePath = getMessageCachePath();
         if (!cachePath.exists()) {
             return;
         }
         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
                     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());
                 }
                 } catch (IOException e) {
                     logger.warn("Failed to delete cached message file “{}”, ignoring: {}", cacheFile, e.getMessage());
                 }
index ca7509310d59e8f4d84d9832aae63bb3459c3876..d96034dfd169cdce3cc5057df02b5ee2c405f1dd 100644 (file)
@@ -9,9 +9,9 @@ public class PathConfig {
     private final File avatarsPath;
 
     public static PathConfig createDefault(final File settingsPath) {
     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) {
     }
 
     private PathConfig(final File dataPath, final File attachmentsPath, final File avatarsPath) {
@@ -20,15 +20,15 @@ public class PathConfig {
         this.avatarsPath = avatarsPath;
     }
 
         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;
     }
 }
     }
 }
index a07c455b2a8153717fcb428e743bb5ac9a599967..d506f0c61d4751a9ea6aedf6500fe0ae9818fea6 100644 (file)
@@ -1,11 +1,13 @@
 package org.asamk.signal.manager;
 
 package org.asamk.signal.manager;
 
+import java.io.File;
+
 public class UserAlreadyExists extends Exception {
 
     private final String username;
 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;
     }
         this.username = username;
         this.fileName = fileName;
     }
@@ -14,7 +16,7 @@ public class UserAlreadyExists extends Exception {
         return username;
     }
 
         return username;
     }
 
-    public String getFileName() {
+    public File getFileName() {
         return fileName;
     }
 }
         return fileName;
     }
 }
index e697efe35d1a7898c960ba0da50d3db2356c22e6..3af52708b5cad9707ca0a9f0c5d312018ead7055 100644 (file)
@@ -90,8 +90,8 @@ public class SignalAccount implements Closeable {
         jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
     }
 
         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<FileChannel, FileLock> pair = openFileChannel(fileName);
         try {
             SignalAccount account = new SignalAccount(pair.first(), pair.second());
         final Pair<FileChannel, FileLock> 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(
     }
 
     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);
     ) 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);
         }
 
             IOUtils.createPrivateFile(fileName);
         }
 
@@ -130,7 +130,7 @@ public class SignalAccount implements Closeable {
     }
 
     public static SignalAccount createLinkedAccount(
     }
 
     public static SignalAccount createLinkedAccount(
-            String dataPath,
+            File dataPath,
             String username,
             UUID uuid,
             String password,
             String username,
             UUID uuid,
             String password,
@@ -141,8 +141,8 @@ public class SignalAccount implements Closeable {
             ProfileKey profileKey
     ) throws IOException {
         IOUtils.createPrivateDirectories(dataPath);
             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);
         }
 
             IOUtils.createPrivateFile(fileName);
         }
 
@@ -167,23 +167,31 @@ public class SignalAccount implements Closeable {
         return account;
     }
 
         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;
         }
         if (username == null) {
             return false;
         }
-        File f = new File(getFileName(dataPath, username));
+        File f = getFileName(dataPath, username);
         return !(!f.exists() || f.isDirectory());
     }
 
         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);
         JsonNode rootNode;
         synchronized (fileChannel) {
             fileChannel.position(0);
@@ -365,8 +373,8 @@ public class SignalAccount implements Closeable {
         }
     }
 
         }
     }
 
-    private static Pair<FileChannel, FileLock> openFileChannel(String fileName) throws IOException {
-        FileChannel fileChannel = new RandomAccessFile(new File(fileName), "rw").getChannel();
+    private static Pair<FileChannel, FileLock> 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…");
         FileLock lock = fileChannel.tryLock();
         if (lock == null) {
             logger.info("Config file is in use by another instance, waiting…");
index 4d8adea6bbaa166053e7ac30ed638008e2273fff..59727a9a773314a9cd81e164d1cdaa2f1f85346d 100644 (file)
@@ -46,11 +46,6 @@ public class IOUtils {
         return baos.toByteArray();
     }
 
         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;
     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<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE);
             Files.createFile(file, PosixFilePermissions.asFileAttribute(perms));
         try {
             Set<PosixFilePermission> 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) {
         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 {
     }
 
     public static void copyStreamToFile(InputStream input, File outputFile) throws IOException {