#1 temporary store fileId in RAM
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package ru.penkrat.stbf.common.screen;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
import ru.penkrat.stbf.api.Media;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
import ru.penkrat.stbf.api.ScreenProperties;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class RamFileIdStorageMediaScreen implements Screen {
|
||||
|
||||
private static final Map<String, String> STORAGE = new ConcurrentHashMap<>();
|
||||
|
||||
private final Screen wrapped;
|
||||
|
||||
private Media wrappedMedia;
|
||||
|
||||
@Override
|
||||
public Media getMedia() {
|
||||
if (wrapped.getMedia() == null) {
|
||||
return null;
|
||||
}
|
||||
if (wrappedMedia != null) {
|
||||
return wrappedMedia;
|
||||
}
|
||||
|
||||
final String url = wrapped.getMedia().getUrl();
|
||||
wrappedMedia = Media.builder()
|
||||
.data(getData(url))
|
||||
.url(getUrl(url))
|
||||
.fileId(STORAGE.get(url))
|
||||
.fileIdConsumer(saveFileId(url))
|
||||
.mediaType(wrapped.getMedia().getMediaType())
|
||||
.duration(wrapped.getMedia().getDuration())
|
||||
.height(wrapped.getMedia().getHeight())
|
||||
.width(wrapped.getMedia().getWidth())
|
||||
.duration(wrapped.getMedia().getDuration())
|
||||
.build();
|
||||
|
||||
return wrappedMedia;
|
||||
}
|
||||
|
||||
protected String getUrl(String url) {
|
||||
return STORAGE.containsKey(url) ? null : url;
|
||||
}
|
||||
|
||||
protected Supplier<byte[]> getData(String url) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
return wrapped.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Keyboard getKeyboard() {
|
||||
return wrapped.getKeyboard();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScreenProperties getScreenProperties() {
|
||||
return wrapped.getScreenProperties();
|
||||
}
|
||||
|
||||
private Consumer<String> saveFileId(String srcUrl) {
|
||||
return fileId -> {
|
||||
if (STORAGE.containsKey(srcUrl)) {
|
||||
return;
|
||||
}
|
||||
log.debug("Store {} as {}", srcUrl, fileId);
|
||||
|
||||
STORAGE.put(srcUrl, fileId);
|
||||
wrappedMedia = null;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user