2021-01-23 18:49:21 +01:00
|
|
|
package it.tdlight.utils;
|
|
|
|
|
2021-01-24 03:15:45 +01:00
|
|
|
import io.vertx.core.eventbus.DeliveryOptions;
|
2021-01-23 18:49:21 +01:00
|
|
|
import io.vertx.core.file.OpenOptions;
|
2021-01-24 03:15:45 +01:00
|
|
|
import io.vertx.reactivex.core.Vertx;
|
2021-01-23 18:49:21 +01:00
|
|
|
import io.vertx.reactivex.core.buffer.Buffer;
|
2021-01-24 03:15:45 +01:00
|
|
|
import io.vertx.reactivex.core.eventbus.Message;
|
|
|
|
import io.vertx.reactivex.core.eventbus.MessageConsumer;
|
2021-01-23 18:49:21 +01:00
|
|
|
import io.vertx.reactivex.core.file.FileSystem;
|
2021-01-24 03:15:45 +01:00
|
|
|
import it.tdlight.tdlibsession.remoteclient.TDLibRemoteClient;
|
|
|
|
import it.tdlight.tdlibsession.td.middle.EndSessionMessage;
|
2021-01-23 18:49:21 +01:00
|
|
|
import java.nio.file.Path;
|
2021-01-23 22:33:52 +01:00
|
|
|
import java.text.CharacterIterator;
|
|
|
|
import java.text.StringCharacterIterator;
|
2021-01-24 03:15:45 +01:00
|
|
|
import java.time.Duration;
|
2021-01-23 22:33:52 +01:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.ZoneOffset;
|
2021-01-23 18:49:21 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-01-23 22:33:52 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-01-23 18:49:21 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
2021-01-25 02:31:17 +01:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2021-01-23 18:49:21 +01:00
|
|
|
import reactor.util.function.Tuple2;
|
2021-01-24 03:15:45 +01:00
|
|
|
import reactor.util.function.Tuples;
|
2021-01-23 18:49:21 +01:00
|
|
|
|
|
|
|
public class BinlogUtils {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BinlogUtils.class);
|
|
|
|
|
|
|
|
public static Mono<BinlogAsyncFile> retrieveBinlog(FileSystem vertxFilesystem, Path binlogPath) {
|
|
|
|
var path = binlogPath.toString();
|
|
|
|
var openOptions = new OpenOptions().setWrite(true).setRead(true).setCreate(false).setDsync(true);
|
2021-01-23 22:33:52 +01:00
|
|
|
return vertxFilesystem
|
|
|
|
// Create file if not exist to avoid errors
|
|
|
|
.rxExists(path).filter(exists -> exists).as(MonoUtils::toMono)
|
|
|
|
.switchIfEmpty(Mono.defer(() -> vertxFilesystem.rxMkdirs(binlogPath.getParent().toString()).as(MonoUtils::toMono))
|
|
|
|
.then(vertxFilesystem.rxCreateFile(path).as(MonoUtils::toMono))
|
|
|
|
.thenReturn(true)
|
|
|
|
)
|
|
|
|
// Open file
|
|
|
|
.flatMap(x -> vertxFilesystem.rxOpen(path, openOptions).as(MonoUtils::toMono))
|
2021-01-23 18:49:21 +01:00
|
|
|
.map(file -> new BinlogAsyncFile(vertxFilesystem, path, file))
|
2021-01-25 02:31:17 +01:00
|
|
|
.single()
|
|
|
|
.publishOn(Schedulers.boundedElastic());
|
2021-01-23 18:49:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Mono<Void> saveBinlog(BinlogAsyncFile binlog, byte[] data) {
|
|
|
|
return binlog.overwrite(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Mono<Void> chooseBinlog(FileSystem vertxFilesystem,
|
|
|
|
Path binlogPath,
|
|
|
|
byte[] remoteBinlog,
|
|
|
|
long remoteBinlogDate) {
|
|
|
|
var path = binlogPath.toString();
|
|
|
|
return retrieveBinlog(vertxFilesystem, binlogPath)
|
|
|
|
.flatMap(binlog -> Mono
|
|
|
|
.just(binlog)
|
|
|
|
.zipWith(binlog.getLastModifiedTime())
|
|
|
|
)
|
2021-01-23 22:33:52 +01:00
|
|
|
.doOnSuccess(s -> logger.info("Local binlog: " + binlogPath + ". Local date: " + Instant.ofEpochMilli(s == null ? 0 : s.getT2()).atZone(ZoneOffset.UTC).toString() + " Remote date: " + Instant.ofEpochMilli(remoteBinlogDate).atZone(ZoneOffset.UTC).toString()))
|
2021-01-23 18:49:21 +01:00
|
|
|
// Files older than the remote file will be overwritten
|
2021-01-23 22:33:52 +01:00
|
|
|
.filter(tuple -> tuple.getT2() >= remoteBinlogDate)
|
|
|
|
.doOnNext(v -> logger.info("Using local binlog: " + binlogPath))
|
2021-01-23 18:49:21 +01:00
|
|
|
.map(Tuple2::getT1)
|
2021-01-23 22:33:52 +01:00
|
|
|
.switchIfEmpty(Mono.defer(() -> Mono.fromRunnable(() -> logger.info("Using remote binlog. Overwriting " + binlogPath)))
|
2021-01-23 18:49:21 +01:00
|
|
|
.then(vertxFilesystem.rxWriteFile(path, Buffer.buffer(remoteBinlog)).as(MonoUtils::toMono))
|
|
|
|
.then(retrieveBinlog(vertxFilesystem, binlogPath))
|
|
|
|
)
|
|
|
|
.single()
|
2021-01-25 02:31:17 +01:00
|
|
|
.then()
|
|
|
|
.publishOn(Schedulers.boundedElastic());
|
2021-01-23 18:49:21 +01:00
|
|
|
}
|
2021-01-23 22:33:52 +01:00
|
|
|
|
2021-01-24 19:13:46 +01:00
|
|
|
public static Mono<Void> cleanSessionPath(FileSystem vertxFilesystem,
|
|
|
|
Path binlogPath,
|
|
|
|
Path sessionPath,
|
|
|
|
Path mediaPath) {
|
2021-01-23 22:33:52 +01:00
|
|
|
return vertxFilesystem
|
|
|
|
.rxReadFile(binlogPath.toString()).as(MonoUtils::toMono)
|
|
|
|
.flatMap(buffer -> vertxFilesystem
|
|
|
|
.rxReadDir(sessionPath.toString(), "^(?!td.binlog$).*").as(MonoUtils::toMono)
|
|
|
|
.flatMapMany(Flux::fromIterable)
|
2021-01-24 19:13:46 +01:00
|
|
|
.doOnNext(file -> logger.debug("Deleting session file {}", file))
|
|
|
|
.flatMap(file -> vertxFilesystem.rxDeleteRecursive(file, true).as(MonoUtils::toMono))
|
|
|
|
.then(vertxFilesystem.rxReadDir(mediaPath.toString(), "^(?!td.binlog$).*").as(MonoUtils::toMono))
|
|
|
|
.flatMapMany(Flux::fromIterable)
|
|
|
|
.doOnNext(file -> logger.debug("Deleting media file {}", file))
|
2021-01-23 22:33:52 +01:00
|
|
|
.flatMap(file -> vertxFilesystem.rxDeleteRecursive(file, true).as(MonoUtils::toMono))
|
|
|
|
.onErrorResume(ex -> Mono.empty())
|
|
|
|
.then()
|
2021-01-25 02:31:17 +01:00
|
|
|
)
|
|
|
|
.publishOn(Schedulers.boundedElastic());
|
2021-01-23 22:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String humanReadableByteCountBin(long bytes) {
|
|
|
|
long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
|
|
|
|
if (absB < 1024) {
|
|
|
|
return bytes + " B";
|
|
|
|
}
|
|
|
|
long value = absB;
|
|
|
|
CharacterIterator ci = new StringCharacterIterator("KMGTPE");
|
|
|
|
for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
|
|
|
|
value >>= 10;
|
|
|
|
ci.next();
|
|
|
|
}
|
|
|
|
value *= Long.signum(bytes);
|
|
|
|
return String.format("%.1f %ciB", value / 1024.0, ci.current());
|
|
|
|
}
|
2021-01-24 03:15:45 +01:00
|
|
|
|
|
|
|
public static Mono<Void> readBinlogConsumer(Vertx vertx,
|
|
|
|
MessageConsumer<byte[]> readBinlogConsumer,
|
|
|
|
int botId,
|
|
|
|
boolean local) {
|
|
|
|
return Flux
|
|
|
|
.<Message<byte[]>>create(sink -> {
|
|
|
|
readBinlogConsumer.handler(sink::next);
|
|
|
|
readBinlogConsumer.endHandler(h -> sink.complete());
|
|
|
|
})
|
2021-01-25 03:36:15 +01:00
|
|
|
.flatMapSequential(req -> BinlogUtils
|
2021-01-24 03:15:45 +01:00
|
|
|
.retrieveBinlog(vertx.fileSystem(), TDLibRemoteClient.getSessionBinlogDirectory(botId))
|
|
|
|
.flatMap(BinlogAsyncFile::readFullyBytes)
|
|
|
|
.single()
|
|
|
|
.map(binlog -> Tuples.of(req, binlog))
|
|
|
|
)
|
|
|
|
.doOnNext(tuple -> {
|
|
|
|
var opts = new DeliveryOptions().setLocalOnly(local).setSendTimeout(Duration.ofSeconds(10).toMillis());
|
|
|
|
tuple.getT1().reply(new EndSessionMessage(botId, tuple.getT2()), opts);
|
|
|
|
})
|
2021-01-25 02:31:17 +01:00
|
|
|
.then()
|
|
|
|
.publishOn(Schedulers.boundedElastic());
|
2021-01-24 03:15:45 +01:00
|
|
|
}
|
2021-01-23 18:49:21 +01:00
|
|
|
}
|