Files
mixtape/zero.Web/Controllers/MediaController.cs
T

137 lines
3.8 KiB
C#
Raw Normal View History

2020-05-31 15:01:28 +02:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
2020-09-22 19:28:54 +02:00
using Microsoft.Net.Http.Headers;
2020-05-31 13:39:47 +02:00
using System;
2020-05-15 14:23:47 +02:00
using System.Collections.Generic;
using System.IO;
2020-09-30 16:56:23 +02:00
using System.Net;
2021-11-15 13:15:00 +01:00
using System.Net.Http;
2020-05-15 14:23:47 +02:00
using System.Threading.Tasks;
2020-09-22 19:28:54 +02:00
using zero.Core;
2020-05-15 14:23:47 +02:00
using zero.Core.Api;
2020-11-20 17:38:01 +01:00
using zero.Core.Collections;
2020-05-15 14:23:47 +02:00
using zero.Core.Entities;
using zero.Core.Identity;
using zero.Web.Models;
namespace zero.Web.Controllers
{
[ZeroAuthorize(Permissions.Sections.Media, PermissionsValue.True)]
2021-05-04 17:23:52 +02:00
public class MediaController : BackofficeCollectionController<Media, IMediaCollection>
2020-05-15 14:23:47 +02:00
{
2020-05-18 11:53:23 +02:00
IMediaFolderApi MediaFolderApi;
2020-09-22 19:28:54 +02:00
IPaths Paths;
2020-05-18 11:53:23 +02:00
2020-11-20 17:38:01 +01:00
public MediaController(IMediaCollection collection, IMediaFolderApi mediaFolderApi, IPaths paths) : base(collection)
2020-05-15 14:23:47 +02:00
{
2020-05-18 11:53:23 +02:00
MediaFolderApi = mediaFolderApi;
2020-09-22 19:28:54 +02:00
Paths = paths;
2020-11-20 17:38:01 +01:00
PreviewTransform = (item, model) => model.Icon = "fth-globe";
2020-05-15 14:23:47 +02:00
}
public async Task<ListResult<MediaListItem>> GetListByQuery([FromQuery] MediaListItemQuery query)
{
query.IncludeInactive = true;
return await Collection.GetListByQuery(query);
}
2020-05-15 14:23:47 +02:00
2020-09-02 15:46:54 +02:00
2020-10-29 19:29:30 +01:00
[HttpPost]
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<Media>> Upload(IFormFile file, [FromForm] string folderId) => await Collection.Save(await Collection.Upload(file, folderId));
2020-09-02 15:46:54 +02:00
2020-10-29 19:29:30 +01:00
[HttpPost]
2020-12-04 11:07:12 +01:00
public async Task<Media> UploadTemporary(IFormFile file, [FromForm] string folderId) => await Collection.Upload(file, folderId);
2020-06-02 12:08:38 +02:00
[HttpPost]
2021-05-04 17:23:52 +02:00
public async Task<EntityResult<Media>> Move([FromBody] ActionCopyModel model) => await Collection.Move(model.Id, model.DestinationId);
public async Task<MediaListResultModel> GetAll([FromQuery] MediaListQuery query)
2020-05-15 14:23:47 +02:00
{
query.IncludeInactive = true;
2020-11-20 17:38:01 +01:00
ListResult<MediaListModel> items = (await Collection.GetByQuery(query)).MapTo(x => new MediaListModel()
2020-05-18 12:31:58 +02:00
{
2020-10-12 12:14:09 +02:00
Id = x.Id,
IsFolder = false,
Name = x.Name,
Size = x.Size,
Source = x.PreviewSource ?? x.Source,
Type = x.Type
});
2021-05-04 17:23:52 +02:00
IList<MediaFolder> hierarchy = null;
MediaFolder folder = null;
2020-05-18 12:31:58 +02:00
2020-05-31 13:39:47 +02:00
if (!String.IsNullOrEmpty(query.FolderId))
{
folder = await MediaFolderApi.GetById(query.FolderId);
2020-06-02 12:52:58 +02:00
hierarchy = await MediaFolderApi.GetHierarchy(query.FolderId);
2020-05-31 13:39:47 +02:00
}
return new MediaListResultModel(items, null, folder, hierarchy);
2020-05-15 14:23:47 +02:00
}
[HttpGet]
2021-09-23 15:04:36 +02:00
public async Task<IActionResult> GetSource([FromQuery] string id, [FromQuery] MediaSourceSize size = MediaSourceSize.Original)
{
2021-09-23 15:04:36 +02:00
string path = await Collection.GetSourceById(id, size);
2020-09-30 16:56:23 +02:00
2020-11-16 14:48:23 +01:00
if (path == null)
{
2021-09-27 10:32:36 +02:00
return EmptyImage();
}
if (path.StartsWith("url://"))
{
path = path.Substring(6);
2020-11-16 14:48:23 +01:00
}
2020-09-30 16:56:23 +02:00
var provider = new FileExtensionContentTypeProvider();
string contentType;
if (path == null || !provider.TryGetContentType(Path.GetFileName(path), out contentType))
{
contentType = "application/octet-stream";
}
if (path.StartsWith("http"))
{
2021-09-12 13:00:07 +02:00
try
{
2021-11-15 13:15:00 +01:00
using HttpClient http = new();
var content = await http.GetByteArrayAsync(path);
2021-09-12 13:00:07 +02:00
return File(content, contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
catch
{
2021-09-27 10:32:36 +02:00
return EmptyImage();
2021-09-12 13:00:07 +02:00
}
2020-09-30 16:56:23 +02:00
}
2021-08-11 15:47:42 +02:00
string fullPath = Path.Combine(Paths.WebRoot, path?.Trim('/') ?? String.Empty);
2020-09-22 19:28:54 +02:00
if (path == null || !System.IO.File.Exists(fullPath))
{
2021-09-27 10:32:36 +02:00
return EmptyImage();
}
2020-09-22 19:28:54 +02:00
return File(path, contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
2021-09-27 10:32:36 +02:00
IActionResult EmptyImage()
{
string path = Paths.Map("assets", "empty.png");
if (!System.IO.File.Exists(path))
{
return NotFound();
}
return File("assets/empty.png", "image/png", DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
2020-05-15 14:23:47 +02:00
}
}