ThumbnailManager: Handling different file extensions

This commit is contained in:
Zoe Roux 2021-08-05 15:41:11 +02:00
parent 1a33f38384
commit 559160ad7b

View File

@ -3,6 +3,7 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Kyoo.Controllers namespace Kyoo.Controllers
@ -49,8 +50,10 @@ namespace Kyoo.Controllers
{ {
AsyncRef<string> mime = new(); AsyncRef<string> mime = new();
await using Stream reader = await _files.GetReader(url, mime); await using Stream reader = await _files.GetReader(url, mime);
// TODO use this mime type to guess the file extension. string extension = new FileExtensionContentTypeProvider()
await using Stream local = await _files.NewFile(localPath); .Mappings.FirstOrDefault(x => x.Value == mime.Value)
.Key;
await using Stream local = await _files.NewFile(localPath + extension);
await reader.CopyToAsync(local); await reader.CopyToAsync(local);
return true; return true;
} }
@ -95,6 +98,8 @@ namespace Kyoo.Controllers
{ {
if (item == null) if (item == null)
throw new ArgumentNullException(nameof(item)); throw new ArgumentNullException(nameof(item));
string directory = await _files.GetExtraDirectory(item);
string imageName = imageID switch string imageName = imageID switch
{ {
Images.Poster => "poster", Images.Poster => "poster",
@ -103,16 +108,19 @@ namespace Kyoo.Controllers
Images.Trailer => "trailer", Images.Trailer => "trailer",
_ => $"{imageID}" _ => $"{imageID}"
}; };
imageName = item switch switch (item)
{ {
Season season => $"season-{season.SeasonNumber}-{imageName}", case Season season:
Episode episode => _files.Combine("Thumbnails", imageName = $"season-{season.SeasonNumber}-{imageName}";
$"{Path.GetFileNameWithoutExtension(episode.Path)}-{imageName}"), break;
_ => imageName case Episode episode:
}; directory = await _files.CreateDirectory(_files.Combine(directory, "Thumbnails"));
imageName = $"{Path.GetFileNameWithoutExtension(episode.Path)}-{imageName}";
break;
}
return _files.Combine(await _files.GetExtraDirectory(item), imageName); return _files.Combine(directory, imageName);
} }
/// <inheritdoc /> /// <inheritdoc />