forked from j62/ctbrec
Fix import / export of model portraits and notes
This commit is contained in:
parent
6892e20a65
commit
a29d57eb57
|
@ -9,14 +9,19 @@ import ctbrec.ModelGroup;
|
||||||
import ctbrec.image.LocalPortraitStore;
|
import ctbrec.image.LocalPortraitStore;
|
||||||
import ctbrec.image.PortraitStore;
|
import ctbrec.image.PortraitStore;
|
||||||
import ctbrec.image.RemotePortraitStore;
|
import ctbrec.image.RemotePortraitStore;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpException;
|
||||||
import ctbrec.io.json.ObjectMapperFactory;
|
import ctbrec.io.json.ObjectMapperFactory;
|
||||||
import ctbrec.io.json.dto.ModelDto;
|
import ctbrec.io.json.dto.ModelDto;
|
||||||
import ctbrec.io.json.mapper.MappingException;
|
import ctbrec.io.json.mapper.MappingException;
|
||||||
import ctbrec.io.json.mapper.ModelMapper;
|
import ctbrec.io.json.mapper.ModelMapper;
|
||||||
|
import ctbrec.notes.LocalModelNotesService;
|
||||||
|
import ctbrec.notes.ModelNotesService;
|
||||||
|
import ctbrec.notes.RemoteModelNotesService;
|
||||||
import ctbrec.sites.Site;
|
import ctbrec.sites.Site;
|
||||||
import ctbrec.sites.SiteUtil;
|
import ctbrec.sites.SiteUtil;
|
||||||
|
import ctbrec.ui.CamrecApplication;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
@ -32,6 +37,10 @@ import java.util.stream.Collectors;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ModelImportExport {
|
public class ModelImportExport {
|
||||||
|
|
||||||
|
private static final String KEY_NOTES = "notes";
|
||||||
|
private static final String KEY_GROUPS = "groups";
|
||||||
|
private static final String KEY_PORTRAITS = "portraits";
|
||||||
|
|
||||||
|
|
||||||
enum ExportIncludes {
|
enum ExportIncludes {
|
||||||
NOTES,
|
NOTES,
|
||||||
|
@ -41,7 +50,7 @@ public class ModelImportExport {
|
||||||
|
|
||||||
private static final ObjectMapper mapper = ObjectMapperFactory.getMapper();
|
private static final ObjectMapper mapper = ObjectMapperFactory.getMapper();
|
||||||
|
|
||||||
record ExportOptions(Set<ExportIncludes> includes, File targetFile) {
|
public record ExportOptions(Set<ExportIncludes> includes, File targetFile) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ModelImportExport() {
|
private ModelImportExport() {
|
||||||
|
@ -68,11 +77,12 @@ public class ModelImportExport {
|
||||||
sb.append(modelArray);
|
sb.append(modelArray);
|
||||||
writer.endArray();
|
writer.endArray();
|
||||||
if (exportOptions.includes().contains(ExportIncludes.NOTES)) {
|
if (exportOptions.includes().contains(ExportIncludes.NOTES)) {
|
||||||
writer.key("notes");
|
ModelNotesService modelNotesService = getModelNotesService(config);
|
||||||
writer.value(config.getSettings().modelNotes);
|
writer.key(KEY_NOTES);
|
||||||
|
writer.value(modelNotesService.loadAllModelNotes());
|
||||||
}
|
}
|
||||||
if (exportOptions.includes().contains(ExportIncludes.GROUPS)) {
|
if (exportOptions.includes().contains(ExportIncludes.GROUPS)) {
|
||||||
writer.key("groups");
|
writer.key(KEY_GROUPS);
|
||||||
writer.array();
|
writer.array();
|
||||||
var groupArray = groups
|
var groupArray = groups
|
||||||
.stream().map(grp -> {
|
.stream().map(grp -> {
|
||||||
|
@ -88,74 +98,74 @@ public class ModelImportExport {
|
||||||
writer.endArray();
|
writer.endArray();
|
||||||
}
|
}
|
||||||
if (exportOptions.includes().contains(ExportIncludes.PORTRAITS)) {
|
if (exportOptions.includes().contains(ExportIncludes.PORTRAITS)) {
|
||||||
var portraits = config.getSettings().modelPortraits;
|
PortraitStore portraitLoader = getPortraitStore(config);
|
||||||
PortraitStore portraitLoader;
|
writer.key(KEY_PORTRAITS);
|
||||||
if (config.getSettings().localRecording) {
|
|
||||||
portraitLoader = new LocalPortraitStore(config);
|
|
||||||
} else {
|
|
||||||
var httpClient = new HttpClient("camrec", config) {
|
|
||||||
@Override
|
|
||||||
public boolean login() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
portraitLoader = new RemotePortraitStore(httpClient, config);
|
|
||||||
}
|
|
||||||
if (portraits != null && !portraits.isEmpty()) {
|
|
||||||
writer.key("portraits");
|
|
||||||
writer.array();
|
writer.array();
|
||||||
for (Map.Entry<String, String> entry : config.getSettings().modelPortraits.entrySet()) {
|
for (Model model : models) {
|
||||||
String modelUrl = entry.getKey();
|
try {
|
||||||
String portraitId = entry.getValue();
|
Optional<byte[]> portrait = portraitLoader.loadModelPortraitByModelUrl(model.getUrl());
|
||||||
Optional<byte[]> portrait = portraitLoader.loadModelPortraitByModelUrl(modelUrl);
|
|
||||||
if (portrait.isPresent()) {
|
if (portrait.isPresent()) {
|
||||||
writer.object();
|
writer.object();
|
||||||
writer.key("url").value(modelUrl);
|
writer.key("url").value(model.getUrl());
|
||||||
writer.key("id").value(portraitId);
|
writer.key("id").value(portraitLoader.idForModelUrl(model.getUrl()));
|
||||||
writer.key("data").value(Base64.getEncoder().encodeToString(portrait.get()));
|
writer.key("data").value(Base64.getEncoder().encodeToString(portrait.get()));
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
} catch (HttpException e) {
|
||||||
|
if (e.getResponseCode() != 404) {
|
||||||
|
log.error("Error while loading portrait from server for {}", model, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
writer.endArray();
|
writer.endArray();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
Files.writeString(exportOptions.targetFile().toPath(), new JSONObject(sb.toString()).toString(2));
|
Files.writeString(exportOptions.targetFile().toPath(), new JSONObject(sb.toString()).toString(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static PortraitStore getPortraitStore(Config config) {
|
||||||
|
PortraitStore portraitLoader;
|
||||||
|
if (config.getSettings().localRecording) {
|
||||||
|
portraitLoader = new LocalPortraitStore(config);
|
||||||
|
} else {
|
||||||
|
portraitLoader = new RemotePortraitStore(CamrecApplication.httpClient, config);
|
||||||
|
}
|
||||||
|
return portraitLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static ModelNotesService getModelNotesService(Config config) {
|
||||||
|
ModelNotesService modelNotesService;
|
||||||
|
if (config.getSettings().localRecording) {
|
||||||
|
modelNotesService = new LocalModelNotesService(config);
|
||||||
|
} else {
|
||||||
|
modelNotesService = new RemoteModelNotesService(CamrecApplication.httpClient, config);
|
||||||
|
}
|
||||||
|
return modelNotesService;
|
||||||
|
}
|
||||||
|
|
||||||
public static List<Model> importFrom(File target, List<Site> sites, Config config) throws IOException {
|
public static List<Model> importFrom(File target, List<Site> sites, Config config) throws IOException {
|
||||||
JSONObject json = new JSONObject(Files.readString(target.toPath(), StandardCharsets.UTF_8));
|
JSONObject json = new JSONObject(Files.readString(target.toPath(), StandardCharsets.UTF_8));
|
||||||
List<Model> models = readModels(json.getJSONArray("models"));
|
List<Model> models = readModels(json.getJSONArray("models"));
|
||||||
models.forEach(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
models.forEach(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
||||||
if (json.has("notes")) {
|
if (json.has(KEY_NOTES)) {
|
||||||
importNotes(json.getJSONObject("notes"), config);
|
importNotes(json.getJSONObject(KEY_NOTES), config);
|
||||||
}
|
}
|
||||||
if (json.has("groups")) {
|
if (json.has(KEY_GROUPS)) {
|
||||||
importGroups(json.getJSONArray("groups"), config);
|
importGroups(json.getJSONArray(KEY_GROUPS), config);
|
||||||
}
|
}
|
||||||
if (json.has("portraits")) {
|
if (json.has(KEY_PORTRAITS)) {
|
||||||
importPortraits(json.getJSONArray("portraits"), config);
|
importPortraits(json.getJSONArray(KEY_PORTRAITS), config);
|
||||||
}
|
}
|
||||||
return models;
|
return models;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void importPortraits(JSONArray portraits, Config config) throws IOException {
|
private static void importPortraits(JSONArray portraits, Config config) throws IOException {
|
||||||
PortraitStore portraitStore;
|
PortraitStore portraitStore = getPortraitStore(config);
|
||||||
if (config.getSettings().localRecording) {
|
|
||||||
portraitStore = new LocalPortraitStore(config);
|
|
||||||
} else {
|
|
||||||
var httpClient = new HttpClient("camrec", config) {
|
|
||||||
@Override
|
|
||||||
public boolean login() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
portraitStore = new RemotePortraitStore(httpClient, config);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < portraits.length(); i++) {
|
for (int i = 0; i < portraits.length(); i++) {
|
||||||
JSONObject portrait = portraits.getJSONObject(i);
|
JSONObject portrait = portraits.getJSONObject(i);
|
||||||
String url = portrait.getString("url");
|
String url = portrait.getString("url");
|
||||||
String id = portrait.getString("id");
|
// String id = portrait.getString("id");
|
||||||
String dataBase64 = portrait.getString("data");
|
String dataBase64 = portrait.getString("data");
|
||||||
portraitStore.writePortrait(url, Base64.getDecoder().decode(dataBase64));
|
portraitStore.writePortrait(url, Base64.getDecoder().decode(dataBase64));
|
||||||
}
|
}
|
||||||
|
@ -175,14 +185,17 @@ public class ModelImportExport {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void importNotes(JSONObject notes, Config config) {
|
private static void importNotes(JSONObject notes, Config config) {
|
||||||
var modelNotes = new HashMap<String, String>();
|
ModelNotesService modelNotesService = getModelNotesService(config);
|
||||||
JSONArray urls = notes.names();
|
JSONArray urls = notes.names();
|
||||||
for (int i = 0; urls != null && i < urls.length(); i++) {
|
for (int i = 0; urls != null && i < urls.length(); i++) {
|
||||||
String url = urls.getString(i);
|
String url = urls.getString(i);
|
||||||
String note = notes.getString(url);
|
String note = notes.getString(url);
|
||||||
modelNotes.put(url, note);
|
try {
|
||||||
|
modelNotesService.writeModelNotes(url, note);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error while importing model notes for {}", url, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
config.getSettings().modelNotes.putAll(modelNotes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Model> readModels(JSONArray models) {
|
private static List<Model> readModels(JSONArray models) {
|
||||||
|
|
Loading…
Reference in New Issue