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

106 lines
3.2 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;
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)]
2020-11-20 17:38:01 +01:00
public class MediaController : BackofficeCollectionController<IMedia, 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
}
2020-11-20 17:38:01 +01:00
public async Task<ListResult<MediaListItem>> GetListByQuery([FromQuery] MediaListItemQuery query) => 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]
2020-12-04 11:07:12 +01:00
public async Task<EntityResult<IMedia>> 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]
2020-11-20 17:38:01 +01:00
public async Task<EntityResult<IMedia>> 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
{
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
});
IList<IMediaFolder> hierarchy = null;
IMediaFolder 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]
2020-11-20 17:38:01 +01:00
public async Task<IActionResult> StreamThumbnail([FromQuery] string id, [FromQuery] bool thumb = true)
{
2020-11-20 17:38:01 +01:00
string path = await Collection.GetSourceById(id, thumb);
2020-09-30 16:56:23 +02:00
2020-11-16 14:48:23 +01:00
if (path == null)
{
return NotFound();
}
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"))
{
using var client = new WebClient();
var content = client.DownloadData(path);
return File(content, contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
2020-09-22 19:28:54 +02:00
string fullPath = Path.Combine(Paths.Root, path?.Trim('/') ?? String.Empty);
2020-09-22 19:28:54 +02:00
if (path == null || !System.IO.File.Exists(fullPath))
{
return NotFound();
}
2020-09-22 19:28:54 +02:00
return File(path, contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
2020-05-15 14:23:47 +02:00
}
}