2020-05-31 15:01:28 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-06-02 14:12:56 +02:00
|
|
|
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;
|
2020-06-02 14:12:56 +02:00
|
|
|
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;
|
|
|
|
|
using zero.Core.Entities;
|
|
|
|
|
using zero.Core.Identity;
|
|
|
|
|
using zero.Web.Models;
|
|
|
|
|
|
|
|
|
|
namespace zero.Web.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ZeroAuthorize(Permissions.Sections.Media, PermissionsValue.True)]
|
|
|
|
|
public class MediaController : BackofficeController
|
|
|
|
|
{
|
|
|
|
|
IMediaApi Api;
|
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-09-22 19:28:54 +02:00
|
|
|
public MediaController(IMediaApi api, IMediaFolderApi mediaFolderApi, IPaths paths)
|
2020-05-15 14:23:47 +02:00
|
|
|
{
|
|
|
|
|
Api = api;
|
2020-05-18 11:53:23 +02:00
|
|
|
MediaFolderApi = mediaFolderApi;
|
2020-09-22 19:28:54 +02:00
|
|
|
Paths = paths;
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:46:54 +02:00
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EditModel<IMedia>> GetById([FromQuery] string id) => Edit(await Api.GetById(id));
|
2020-05-15 14:23:47 +02:00
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<Dictionary<string, IMedia>> GetByIds([FromQuery] string[] ids) => await Api.GetById(ids);
|
2020-09-02 15:46:54 +02:00
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<ListResult<MediaListItem>> GetListByQuery([FromQuery] MediaListItemQuery query) => await Api.GetListByQuery(query);
|
2020-09-02 15:46:54 +02:00
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult<IMedia>> Save([FromBody] IMedia model) => await Api.Save(model);
|
2020-09-02 15:46:54 +02:00
|
|
|
|
2020-10-29 19:29:30 +01:00
|
|
|
[HttpPost]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult<IMedia>> Upload(IFormFile file, string folderId) => await Api.Save(await Api.Upload(file, folderId));
|
2020-09-02 15:46:54 +02:00
|
|
|
|
2020-10-29 19:29:30 +01:00
|
|
|
[HttpPost]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<Media> UploadTemporary(IFormFile file, string folderId) => await Api.Upload(file, folderId);
|
2020-09-02 15:46:54 +02:00
|
|
|
|
2020-10-29 19:29:30 +01:00
|
|
|
[HttpDelete]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult<IMedia>> Delete([FromQuery] string id) => await Api.Delete(id);
|
2020-06-02 12:08:38 +02:00
|
|
|
|
2020-09-03 14:50:41 +02:00
|
|
|
[HttpPost]
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<EntityResult<IMedia>> Move([FromBody] ActionCopyModel model) => await Api.Move(model.Id, model.DestinationId);
|
2020-09-03 14:50:41 +02:00
|
|
|
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
public async Task<MediaListResultModel> GetAll([FromQuery] MediaListQuery query)
|
2020-05-15 14:23:47 +02:00
|
|
|
{
|
2020-10-12 12:14:09 +02:00
|
|
|
ListResult<MediaListModel> items = (await Api.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
|
|
|
}
|
|
|
|
|
|
2020-10-27 15:47:23 +01:00
|
|
|
return new MediaListResultModel(items, null, folder, hierarchy);
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-02 14:12:56 +02:00
|
|
|
[HttpGet]
|
2020-10-29 19:29:30 +01:00
|
|
|
public async Task<IActionResult> StreamThumbnail([FromQuery] string id, [FromQuery] bool thumb = true)
|
2020-06-02 14:12:56 +02:00
|
|
|
{
|
2020-09-15 18:00:37 +02:00
|
|
|
string path = await Api.GetSourceById(id, thumb);
|
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-06-02 14:12:56 +02:00
|
|
|
|
2020-09-22 19:28:54 +02:00
|
|
|
if (path == null || !System.IO.File.Exists(fullPath))
|
2020-06-02 14:12:56 +02:00
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 19:28:54 +02:00
|
|
|
return File(path, contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
|
2020-06-02 14:12:56 +02:00
|
|
|
}
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
}
|