[IMPL] MkvFileCollector + FileCollector interface + new structure

This commit is contained in:
RatzzFatzz
2020-03-06 14:11:35 +01:00
parent 5c36511774
commit 2d5bd87d00
7 changed files with 109 additions and 3 deletions

View File

@@ -0,0 +1,4 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger;
public class AttributeUpdaterKernel {
}

View File

@@ -0,0 +1,4 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
public class AttributeConfig {
}

View File

@@ -0,0 +1,12 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import java.io.File;
import java.util.List;
public interface FileCollector {
List<File> loadFiles(String path);
List<FileAttribute> loadAttributes(File file);
}

View File

@@ -0,0 +1,86 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import com.fasterxml.jackson.databind.ObjectMapper;
import config.MKVToolProperties;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Log4j2
public class MkvFileCollector implements FileCollector {
private ObjectMapper mapper = new ObjectMapper();
/**
* @param path Is entered path, which leads to one file directly or a directory which will be loaded recursive
* @return list of all files within the directory, if it's only a file, the file will be returned in a list
*/
@Override
public List<File> loadFiles(String path) {
File file = new File(path);
if(file.isFile() && file.getAbsolutePath().endsWith(".mkv")){
return new ArrayList<File>() {{
add(file);
}};
}else if(file.isDirectory()){
try(Stream<Path> paths = Files.walk(Paths.get(path))){
return paths
.filter(Files::isRegularFile)
.map(Path::toFile)
.filter(f -> f.getAbsolutePath().endsWith(".mkv"))
.collect(Collectors.toList());
}catch(IOException e){
log.error("Couldn't find file or directory!", e);
return null;
}
}else{
return null;
}
}
/**
* @param file Takes the file from which the attributes will be returned
* @return list of all important attributes
*/
@Override
public List<FileAttribute> loadAttributes(File file) {
Map<String, Object> jsonMap;
List<FileAttribute> fileAttributes = new ArrayList<>();
try(InputStream inputStream =
Runtime.getRuntime().exec("\"" + MKVToolProperties.getInstance().getMkvmergePath()
+ "\" --identify --identification-format json \"" + file.getAbsolutePath() + "\"").getInputStream()){
jsonMap = mapper.readValue(inputStream, Map.class);
List<Map<String, Object>> tracks = (List<Map<String, Object>>) jsonMap.get("tracks");
for(Map<String, Object> attribute : tracks){
if(! "video".equals(attribute.get("type"))){
Map<String, Object> properties = (Map<String, Object>) attribute.get("properties");
fileAttributes.add(new FileAttribute(
(int) properties.get("number"),
(String) properties.get("language"),
(String) properties.get("track_name"),
(Boolean) properties.get("default_track"),
(Boolean) properties.get("forced_track"),
(String) attribute.get("type")));
}
}
}catch(IOException e){
log.error("File could not be found or loaded!");
}
return fileAttributes;
}
private boolean verifyFile(File file) {
return false;
}
}

View File

@@ -0,0 +1,57 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Log4j2
@Getter
public class FileAttribute {
private int id;
private String language;
private String trackName;
private boolean defaultTrack;
private boolean forcedTrack;
private String type;
public FileAttribute(int id, String language, String trackName, boolean defaultTrack, boolean forcedTrack, String type) {
this.id = id;
this.language = language;
this.trackName = trackName;
this.defaultTrack = defaultTrack;
this.forcedTrack = forcedTrack;
this.type = type;
}
public static boolean pathIsValid(String path) {
File file = new File(path);
if(file.isFile()){
return file.getAbsolutePath().endsWith(".mkv");
}
if(file.isDirectory()){
try(Stream<Path> paths = Files.walk(Paths.get(path))){
List<String> allPaths = paths
.filter(Files::isRegularFile)
.map(f -> f.toAbsolutePath().toString())
.collect(Collectors.toList());
for(String filePath : allPaths){
if(! filePath.endsWith(".mkv")){
return false;
}
}
}catch(IOException e){
log.error("Couldn't find file or directory!", e);
}
return true;
}
return false;
}
}