Skip to content

Commit

Permalink
optimize: link ass file if exists for solve #1 .
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed Aug 14, 2023
1 parent d1feb14 commit 5e0727a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=run.ikaros.jellyfin
description=A jellyfin plugin for ikaros.
version=0.6.0
version=0.6.1
71 changes: 32 additions & 39 deletions src/main/java/run/ikaros/jellyfin/MediaDirInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -178,7 +179,7 @@ private void linkEpisodeFileAndGenerateNfo(Optional<String> bgmTvIdOp,
String fileName = file.getName();

String epFileAbsolutePath = file.getFsPath();
if(epFileAbsolutePath == null) {
if (epFileAbsolutePath == null) {
log.warn("skip link episode file, episode file path is null for file: {}", file);
return;
}
Expand All @@ -203,7 +204,7 @@ private void linkEpisodeFileAndGenerateNfo(Optional<String> bgmTvIdOp,
targetEpisodeFile.getAbsolutePath(), epFileAbsolutePath, e);
}
// generate nfo file
Integer sequence = episode.getSequence();
Integer sequence = episode.getSequence();
if (!episodeNfoFile.exists()) {
XmlUtils.generateJellyfinEpisodeNfoXml(episodeNfoFile.getAbsolutePath(),
episode.getDescription(),
Expand All @@ -216,52 +217,44 @@ private void linkEpisodeFileAndGenerateNfo(Optional<String> bgmTvIdOp,
}

// link ass file if exists
String originalFileNameWithNoPostfix =
fileName.substring(0, fileName.indexOf("."));
log.debug("originalFileNameWithNoPostfix: {}", originalFileNameWithNoPostfix);
fileOperate.findAllByNameLikeAndType(originalFileNameWithNoPostfix,
FileType.DOCUMENT)
.filter(
entity -> {
String fileEntityName = entity.getName();
String postfix = fileEntityName.substring(fileEntityName.indexOf(".") + 1);
boolean result = postfix.endsWith("ass");
log.debug("fileEntityName: [{}], postfix: [{}], end with ass: [{}].",
fileEntityName, postfix, result);
return result;
})
.subscribe(entity -> {
String fsPath = entity.getFsPath();
log.debug("ass file file system path: {}", fsPath);
if (StringUtils.hasText(fsPath)) {
File assFile = new File(fsPath);
log.debug("ass file exists: {}", assFile.exists());
if (assFile.exists()) {
File targetAssFile = new File(subjectDirAbsolutePath
+ File.separatorChar + entity.getName());
try {
log.debug("targetAssFile exists: {}.", targetAssFile.exists());
if (!targetAssFile.exists()) {
Files.createLink(targetAssFile.toPath(), assFile.toPath());
log.debug("create jellyfin episode subtitle hard link success, "
+ "link={}, existing={}",
targetAssFile.getAbsolutePath(), fsPath);
}
} catch (IOException e) {
log.debug("create jellyfin episode subtitle hard link fail, "
if (Objects.nonNull(episode.getResources()) && !episode.getResources().isEmpty()
&& Objects.nonNull(episode.getResources().get(0))
&& Objects.nonNull(episode.getResources().get(0).getSubtitles())
&& !episode.getResources().get(0).getSubtitles().isEmpty()) {
List<Subtitle> subtitles = episode.getResources().get(0).getSubtitles();
for (Subtitle subtitle : subtitles) {
String subtitleName = subtitle.getName();
String url = subtitle.getUrl();
String assFilePath = url.startsWith("http") ? "" : ikarosProperties.getWorkDir() + url;
File assFile = new File(assFilePath);
log.debug("ass file exists: {}", assFile.exists());
if (assFile.exists()) {
File targetAssFile = new File(subjectDirAbsolutePath
+ File.separatorChar + subtitleName);
try {
log.debug("targetAssFile exists: {}.", targetAssFile.exists());
if (!targetAssFile.exists()) {
Files.createLink(targetAssFile.toPath(), assFile.toPath());
log.debug("create jellyfin episode subtitle hard link success, "
+ "link={}, existing={}",
targetAssFile.getAbsolutePath(), fsPath, e);
targetAssFile.getAbsolutePath(), subtitleName);
}
} catch (IOException e) {
log.debug("create jellyfin episode subtitle hard link fail, "
+ "link={}, existing={}",
targetAssFile.getAbsolutePath(), subtitleName, e);
}
}
});
}
}

} else {
// 剧集文件不存在,可能是已经推送到了远端
// 如果此时媒体目录的剧集文件和nfo文件存在,则删除
if(targetEpisodeFile.exists()) {
if (targetEpisodeFile.exists()) {
targetEpisodeFile.delete();
}
if(episodeNfoFile.exists()) {
if (episodeNfoFile.exists()) {
episodeNfoFile.delete();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: PluginJellyfin
# plugin entry class that extends BasePlugin
clazz: run.ikaros.jellyfin.JellyfinPlugin
# plugin 'version' is a valid semantic version string (see semver.org).
version: 0.6.0
version: 0.6.1
requires: ">=0.6.0"
author:
name: Ikaros OSS Team
Expand Down

0 comments on commit 5e0727a

Please sign in to comment.