2021-11-25 16:01:38 +01:00
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace zero.Media;
|
2021-11-25 15:38:36 +01:00
|
|
|
|
|
|
|
|
public class MediaManagement : IMediaManagement
|
|
|
|
|
{
|
|
|
|
|
protected IMediaFileSystem FileSystem { get; set; }
|
|
|
|
|
|
|
|
|
|
protected IMediaStore Store { get; set; }
|
|
|
|
|
|
2021-11-25 16:01:38 +01:00
|
|
|
protected IMediaCreator Creator { get; set; }
|
2021-11-25 15:38:36 +01:00
|
|
|
|
2021-11-25 16:01:38 +01:00
|
|
|
|
|
|
|
|
public MediaManagement(IMediaFileSystem fileSystem, IMediaStore store, IMediaCreator creator)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
FileSystem = fileSystem;
|
|
|
|
|
Store = store;
|
2021-11-25 16:01:38 +01:00
|
|
|
Creator = creator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string GetPublicFilePath(Media file, string thumbnailKey = null)
|
|
|
|
|
{
|
|
|
|
|
string path = file?.Path;
|
|
|
|
|
|
|
|
|
|
if (!thumbnailKey.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
2021-12-18 01:51:38 +01:00
|
|
|
path = file?.ImageMeta?.Thumbnails?.GetValueOrDefault(thumbnailKey);
|
2021-11-25 16:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (path.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FileSystem.MapToPublicPath(path);
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-09 14:36:30 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Stream> GetFileStream(Media file, string thumbnailKey = null)
|
|
|
|
|
{
|
|
|
|
|
string path = file?.Path;
|
|
|
|
|
|
|
|
|
|
if (!thumbnailKey.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
2021-12-18 01:51:38 +01:00
|
|
|
path = file?.ImageMeta?.Thumbnails?.GetValueOrDefault(thumbnailKey);
|
2021-12-09 14:36:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (path.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await FileSystem.StreamFile(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 15:38:36 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Media> GetFile(string id)
|
|
|
|
|
{
|
|
|
|
|
Media file = await Store.Load(id);
|
2021-12-16 16:59:27 +01:00
|
|
|
return file != null && !file.IsFolder ? file : null;
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> UpdateFile(Media file)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
// TODO check new file/image/media
|
|
|
|
|
return await Store.Update(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> DeleteFile(Media file)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
// TODO delete in file system
|
|
|
|
|
return await Store.Delete(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 16:01:38 +01:00
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> UploadFile(Stream fileStream, string filename, string folderId = null, CancellationToken cancellationToken = default)
|
2021-11-25 16:01:38 +01:00
|
|
|
{
|
2021-12-18 16:24:07 +01:00
|
|
|
Result<Media> result = await Creator.UploadFile(fileStream, filename, folderId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await Store.Create(result.Model);
|
2021-11-25 16:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-25 15:38:36 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Media> GetFolder(string id)
|
|
|
|
|
{
|
|
|
|
|
Media folder = await Store.Load(id);
|
2021-12-16 16:59:27 +01:00
|
|
|
return folder != null && folder.IsFolder ? folder : null;
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> CreateFolder(Media folder)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
folder.IsActive = true;
|
2021-12-16 16:59:27 +01:00
|
|
|
folder.IsFolder = true;
|
2021-11-25 15:38:36 +01:00
|
|
|
return await Store.Create(folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> CreateFolder(string name, string parentId = null)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
Media media = await Store.Empty();
|
|
|
|
|
media.Name = name;
|
|
|
|
|
media.ParentId = parentId;
|
|
|
|
|
return await CreateFolder(media);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<Media>> UpdateFolder(Media folder)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
return await Store.Update(folder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-11-26 15:47:11 +01:00
|
|
|
public virtual async Task<Result<string[]>> DeleteFolder(Media folder)
|
2021-11-25 15:38:36 +01:00
|
|
|
{
|
|
|
|
|
// TODO recursive
|
|
|
|
|
return await Store.DeleteWithDescendants(folder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface IMediaManagement
|
|
|
|
|
{
|
2021-11-25 16:01:38 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get publicly accessible file path for a media file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">The media file</param>
|
|
|
|
|
/// <param name="thumbnailKey">An optional thumbnail key which returns the path to a generated thumbnail</param>
|
|
|
|
|
string GetPublicFilePath(Media file, string thumbnailKey = null);
|
|
|
|
|
|
2021-12-09 14:36:30 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get file stream for a media file (stream has to be disposed manually)
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Stream> GetFileStream(Media file, string thumbnailKey = null);
|
|
|
|
|
|
2021-11-26 11:21:49 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get a media file by id
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Media> GetFile(string id);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update and store a media file
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> UpdateFile(Media file);
|
2021-11-26 11:21:49 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a media file (collection entry as well as physical file)
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> DeleteFile(Media file);
|
2021-11-26 11:21:49 +01:00
|
|
|
|
2021-11-25 16:01:38 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Uploads a file and persists it
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> UploadFile(Stream fileStream, string filename, string folderId = null, CancellationToken cancellationToken = default);
|
2021-11-25 16:01:38 +01:00
|
|
|
|
2021-11-25 15:38:36 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get a media folder by id
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Media> GetFolder(string id);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new media folder
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> CreateFolder(Media folder);
|
2021-11-25 15:38:36 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new media folder
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> CreateFolder(string name, string parentId = null);
|
2021-11-25 15:38:36 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Rename and store a media folder
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<Media>> UpdateFolder(Media folder);
|
2021-11-25 15:38:36 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a folder, as well as all descendant folders and files
|
|
|
|
|
/// </summary>
|
2021-11-26 15:47:11 +01:00
|
|
|
Task<Result<string[]>> DeleteFolder(Media folder);
|
2021-11-25 15:38:36 +01:00
|
|
|
}
|