MrLetsplay 444e42c091
All checks were successful
Build and push container / Build-Docker-Container (push) Successful in 5m44s
Improve thumbnail creation
2025-02-19 23:32:14 +01:00

92 lines
2.7 KiB
Java

package me.mrletsplay.videobase;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import me.mrletsplay.mrcore.json.JSONObject;
import me.mrletsplay.mrcore.json.converter.JSONConverter;
import me.mrletsplay.mrcore.json.converter.SerializationOption;
import me.mrletsplay.simplehttpserver.http.cors.CorsConfiguration;
import me.mrletsplay.simplehttpserver.http.server.HttpServer;
import me.mrletsplay.videobase.library.Library;
import me.mrletsplay.videobase.rest.LibraryAPI;
import me.mrletsplay.videobase.util.ThumbnailCreator;
public class VideoBase {
public static final Logger LOGGER = LoggerFactory.getLogger(VideoBase.class.getName());
private static ScheduledExecutorService executor;
private static Config config;
private static Library library;
public static void main(String[] args) {
executor = Executors.newScheduledThreadPool(0);
Path configPath = Path.of("config.json");
if(args.length >= 1) {
configPath = Path.of(args[0]);
}
configPath = configPath.toAbsolutePath();
try {
if(!Files.exists(configPath)) {
Files.createDirectories(configPath.getParent());
Files.writeString(configPath, Config.createDefault().toJSON(SerializationOption.DONT_INCLUDE_CLASS).toFancyString(), StandardCharsets.UTF_8);
LOGGER.info("Default configuration created, please edit it and restart");
System.exit(0);
}
config = JSONConverter.decodeObject(new JSONObject(Files.readString(configPath, StandardCharsets.UTF_8)), Config.class);
config.validate();
} catch (IOException e) {
LOGGER.error("Failed to load config", e);
System.exit(1);
}
loadLibrary();
executor.scheduleWithFixedDelay(VideoBase::loadLibrary, 10, 10, TimeUnit.MINUTES);
((ch.qos.logback.classic.Logger) LOGGER).setLevel(Level.INFO);
HttpServer server = new HttpServer(HttpServer.newConfigurationBuilder()
.hostBindAll()
.port(6969)
.poolSize(16)
.ioWorkers(2)
.logger(LOGGER)
// .logger(NOPLogger.NOP_LOGGER)
.defaultCorsConfiguration(CorsConfiguration.createAllowAll())
.create());
new LibraryAPI().register(server.getDocumentProvider());
server.start();
}
public static Config getConfig() {
return config;
}
public static Library getLibrary() {
return library;
}
private static void loadLibrary() {
library = Library.load(Path.of(config.getLibraryPath()), config.isReadOnly());
ThumbnailCreator.clearCache();
ThumbnailCreator.createThumbnails();
}
}