mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-08 16:58:50 +01:00
Initial check-in
This commit is contained in:
44
MediaBrowser.Controller/Resolvers/AudioResolver.cs
Normal file
44
MediaBrowser.Controller/Resolvers/AudioResolver.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.IO;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Controller.Resolvers
|
||||
{
|
||||
public class AudioResolver : BaseItemResolver<Audio>
|
||||
{
|
||||
protected override Audio Resolve(ItemResolveEventArgs args)
|
||||
{
|
||||
if (!args.IsFolder)
|
||||
{
|
||||
if (IsAudioFile(args.Path))
|
||||
{
|
||||
return new Audio();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool IsAudioFile(string path)
|
||||
{
|
||||
string extension = Path.GetExtension(path).ToLower();
|
||||
|
||||
switch (extension)
|
||||
{
|
||||
case ".mp3":
|
||||
case ".wma":
|
||||
case ".acc":
|
||||
case ".flac":
|
||||
case ".m4a":
|
||||
case ".m4b":
|
||||
case ".wav":
|
||||
case ".ape":
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
146
MediaBrowser.Controller/Resolvers/BaseItemResolver.cs
Normal file
146
MediaBrowser.Controller/Resolvers/BaseItemResolver.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace MediaBrowser.Controller.Resolvers
|
||||
{
|
||||
public abstract class BaseItemResolver<T> : IBaseItemResolver
|
||||
where T : BaseItem, new ()
|
||||
{
|
||||
protected virtual T Resolve(ItemResolveEventArgs args)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
|
||||
{
|
||||
// If the subclass didn't specify this
|
||||
if (string.IsNullOrEmpty(item.Path))
|
||||
{
|
||||
item.Path = args.Path;
|
||||
}
|
||||
|
||||
Folder parentFolder = args.Parent as Folder;
|
||||
|
||||
if (parentFolder != null)
|
||||
{
|
||||
item.Parent = parentFolder;
|
||||
}
|
||||
|
||||
item.Id = Kernel.GetMD5(item.Path);
|
||||
|
||||
PopulateImages(item, args);
|
||||
PopulateLocalTrailers(item, args);
|
||||
}
|
||||
|
||||
public BaseItem ResolvePath(ItemResolveEventArgs args)
|
||||
{
|
||||
T item = Resolve(args);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
SetItemValues(item, args);
|
||||
|
||||
EnsureName(item);
|
||||
EnsureDates(item);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private void EnsureName(T item)
|
||||
{
|
||||
// If the subclass didn't supply a name, add it here
|
||||
if (string.IsNullOrEmpty(item.Name))
|
||||
{
|
||||
item.Name = Path.GetFileNameWithoutExtension(item.Path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void EnsureDates(T item)
|
||||
{
|
||||
// If the subclass didn't supply dates, add them here
|
||||
if (item.DateCreated == DateTime.MinValue)
|
||||
{
|
||||
item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
|
||||
}
|
||||
|
||||
if (item.DateModified == DateTime.MinValue)
|
||||
{
|
||||
item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void PopulateImages(T item, ItemResolveEventArgs args)
|
||||
{
|
||||
List<string> backdropFiles = new List<string>();
|
||||
|
||||
foreach (KeyValuePair<string,FileAttributes> file in args.FileSystemChildren)
|
||||
{
|
||||
if (file.Value.HasFlag(FileAttributes.Directory))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string filePath = file.Key;
|
||||
|
||||
string ext = Path.GetExtension(filePath);
|
||||
|
||||
if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string name = Path.GetFileNameWithoutExtension(filePath);
|
||||
|
||||
if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.PrimaryImagePath = filePath;
|
||||
}
|
||||
else if (name.StartsWith("backdrop", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
backdropFiles.Add(filePath);
|
||||
}
|
||||
if (name.Equals("logo", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.LogoImagePath = filePath;
|
||||
}
|
||||
if (name.Equals("banner", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.BannerImagePath = filePath;
|
||||
}
|
||||
if (name.Equals("art", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.ArtImagePath = filePath;
|
||||
}
|
||||
if (name.Equals("thumb", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
item.ThumbnailImagePath = filePath;
|
||||
}
|
||||
}
|
||||
|
||||
item.BackdropImagePaths = backdropFiles;
|
||||
}
|
||||
|
||||
protected virtual void PopulateLocalTrailers(T item, ItemResolveEventArgs args)
|
||||
{
|
||||
var trailerPath = args.GetFolderByName("trailers");
|
||||
|
||||
if (trailerPath.HasValue)
|
||||
{
|
||||
string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Key, "*", SearchOption.TopDirectoryOnly);
|
||||
|
||||
item.LocalTrailers = allFiles.Select(f => Kernel.Instance.ItemController.GetItem(f)).OfType<Video>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IBaseItemResolver
|
||||
{
|
||||
BaseItem ResolvePath(ItemResolveEventArgs args);
|
||||
}
|
||||
}
|
||||
45
MediaBrowser.Controller/Resolvers/FolderResolver.cs
Normal file
45
MediaBrowser.Controller/Resolvers/FolderResolver.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Controller.Xml;
|
||||
|
||||
namespace MediaBrowser.Controller.Resolvers
|
||||
{
|
||||
public class FolderResolver : BaseFolderResolver<Folder>
|
||||
{
|
||||
protected override Folder Resolve(ItemResolveEventArgs args)
|
||||
{
|
||||
if (args.IsFolder)
|
||||
{
|
||||
return new Folder();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseFolderResolver<T> : BaseItemResolver<T>
|
||||
where T : Folder, new ()
|
||||
{
|
||||
protected override void SetItemValues(T item, ItemResolveEventArgs args)
|
||||
{
|
||||
base.SetItemValues(item, args);
|
||||
|
||||
item.IsRoot = args.Parent == null;
|
||||
|
||||
PopulateFolderMetadata(item, args);
|
||||
}
|
||||
|
||||
private void PopulateFolderMetadata(Folder folder, ItemResolveEventArgs args)
|
||||
{
|
||||
var metadataFile = args.GetFileByName("folder.xml");
|
||||
|
||||
if (metadataFile.HasValue)
|
||||
{
|
||||
new FolderXmlParser().Fetch(folder, metadataFile.Value.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
114
MediaBrowser.Controller/Resolvers/VideoResolver.cs
Normal file
114
MediaBrowser.Controller/Resolvers/VideoResolver.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System.IO;
|
||||
using MediaBrowser.Controller.Events;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Controller.Resolvers
|
||||
{
|
||||
public class VideoResolver : BaseVideoResolver<Video>
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class BaseVideoResolver<T> : BaseItemResolver<T>
|
||||
where T : Video, new()
|
||||
{
|
||||
protected override T Resolve(ItemResolveEventArgs args)
|
||||
{
|
||||
if (!args.IsFolder)
|
||||
{
|
||||
if (IsVideoFile(args.Path))
|
||||
{
|
||||
return new T()
|
||||
{
|
||||
VideoType = VideoType.VideoFile,
|
||||
Path = args.Path
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
T item = ResolveFromFolderName(args.Path);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, FileAttributes> folder in args.FileSystemChildren)
|
||||
{
|
||||
if (!folder.Value.HasFlag(FileAttributes.Directory))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
item = ResolveFromFolderName(folder.Key);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private T ResolveFromFolderName(string folder)
|
||||
{
|
||||
if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
|
||||
{
|
||||
return new T()
|
||||
{
|
||||
VideoType = VideoType.DVD,
|
||||
Path = Path.GetDirectoryName(folder)
|
||||
};
|
||||
}
|
||||
if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
|
||||
{
|
||||
return new T()
|
||||
{
|
||||
VideoType = VideoType.BluRay,
|
||||
Path = Path.GetDirectoryName(folder)
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool IsVideoFile(string path)
|
||||
{
|
||||
string extension = Path.GetExtension(path).ToLower();
|
||||
|
||||
switch (extension)
|
||||
{
|
||||
case ".mkv":
|
||||
case ".m2ts":
|
||||
case ".iso":
|
||||
case ".ts":
|
||||
case ".rmvb":
|
||||
case ".mov":
|
||||
case ".avi":
|
||||
case ".mpg":
|
||||
case ".mpeg":
|
||||
case ".wmv":
|
||||
case ".mp4":
|
||||
case ".divx":
|
||||
case ".dvr-ms":
|
||||
case ".wtv":
|
||||
case ".ogm":
|
||||
case ".ogv":
|
||||
case ".asf":
|
||||
case ".m4v":
|
||||
case ".flv":
|
||||
case ".f4v":
|
||||
case ".3gp":
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user