Use File instead of String

This commit is contained in:
AsamK 2020-12-25 23:07:36 +01:00
parent 5c754b6f5d
commit 22f19c4067
7 changed files with 61 additions and 54 deletions

View file

@ -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());
}

View file

@ -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;
}
}

View file

@ -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;
}
}