Add mechanism to automatically transfer portraits from client to server
This commit is contained in:
parent
c52a25f2bc
commit
63ffe78c36
|
@ -1,10 +1,10 @@
|
||||||
package ctbrec.image;
|
package ctbrec.image;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
|
import ctbrec.GlobalThreadPool;
|
||||||
import ctbrec.Hmac;
|
import ctbrec.Hmac;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
@ -15,18 +15,56 @@ import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static ctbrec.io.HttpConstants.MIMETYPE_IMAGE_JPG;
|
import static ctbrec.io.HttpConstants.MIMETYPE_IMAGE_JPG;
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class RemotePortraitStore implements PortraitStore {
|
public class RemotePortraitStore implements PortraitStore {
|
||||||
|
|
||||||
private final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
private final Config config;
|
private final Config config;
|
||||||
|
|
||||||
|
public RemotePortraitStore(HttpClient httpClient, Config config) {
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
this.config = config;
|
||||||
|
transferOldPortraitsToServer(config.getSettings().modelPortraits);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void transferOldPortraitsToServer(Map<String, String> modelPortraits) {
|
||||||
|
LocalPortraitStore localPortraitStore = new LocalPortraitStore(config);
|
||||||
|
GlobalThreadPool.submit(() -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
List<String> successfullyTransfered = new ArrayList<>();
|
||||||
|
for (Map.Entry<String, String> entry : modelPortraits.entrySet()) {
|
||||||
|
localPortraitStore.loadModelPortraitById(entry.getValue()).ifPresent(data -> {
|
||||||
|
try {
|
||||||
|
log.info("Uploading portrait to server {} - {} bytes", entry.getKey(), data.length);
|
||||||
|
RemotePortraitStore.this.writePortrait(entry.getKey(), data);
|
||||||
|
successfullyTransfered.add(entry.getKey());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Could not transfer portrait from local to remote store: {} {}", entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (String s : successfullyTransfered) {
|
||||||
|
try {
|
||||||
|
localPortraitStore.removePortrait(s);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("Could not delete local potrait, which has been transferred to the server: {} ", s, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private String getEndpoint() {
|
private String getEndpoint() {
|
||||||
return config.getServerUrl() + "/image/portrait";
|
return config.getServerUrl() + "/image/portrait";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue