jellyfin-server/MediaBrowser.Providers/ImagesByName/ImageUtils.cs

96 lines
3.1 KiB
C#
Raw Normal View History

2016-03-27 21:11:27 +00:00
using MediaBrowser.Common.Net;
2013-12-30 16:32:01 +00:00
using MediaBrowser.Controller.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2013-12-30 16:32:01 +00:00
namespace MediaBrowser.Providers.ImagesByName
{
public static class ImageUtils
{
/// <summary>
/// Ensures the list.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="file">The file.</param>
/// <param name="httpClient">The HTTP client.</param>
/// <param name="fileSystem">The file system.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2017-04-29 06:22:33 +00:00
public static async Task<string> EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, CancellationToken cancellationToken)
2013-12-30 16:32:01 +00:00
{
2015-10-04 03:38:46 +00:00
var fileInfo = fileSystem.GetFileInfo(file);
2013-12-30 16:32:01 +00:00
if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
{
2017-04-29 06:22:33 +00:00
var temp = await httpClient.GetTempFile(new HttpRequestOptions
2013-12-30 16:32:01 +00:00
{
2017-04-29 06:22:33 +00:00
CancellationToken = cancellationToken,
Progress = new Progress<double>(),
Url = url
2013-12-30 16:32:01 +00:00
2017-04-29 06:22:33 +00:00
}).ConfigureAwait(false);
2013-12-30 16:32:01 +00:00
2017-05-04 18:14:45 +00:00
fileSystem.CreateDirectory(fileSystem.GetDirectoryName(file));
2013-12-30 16:32:01 +00:00
2017-04-29 06:22:33 +00:00
try
{
fileSystem.CopyFile(temp, file, true);
2013-12-30 16:32:01 +00:00
}
2017-04-29 06:22:33 +00:00
catch
2013-12-30 16:32:01 +00:00
{
2017-04-29 06:22:33 +00:00
2013-12-30 16:32:01 +00:00
}
2017-04-29 06:22:33 +00:00
return temp;
2013-12-30 16:32:01 +00:00
}
2017-04-29 06:22:33 +00:00
return file;
2013-12-30 16:32:01 +00:00
}
public static string FindMatch(IHasImages item, IEnumerable<string> images)
{
var name = GetComparableName(item.Name);
return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
}
private static string GetComparableName(string name)
{
return name.Replace(" ", string.Empty)
.Replace(".", string.Empty)
.Replace("&", string.Empty)
.Replace("!", string.Empty)
2014-01-09 04:44:51 +00:00
.Replace(",", string.Empty)
.Replace("/", string.Empty);
2013-12-30 16:32:01 +00:00
}
public static IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
2013-12-30 16:32:01 +00:00
{
using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
2013-12-30 16:32:01 +00:00
{
using (var reader = new StreamReader(fileStream))
2013-12-30 16:32:01 +00:00
{
var lines = new List<string>();
2013-12-30 16:32:01 +00:00
while (!reader.EndOfStream)
2013-12-30 16:50:57 +00:00
{
var text = reader.ReadLine();
if (!string.IsNullOrWhiteSpace(text))
{
lines.Add(text);
}
2013-12-30 16:50:57 +00:00
}
2013-12-30 16:32:01 +00:00
return lines;
}
2013-12-30 16:32:01 +00:00
}
}
}
}