Skip to content

Commit

Permalink
Merge pull request #348
Browse files Browse the repository at this point in the history
replacing configration file with database
  • Loading branch information
yermak authored Dec 27, 2021
2 parents 843e697 + e6f2923 commit 7e7ad8b
Show file tree
Hide file tree
Showing 19 changed files with 276 additions and 207 deletions.
26 changes: 19 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,27 @@
<version>1.4</version>
</dependency>


<!--
<dependency>
<groupId>com.apple</groupId>
<artifactId>javaextensions</artifactId>
<scope>>system</scope>
<systemPath>/Users/Yarick_Yermak/Projects/AudioBookConverter/lib/applejavaextensions-1.4.jar</systemPath>
<groupId>org.jetbrains.xodus</groupId>
<artifactId>xodus-openAPI</artifactId>
<version>1.3.232</version>
</dependency>
-->
<dependency>
<groupId>org.jetbrains.xodus</groupId>
<artifactId>xodus-environment</artifactId>
<version>1.3.232</version>
</dependency>
<dependency>
<groupId>org.jetbrains.xodus</groupId>
<artifactId>xodus-entity-store</artifactId>
<version>1.3.232</version>
</dependency>
<dependency>
<groupId>org.jetbrains.xodus</groupId>
<artifactId>xodus-vfs</artifactId>
<version>1.3.232</version>
</dependency>


<dependency>
<groupId>org.testng</groupId>
Expand Down
63 changes: 0 additions & 63 deletions src/main/java/uk/yermak/audiobookconverter/AppProperties.java

This file was deleted.

163 changes: 163 additions & 0 deletions src/main/java/uk/yermak/audiobookconverter/AppSetting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package uk.yermak.audiobookconverter;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import jetbrains.exodus.ByteIterable;
import jetbrains.exodus.bindings.StringBinding;
import jetbrains.exodus.entitystore.Entity;
import jetbrains.exodus.entitystore.EntityIterable;
import jetbrains.exodus.entitystore.PersistentEntityStore;
import jetbrains.exodus.entitystore.PersistentEntityStores;
import jetbrains.exodus.env.*;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.*;
import java.util.stream.StreamSupport;

public class AppSetting {
final static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final File APP_DIR = new File(System.getProperty("APP_HOME"));
public static final String PRESET_ENTITY = "Preset";
public static final String PRESET_NAME = "name";
public static final String PRESET_FORMAT = "format";
public static final String PRESET_BITRATE = "bitrate";
public static final String PRESET_FREQUENCY = "frequency";
public static final String PRESET_CHANNELS = "channels";
public static final String PRESET_CUTOFF = "cutoff";
public static final String PRESET_CBR = "cbr";
public static final String PRESET_QUALITY = "quality";
public static final String GENRE_ENTITY = "Genre";
public static final String GENRE_TITLE = "title";
public static final String GENRE_CREATED = "created";
public static final String SETTINGS = "Settings";


public static String getProperty(String key) {
String result;
try (jetbrains.exodus.env.Environment env = Environments.newInstance(APP_DIR.getPath())) {
result = env.computeInReadonlyTransaction(txn -> {
final Store store = env.openStore(SETTINGS, StoreConfig.WITHOUT_DUPLICATES, txn);
ByteIterable entry = store.get(txn, StringBinding.stringToEntry(key));
if (entry!=null) {
return StringBinding.entryToString(entry);
} else {
return null;
}
});
}
return result;
}

public static synchronized void setProperty(String key, String value) {
try (jetbrains.exodus.env.Environment env = Environments.newInstance(APP_DIR.getPath())) {
env.executeInTransaction(txn -> {
final Store store = env.openStore(SETTINGS, StoreConfig.WITHOUT_DUPLICATES, txn);
store.put(txn, StringBinding.stringToEntry(key), StringBinding.stringToEntry(value));
});
}
}

public static void saveGenres(String genre) {
if (StringUtils.isNotEmpty(genre) & loadGenres().stream().noneMatch(s -> s.equals(genre))) {
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
entityStore.executeInTransaction(txn -> {
final Entity genreEntity = txn.newEntity(GENRE_ENTITY);
genreEntity.setProperty(GENRE_TITLE, genre);
genreEntity.setProperty(GENRE_CREATED, System.currentTimeMillis());
});
}
}
}

public static ObservableList<String> loadGenres() {
ObservableList<String> genres;
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
genres = entityStore.computeInReadonlyTransaction(txn -> {
ObservableList<String> list = FXCollections.observableArrayList();
StreamSupport.stream(txn.sort(GENRE_ENTITY, GENRE_TITLE, true).spliterator(), false).forEach(g -> list.add((String) g.getProperty(GENRE_TITLE)));
return list;
});
}
return genres;
}

public static void removeGenre(String genre) {
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
entityStore.executeInTransaction(txn -> {
EntityIterable entities = txn.find(GENRE_ENTITY, GENRE_TITLE, genre);
for (Entity entity : entities) {
entity.delete();
}
});
}
}

public static List<Preset> loadPresets() {
List<Preset> presets;
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
presets = entityStore.computeInReadonlyTransaction(txn -> {
List<Preset> list = new ArrayList<>();
EntityIterable all = txn.getAll(PRESET_ENTITY);
for (Entity entity : all) {
Preset preset = bindPreset(entity);
list.add(preset);
}
return list;
});
}
return presets;
}

@NotNull
private static Preset bindPreset(Entity entity) {
String name = (String) entity.getProperty(PRESET_NAME);
String format = (String) entity.getProperty(PRESET_FORMAT);
Integer bitrate = (Integer) entity.getProperty(PRESET_BITRATE);
Integer frequency = (Integer) entity.getProperty(PRESET_FREQUENCY);
Integer channels = (Integer) entity.getProperty(PRESET_CHANNELS);
Integer cutoff = (Integer) entity.getProperty(PRESET_CUTOFF);
Boolean cbr = (Boolean) entity.getProperty(PRESET_CBR);
Integer quality = (Integer) entity.getProperty(PRESET_QUALITY);
Preset preset = new Preset(name, new OutputParameters(Format.instance(format), bitrate, frequency, channels, cutoff, cbr, quality));
return preset;
}

public static void savePreset(Preset preset) {
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
entityStore.executeInTransaction(txn -> {
Entity entity;
EntityIterable entities = txn.find(PRESET_ENTITY, PRESET_NAME, preset.getName());
if (entities.isEmpty()) {
entity = txn.newEntity(PRESET_ENTITY);
} else {
entity = entities.getFirst();
}
entity.setProperty(PRESET_NAME, preset.getName());
entity.setProperty(PRESET_FORMAT, preset.getFormat().format);
entity.setProperty(PRESET_BITRATE, preset.getBitRate());
entity.setProperty(PRESET_FREQUENCY, preset.getFrequency());
entity.setProperty(PRESET_CHANNELS, preset.getChannels());
entity.setProperty(PRESET_CUTOFF, preset.getCutoff());
entity.setProperty(PRESET_CBR, preset.isCbr());
entity.setProperty(PRESET_QUALITY, preset.getVbrQuality());
});
}
}

public static Preset loadPreset(String presetName) {
Preset preset;
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) {
preset = entityStore.computeInReadonlyTransaction(txn -> {
EntityIterable entities = txn.find(PRESET_ENTITY, PRESET_NAME, presetName);
if (entities.isEmpty()) return null;
return bindPreset(entities.getFirst());
});
}
return preset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.apple.eio.FileManager;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -16,7 +15,6 @@
import org.controlsfx.control.Notifications;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.yermak.audiobookconverter.Version;
import uk.yermak.audiobookconverter.fx.ConversionContext;
import uk.yermak.audiobookconverter.fx.JfxEnv;

