* @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;
}
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 {
@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;
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(
* @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();
}
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")) {
private void retryFailedReceivedMessages(
ReceiveMessageHandler handler, boolean ignoreAttachments
) {
- final File cachePath = new File(getMessageCachePath());
+ final File cachePath = getMessageCachePath();
if (!cachePath.exists()) {
return;
}
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());
}
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) {
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;
}
}
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;
}
return username;
}
- public String getFileName() {
+ public File getFileName() {
return fileName;
}
}
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());
}
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);
}
}
public static SignalAccount createLinkedAccount(
- String dataPath,
+ File dataPath,
String username,
UUID uuid,
String password,
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);
}
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);
}
}
- 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…");
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 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));
}
}
- 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 {