mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Use File instead of String
This commit is contained in:
parent
5c754b6f5d
commit
22f19c4067
7 changed files with 61 additions and 54 deletions
|
@ -212,17 +212,19 @@ public class Main {
|
||||||
* @return the data directory to be used by signal-cli.
|
* @return the data directory to be used by signal-cli.
|
||||||
*/
|
*/
|
||||||
private static File getDefaultDataPath() {
|
private static File getDefaultDataPath() {
|
||||||
File dataPath = new File(IOUtils.getDataHomeDir(), "/signal-cli");
|
File dataPath = new File(IOUtils.getDataHomeDir(), "signal-cli");
|
||||||
if (dataPath.exists()) {
|
if (dataPath.exists()) {
|
||||||
return dataPath;
|
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()) {
|
if (legacySettingsPath.exists()) {
|
||||||
return legacySettingsPath;
|
return legacySettingsPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
legacySettingsPath = new File(System.getProperty("user.home"), "/.config/textsecure");
|
legacySettingsPath = new File(configPath, "textsecure");
|
||||||
if (legacySettingsPath.exists()) {
|
if (legacySettingsPath.exists()) {
|
||||||
return legacySettingsPath;
|
return legacySettingsPath;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
import org.asamk.signal.manager.StickerPackInvalidException;
|
import org.asamk.signal.manager.StickerPackInvalidException;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class UploadStickerPackCommand implements LocalCommand {
|
public class UploadStickerPackCommand implements LocalCommand {
|
||||||
|
@ -19,7 +20,7 @@ public class UploadStickerPackCommand implements LocalCommand {
|
||||||
@Override
|
@Override
|
||||||
public int handleCommand(final Namespace ns, final Manager m) {
|
public int handleCommand(final Namespace ns, final Manager m) {
|
||||||
try {
|
try {
|
||||||
String path = ns.getString("path");
|
File path = new File(ns.getString("path"));
|
||||||
String url = m.uploadStickerPack(path);
|
String url = m.uploadStickerPack(path);
|
||||||
System.out.println(url);
|
System.out.println(url);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -259,22 +259,22 @@ public class Manager implements Closeable {
|
||||||
return account.getDeviceId();
|
return account.getDeviceId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getMessageCachePath() {
|
private File getMessageCachePath() {
|
||||||
return pathConfig.getDataPath() + "/" + account.getUsername() + ".d/msg-cache";
|
return SignalAccount.getMessageCachePath(pathConfig.getDataPath(), account.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getMessageCachePath(String sender) {
|
private File getMessageCachePath(String sender) {
|
||||||
if (sender == null || sender.isEmpty()) {
|
if (sender == null || sender.isEmpty()) {
|
||||||
return getMessageCachePath();
|
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
|
* @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
|
* @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);
|
SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
|
||||||
|
|
||||||
SignalServiceMessageSender messageSender = createMessageSender();
|
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 {
|
) throws IOException, StickerPackInvalidException {
|
||||||
ZipFile zip = null;
|
ZipFile zip = null;
|
||||||
String rootPath = null;
|
String rootPath = null;
|
||||||
|
|
||||||
final File file = new File(path);
|
|
||||||
if (file.getName().endsWith(".zip")) {
|
if (file.getName().endsWith(".zip")) {
|
||||||
zip = new ZipFile(file);
|
zip = new ZipFile(file);
|
||||||
} else if (file.getName().equals("manifest.json")) {
|
} else if (file.getName().equals("manifest.json")) {
|
||||||
|
@ -1788,7 +1787,7 @@ public class Manager implements Closeable {
|
||||||
private void retryFailedReceivedMessages(
|
private void retryFailedReceivedMessages(
|
||||||
ReceiveMessageHandler handler, boolean ignoreAttachments
|
ReceiveMessageHandler handler, boolean ignoreAttachments
|
||||||
) {
|
) {
|
||||||
final File cachePath = new File(getMessageCachePath());
|
final File cachePath = getMessageCachePath();
|
||||||
if (!cachePath.exists()) {
|
if (!cachePath.exists()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1954,7 +1953,7 @@ public class Manager implements Closeable {
|
||||||
cacheFile = getMessageCacheFile(source, now, envelope.getTimestamp());
|
cacheFile = getMessageCacheFile(source, now, envelope.getTimestamp());
|
||||||
Files.delete(cacheFile.toPath());
|
Files.delete(cacheFile.toPath());
|
||||||
// Try to delete directory if empty
|
// Try to delete directory if empty
|
||||||
new File(getMessageCachePath()).delete();
|
getMessageCachePath().delete();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warn("Failed to delete cached message file “{}”, ignoring: {}", cacheFile, e.getMessage());
|
logger.warn("Failed to delete cached message file “{}”, ignoring: {}", cacheFile, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@ public class PathConfig {
|
||||||
private final File avatarsPath;
|
private final File avatarsPath;
|
||||||
|
|
||||||
public static PathConfig createDefault(final File settingsPath) {
|
public static PathConfig createDefault(final File settingsPath) {
|
||||||
return new PathConfig(new File(settingsPath, "/data"),
|
return new PathConfig(new File(settingsPath, "data"),
|
||||||
new File(settingsPath, "/attachments"),
|
new File(settingsPath, "attachments"),
|
||||||
new File(settingsPath, "/avatars"));
|
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() {
|
public File getDataPath() {
|
||||||
return dataPath.getPath();
|
return dataPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAttachmentsPath() {
|
public File getAttachmentsPath() {
|
||||||
return attachmentsPath.getPath();
|
return attachmentsPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAvatarsPath() {
|
public File getAvatarsPath() {
|
||||||
return avatarsPath.getPath();
|
return avatarsPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package org.asamk.signal.manager;
|
package org.asamk.signal.manager;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public class UserAlreadyExists extends Exception {
|
public class UserAlreadyExists extends Exception {
|
||||||
|
|
||||||
private final String username;
|
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.username = username;
|
||||||
this.fileName = fileName;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
public static SignalAccount load(File dataPath, String username) throws IOException {
|
||||||
final String fileName = getFileName(dataPath, username);
|
final File fileName = getFileName(dataPath, username);
|
||||||
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
|
final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
|
||||||
try {
|
try {
|
||||||
SignalAccount account = new SignalAccount(pair.first(), pair.second());
|
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 {
|
) throws IOException {
|
||||||
IOUtils.createPrivateDirectories(dataPath);
|
IOUtils.createPrivateDirectories(dataPath);
|
||||||
String fileName = getFileName(dataPath, username);
|
File fileName = getFileName(dataPath, username);
|
||||||
if (!new File(fileName).exists()) {
|
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,
|
String username,
|
||||||
UUID uuid,
|
UUID uuid,
|
||||||
String password,
|
String password,
|
||||||
|
@ -141,8 +141,8 @@ public class SignalAccount implements Closeable {
|
||||||
ProfileKey profileKey
|
ProfileKey profileKey
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
IOUtils.createPrivateDirectories(dataPath);
|
IOUtils.createPrivateDirectories(dataPath);
|
||||||
String fileName = getFileName(dataPath, username);
|
File fileName = getFileName(dataPath, username);
|
||||||
if (!new File(fileName).exists()) {
|
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) {
|
public static File getFileName(File dataPath, String username) {
|
||||||
return dataPath + "/" + username;
|
return new File(dataPath, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File getGroupCachePath(String dataPath, String username) {
|
private static File getUserPath(final File dataPath, final String username) {
|
||||||
return new File(new File(dataPath, username + ".d"), "group-cache");
|
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) {
|
if (username == null) {
|
||||||
return false;
|
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;
|
JsonNode rootNode;
|
||||||
synchronized (fileChannel) {
|
synchronized (fileChannel) {
|
||||||
fileChannel.position(0);
|
fileChannel.position(0);
|
||||||
|
@ -365,8 +373,8 @@ public class SignalAccount implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pair<FileChannel, FileLock> openFileChannel(String fileName) throws IOException {
|
private static Pair<FileChannel, FileLock> openFileChannel(File fileName) throws IOException {
|
||||||
FileChannel fileChannel = new RandomAccessFile(new File(fileName), "rw").getChannel();
|
FileChannel fileChannel = new RandomAccessFile(fileName, "rw").getChannel();
|
||||||
FileLock lock = fileChannel.tryLock();
|
FileLock lock = fileChannel.tryLock();
|
||||||
if (lock == null) {
|
if (lock == null) {
|
||||||
logger.info("Config file is in use by another instance, waiting…");
|
logger.info("Config file is in use by another instance, waiting…");
|
||||||
|
|
|
@ -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 {
|
public static void createPrivateDirectories(File file) throws IOException {
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
return;
|
return;
|
||||||
|
@ -65,8 +60,8 @@ public class IOUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createPrivateFile(String path) throws IOException {
|
public static void createPrivateFile(File path) throws IOException {
|
||||||
final Path file = new File(path).toPath();
|
final Path file = path.toPath();
|
||||||
try {
|
try {
|
||||||
Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE);
|
Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE);
|
||||||
Files.createFile(file, PosixFilePermissions.asFileAttribute(perms));
|
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");
|
String dataHome = System.getenv("XDG_DATA_HOME");
|
||||||
if (dataHome != null) {
|
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 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue