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-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-05-15 14:23:47 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
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-02 15:46:54 +02:00
|
|
|
public MediaController(IMediaApi api, IMediaFolderApi mediaFolderApi)
|
2020-05-15 14:23:47 +02:00
|
|
|
{
|
|
|
|
|
Api = api;
|
2020-05-18 11:53:23 +02:00
|
|
|
MediaFolderApi = mediaFolderApi;
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-02 15:46:54 +02:00
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetById([FromQuery] string id) => Edit(await Api.GetById(id));
|
2020-05-15 14:23:47 +02:00
|
|
|
|
|
|
|
|
|
2020-09-02 15:46:54 +02:00
|
|
|
public async Task<IActionResult> GetByIds([FromQuery] string[] ids) => Json(await Api.GetById(ids));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetListByQuery([FromQuery] MediaListItemQuery query) => Json(await Api.GetListByQuery(query));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Save([FromBody] IMedia model) => Json(await Api.Save(model));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Upload(IFormFile file, string folderId) => Json(await Api.Save(await Api.Upload(file, folderId)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> UploadTemporary(IFormFile file, string folderId) => Json(await Api.Upload(file, folderId));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IActionResult> Delete([FromQuery] string id) => Json(await Api.Delete(id));
|
2020-06-02 12:08:38 +02:00
|
|
|
|
|
|
|
|
|
2020-09-03 14:50:41 +02:00
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> Move([FromBody] ActionCopyModel model) => Json(await Api.Move(model.Id, model.DestinationId));
|
|
|
|
|
|
|
|
|
|
|
2020-05-15 14:23:47 +02:00
|
|
|
public async Task<IActionResult> GetAll([FromQuery] MediaListQuery query)
|
|
|
|
|
{
|
2020-09-02 15:46:54 +02:00
|
|
|
ListResult<MediaListModel> items = await Mapper.Map<IMedia, MediaListModel>(await Api.GetByQuery(query));
|
|
|
|
|
IList<IMediaFolder> hierarchy = null;
|
|
|
|
|
IEnumerable<IMediaFolder> folders = new List<IMediaFolder>();
|
|
|
|
|
IMediaFolder folder = null;
|
2020-05-15 14:23:47 +02:00
|
|
|
|
2020-05-18 12:31:58 +02:00
|
|
|
if (query.Page < 2)
|
|
|
|
|
{
|
2020-09-02 15:46:54 +02:00
|
|
|
folders = await MediaFolderApi.GetAll(query.FolderId);
|
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-09-02 15:46:54 +02:00
|
|
|
return Json(new MediaListResultModel(items, null, folder, hierarchy));
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-02 14:12:56 +02:00
|
|
|
[HttpGet]
|
2020-09-15 18:00:37 +02:00
|
|
|
public async Task<IActionResult> StreamThumbnail(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-06-02 14:12:56 +02:00
|
|
|
|
|
|
|
|
if (path == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
|
|
|
string contentType;
|
|
|
|
|
if (!provider.TryGetContentType(Path.GetFileName(path), out contentType))
|
|
|
|
|
{
|
|
|
|
|
contentType = "application/octet-stream";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return File(path, contentType);
|
|
|
|
|
}
|
2020-05-15 14:23:47 +02:00
|
|
|
}
|
|
|
|
|
}
|