Expand Down Expand Up @@ -138,13 +136,13 @@ static class VersionChecker implements Runnable {
@Override
public void run() {
try {
String platform = Environment.current.loadAppProperties().getProperty("platform");
String platform = Platform.current.loadAppProperties().getProperty("platform");
if (platform == null) platform = "version";
if ("steam".equals(platform)) return;
String version = readStringFromURL("https://raw.githubusercontent.com/yermak/AudioBookConverter/version/" + platform + ".txt");
if (!Version.getVersionString().equals(StringUtils.trim(version))) {
logger.info("New version found: {}", version);
Platform.runLater(() -> {
javafx.application.Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("New Version Available!");
String path = FileManager.getPathToApplicationBundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static long parseDuration(String info) {
public static void mp4v2UpdateDuration(MediaInfo mediaInfo, String outputFileName) throws IOException {
Process process = null;
try {
ProcessBuilder infoProcessBuilder = new ProcessBuilder(Environment.MP4INFO, outputFileName);
ProcessBuilder infoProcessBuilder = new ProcessBuilder(Platform.MP4INFO, outputFileName);
process = infoProcessBuilder.start();
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamCopier.copy(process.getInputStream(), out);
Expand All @@ -67,7 +67,7 @@ public static void mp4v2UpdateDuration(MediaInfo mediaInfo, String outputFileNam

public static void ffMpegUpdateDuration(MediaInfo mediaInfo, String outputFileName) throws IOException {
final Set<String> AUDIO_CODECS = ImmutableSet.of("mp3", "aac", "wmav2", "flac", "alac", "vorbis", "opus");
FFprobe ffprobe = new FFprobe(Environment.FFPROBE);
FFprobe ffprobe = new FFprobe(Platform.FFPROBE);
FFmpegProbeResult probe = ffprobe.probe(outputFileName);
List<FFmpegStream> streams = probe.getStreams();
for (FFmpegStream stream : streams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FFMediaLoader(List<String> files, ConversionGroup conversionGroup) {
public List<MediaInfo> loadMediaInfo() {
logger.info("Loading media info");
try {
FFprobe ffprobe = new FFprobe(Environment.FFPROBE);
FFprobe ffprobe = new FFprobe(Platform.FFPROBE);
List<MediaInfo> media = new ArrayList<>();
for (String fileName : fileNames) {
Future<MediaInfo> futureLoad = mediaExecutor.submit(new MediaInfoCallable(ffprobe, fileName, conversionGroup));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void optimize() throws InterruptedException {
try {

String[] optimize = {
Environment.FFMPEG,
Platform.FFMPEG,
"-i", tempFile,
"-map", "0:v",
"-map", "0:a",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package uk.yermak.audiobookconverter;

import javafx.application.Platform;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.concurrent.Callable;
Expand All @@ -28,7 +26,7 @@ public ArtWork call() throws Exception {
if (conversionGroup.isOver() || conversionGroup.isStarted() || conversionGroup.isDetached())
throw new InterruptedException("ArtWork loading was interrupted");
String poster = Utils.getTmp(mediaInfo.getUID(), stream, format);
ProcessBuilder pictureProcessBuilder = new ProcessBuilder(Environment.FFMPEG,
ProcessBuilder pictureProcessBuilder = new ProcessBuilder(Platform.FFMPEG,
"-i", mediaInfo.getFileName(),
"-map", "0:" + stream,
"-y",
Expand All @@ -50,7 +48,7 @@ public ArtWork call() throws Exception {
FFMediaLoader.logger.error("ArtWork Error: {}", err);

ArtWorkBean artWorkBean = new ArtWorkBean(poster);
Platform.runLater(() -> {
javafx.application.Platform.runLater(() -> {
if (!conversionGroup.isOver() && !conversionGroup.isStarted() && !conversionGroup.isDetached()) {
AudiobookConverter.getContext().addPosterIfMissingWithDelay(artWorkBean);
}
Expand Down
Loading

0 comments on commit 7e7ad8b

Please sign in to comment.