More fixes for git storage

This commit is contained in:
crschnick 2023-11-29 17:31:17 +00:00
parent 6b9f32039c
commit 8e6d1cafb4
4 changed files with 32 additions and 30 deletions

View file

@ -76,11 +76,15 @@ public class DataStoreCategory extends StorageElement {
return new DataStoreCategory(null, uuid, name, Instant.now(), Instant.now(), true, parentCategory, StoreSortMode.ALPHABETICAL_ASC, false);
}
public static DataStoreCategory fromDirectory(Path dir) throws Exception {
public static Optional<DataStoreCategory> fromDirectory(Path dir) throws Exception {
ObjectMapper mapper = JacksonMapper.getDefault();
var entryFile = dir.resolve("category.json");
var stateFile = dir.resolve("state.json");
if (!Files.exists(entryFile)) {
return Optional.empty();
}
var stateJson = Files.exists(stateFile) ? mapper.readTree(stateFile.toFile()) : JsonNodeFactory.instance.objectNode();
var json = mapper.readTree(entryFile.toFile());
@ -98,7 +102,7 @@ public class DataStoreCategory extends StorageElement {
var lastUsed = Optional.ofNullable(stateJson.get("lastUsed")).map(jsonNode -> jsonNode.textValue()).map(Instant::parse).orElse(Instant.now());
var lastModified = Optional.ofNullable(stateJson.get("lastModified")).map(jsonNode -> jsonNode.textValue()).map(Instant::parse).orElse(Instant.now());
return new DataStoreCategory(dir, uuid, name, lastUsed, lastModified, false, parentUuid, sortMode, share);
return Optional.of(new DataStoreCategory(dir, uuid, name, lastUsed, lastModified, false, parentUuid, sortMode, share));
}
public boolean canShare() {

View file

@ -202,14 +202,14 @@ public class DataStoreEntry extends StorageElement {
);
}
public static DataStoreEntry fromDirectory(Path dir) throws Exception {
public static Optional<DataStoreEntry> fromDirectory(Path dir) throws Exception {
ObjectMapper mapper = JacksonMapper.getDefault();
var entryFile = dir.resolve("entry.json");
var storeFile = dir.resolve("store.json");
var stateFile = dir.resolve("state.json");
if (!Files.exists(entryFile) || !Files.exists(storeFile)) {
return null;
return Optional.empty();
}
if (!Files.exists(stateFile)) {
@ -262,18 +262,8 @@ public class DataStoreEntry extends StorageElement {
} catch (Exception e) {
ErrorEvent.fromThrowable(e).handle();
}
return createExisting(
dir,
uuid,
categoryUuid,
name,
lastUsed,
lastModified,
storeNode,
configuration,
persistentState,
expanded,
color);
return Optional.of(
createExisting(dir, uuid, categoryUuid, name, lastUsed, lastModified, storeNode, configuration, persistentState, expanded, color));
}
public void setInRefresh(boolean newRefresh) {

View file

@ -2,15 +2,15 @@ package io.xpipe.app.storage;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.core.process.OsType;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.Value;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.regex.Matcher;
@Value
@AllArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class LocalFileReference {
private static Path lastDataDir;
@ -28,25 +28,33 @@ public class LocalFileReference {
return null;
}
var replaced = s.trim().replace("<DATA>", getDataDir().toString());
// Replacement order is important
var replaced = s.trim().replace("<DATA>", getDataDir().toString())
.replace("~",System.getProperty("user.home"));
try {
return new LocalFileReference(Path.of(replaced).toString());
var normalized = Path.of(replaced).normalize().toString().replaceAll("\\\\", "/");
return new LocalFileReference(normalized);
} catch (InvalidPathException ex) {
return new LocalFileReference(replaced);
}
}
@NonNull
String path;
private final String path;
public String toString() {
return path.replaceAll("/", Matcher.quoteReplacement(OsType.getLocal().getFileSystemSeparator()));
}
public String serialize() {
var start = getDataDir();
try {
if (Path.of(path).startsWith(start)) {
return "<DATA>" + OsType.getLocal().getFileSystemSeparator() + start.relativize(Path.of(path));
var normalizedPath = Path.of(path);
if (normalizedPath.startsWith(start)) {
return "<DATA>" + "/" + start.relativize(normalizedPath);
}
} catch (InvalidPathException ignored) {}
return path.toString();
return path;
}
}

View file

@ -135,7 +135,7 @@ public class StandardStorage extends DataStorage {
}
var c = DataStoreCategory.fromDirectory(path);
storeCategories.add(c);
c.ifPresent(storeCategories::add);
} catch (IOException ex) {
// IO exceptions are not expected
exception.set(ex);
@ -218,16 +218,16 @@ public class StandardStorage extends DataStorage {
}
var entry = DataStoreEntry.fromDirectory(path);
if (entry == null) {
if (entry.isEmpty()) {
return;
}
var foundCat = getStoreCategoryIfPresent(entry.getCategoryUuid());
var foundCat = getStoreCategoryIfPresent(entry.get().getCategoryUuid());
if (foundCat.isEmpty()) {
entry.setCategoryUuid(null);
entry.get().setCategoryUuid(null);
}
storeEntries.put(entry, entry);
storeEntries.put(entry.get(), entry.get());
} catch (IOException ex) {
// IO exceptions are not expected
exception.set(ex);