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.PortraitStore;
|
||||
import ctbrec.image.RemotePortraitStore;
|
||||
import ctbrec.io.HttpClient;
|
||||
import ctbrec.io.HttpException;
|
||||
import ctbrec.io.json.ObjectMapperFactory;
|
||||
import ctbrec.io.json.dto.ModelDto;
|
||||
import ctbrec.io.json.mapper.MappingException;
|
||||
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.SiteUtil;
|
||||
import ctbrec.ui.CamrecApplication;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
@ -32,6 +37,10 @@ import java.util.stream.Collectors;
|
|||
@Slf4j
|
||||
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 {
|
||||
NOTES,
|
||||
|
@ -41,7 +50,7 @@ public class ModelImportExport {
|
|||
|
||||
private static final ObjectMapper mapper = ObjectMapperFactory.getMapper();
|
||||
|
||||
record ExportOptions(Set<ExportIncludes> includes, File targetFile) {
|
||||
public record ExportOptions(Set<ExportIncludes> includes, File targetFile) {
|
||||
}
|
||||
|
||||
private ModelImportExport() {
|
||||
|
@ -68,11 +77,12 @@ public class ModelImportExport {
|
|||
sb.append(modelArray);
|
||||
writer.endArray();
|
||||
if (exportOptions.includes().contains(ExportIncludes.NOTES)) {
|
||||
writer.key("notes");
|
||||
writer.value(config.getSettings().modelNotes);
|
||||
ModelNotesService modelNotesService = getModelNotesService(config);
|
||||
writer.key(KEY_NOTES);
|
||||
writer.value(modelNotesService.loadAllModelNotes());
|
||||
}
|
||||
if (exportOptions.includes().contains(ExportIncludes.GROUPS)) {
|
||||
writer.key("groups");
|
||||
writer.key(KEY_GROUPS);
|
||||
writer.array();
|
||||
var groupArray = groups
|
||||
.stream().map(grp -> {
|
||||
|
@ -88,74 +98,74 @@ public class ModelImportExport {
|
|||
writer.endArray();
|
||||
}
|
||||
if (exportOptions.includes().contains(ExportIncludes.PORTRAITS)) {
|
||||
var portraits = config.getSettings().modelPortraits;
|
||||
PortraitStore portraitLoader;
|
||||
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();
|
||||
for (Map.Entry<String, String> entry : config.getSettings().modelPortraits.entrySet()) {
|
||||
String modelUrl = entry.getKey();
|
||||
String portraitId = entry.getValue();
|
||||
Optional<byte[]> portrait = portraitLoader.loadModelPortraitByModelUrl(modelUrl);
|
||||
PortraitStore portraitLoader = getPortraitStore(config);
|
||||
writer.key(KEY_PORTRAITS);
|
||||
writer.array();
|
||||
for (Model model : models) {
|
||||
try {
|
||||
Optional<byte[]> portrait = portraitLoader.loadModelPortraitByModelUrl(model.getUrl());
|
||||
if (portrait.isPresent()) {
|
||||
writer.object();
|
||||
writer.key("url").value(modelUrl);
|
||||
writer.key("id").value(portraitId);
|
||||
writer.key("url").value(model.getUrl());
|
||||
writer.key("id").value(portraitLoader.idForModelUrl(model.getUrl()));
|
||||
writer.key("data").value(Base64.getEncoder().encodeToString(portrait.get()));
|
||||
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();
|
||||
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 {
|
||||
JSONObject json = new JSONObject(Files.readString(target.toPath(), StandardCharsets.UTF_8));
|
||||
List<Model> models = readModels(json.getJSONArray("models"));
|
||||
models.forEach(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
||||
if (json.has("notes")) {
|
||||
importNotes(json.getJSONObject("notes"), config);
|
||||
if (json.has(KEY_NOTES)) {
|
||||
importNotes(json.getJSONObject(KEY_NOTES), config);
|
||||
}
|
||||
if (json.has("groups")) {
|
||||
importGroups(json.getJSONArray("groups"), config);
|
||||
if (json.has(KEY_GROUPS)) {
|
||||
importGroups(json.getJSONArray(KEY_GROUPS), config);
|
||||
}
|
||||
if (json.has("portraits")) {
|
||||
importPortraits(json.getJSONArray("portraits"), config);
|
||||
if (json.has(KEY_PORTRAITS)) {
|
||||
importPortraits(json.getJSONArray(KEY_PORTRAITS), config);
|
||||
}
|
||||
return models;
|
||||
}
|
||||
|
||||
private static void importPortraits(JSONArray portraits, Config config) throws IOException {
|
||||
PortraitStore portraitStore;
|
||||
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);
|
||||
}
|
||||
PortraitStore portraitStore = getPortraitStore(config);
|
||||
for (int i = 0; i < portraits.length(); i++) {
|
||||
JSONObject portrait = portraits.getJSONObject(i);
|
||||
String url = portrait.getString("url");
|
||||
String id = portrait.getString("id");
|
||||
// String id = portrait.getString("id");
|
||||
String dataBase64 = portrait.getString("data");
|
||||
portraitStore.writePortrait(url, Base64.getDecoder().decode(dataBase64));
|
||||
}
|
||||
|
@ -175,14 +185,17 @@ public class ModelImportExport {
|
|||
}
|
||||
|
||||
private static void importNotes(JSONObject notes, Config config) {
|
||||
var modelNotes = new HashMap<String, String>();
|
||||
ModelNotesService modelNotesService = getModelNotesService(config);
|
||||
JSONArray urls = notes.names();
|
||||
for (int i = 0; urls != null && i < urls.length(); i++) {
|
||||
String url = urls.getString(i);
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue