Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using ELFinder.Connector.Web.Serialization;
using ELFinder.Connector.Web.Serialization.Values;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ELFinder.Connector.ASPNetCore.ActionResults.Data
{

/// <summary>
/// ELFInder Json data result
/// </summary>
public class ELFinderJsonDataResult : JsonResult
{

#region Constructors

/// <summary>
/// Create a new instance
/// </summary>
//public ELFinderJsonDataResult()
//{
//}

/// <summary>
/// Create a new instance
/// </summary>
/// <param name="data">Data</param>
public ELFinderJsonDataResult(object data):base(data)
{
}

#endregion

#region Overrides

/// <summary>
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
/// </summary>
/// <param name="context">The context within which the result is executed.</param><exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception>
public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

var services = context.HttpContext.RequestServices;
var executor = services.GetRequiredService<ELFinderJsonResultExecutor>();
return executor.ExecuteAsync(context, this);
}

/// <summary>
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
/// </summary>
/// <param name="context">The context within which the result is executed.</param><exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception>
public override void ExecuteResult(ActionContext context)
{
ExecuteResultAsync(context);
}

#endregion

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using ELFinder.Connector.Web.Serialization.Values;
using ELFinder.Connector.Web.Serialization;
using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal;
using Microsoft.AspNetCore.Mvc;

namespace ELFinder.Connector.ASPNetCore.ActionResults.Data
{
/// <summary>
/// ELFInder Json Result Executor
/// Executes a <see cref="JsonResult"/> to write to the response.
/// </summary>
public class ELFinderJsonResultExecutor
{
private static readonly string DefaultContentType = new MediaTypeHeaderValue("application/json")
{
Encoding = Encoding.UTF8
}.ToString();

private readonly IArrayPool<char> _charPool;

/// <summary>
/// Creates a new <see cref="JsonResultExecutor"/>.
/// </summary>
/// <param name="writerFactory">The <see cref="IHttpResponseStreamWriterFactory"/>.</param>
/// <param name="logger">The <see cref="ILogger{JsonResultExecutor}"/>.</param>
/// <param name="options">The <see cref="IOptions{MvcJsonOptions}"/>.</param>
/// <param name="charPool">The <see cref="ArrayPool{Char}"/> for creating <see cref="T:char[]"/> buffers.</param>
public ELFinderJsonResultExecutor(
IHttpResponseStreamWriterFactory writerFactory,
ILogger<JsonResultExecutor> logger,
IOptions<MvcJsonOptions> options,
ArrayPool<char> charPool)
{
if (writerFactory == null)
{
throw new ArgumentNullException(nameof(writerFactory));
}

if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

if (charPool == null)
{
throw new ArgumentNullException(nameof(charPool));
}

WriterFactory = writerFactory;
Logger = logger;
Options = options.Value;
_charPool = new JsonArrayPool<char>(charPool);
}

/// <summary>
/// Gets the <see cref="ILogger"/>.
/// </summary>
protected ILogger Logger { get; }

/// <summary>
/// Gets the <see cref="MvcJsonOptions"/>.
/// </summary>
protected MvcJsonOptions Options { get; }

/// <summary>
/// Gets the <see cref="IHttpResponseStreamWriterFactory"/>.
/// </summary>
protected IHttpResponseStreamWriterFactory WriterFactory { get; }

/// <summary>
/// Executes the <see cref="JsonResult"/> and writes the response.
/// </summary>
/// <param name="context">The <see cref="ActionContext"/>.</param>
/// <param name="result">The <see cref="JsonResult"/>.</param>
/// <returns>A <see cref="Task"/> which will complete when writing has completed.</returns>
public Task ExecuteAsync(ActionContext context, JsonResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (result == null)
{
throw new ArgumentNullException(nameof(result));
}

var response = context.HttpContext.Response;

string resolvedContentType = null;
Encoding resolvedContentTypeEncoding = null;
ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
result.ContentType,
response.ContentType,
DefaultContentType,
out resolvedContentType,
out resolvedContentTypeEncoding);

response.ContentType = resolvedContentType;

if (result.StatusCode != null)
{
response.StatusCode = result.StatusCode.Value;
}

var serializerSettings = result.SerializerSettings ?? Options.SerializerSettings;

// Logger.JsonResultExecuting(result.Value);
using (var writer = WriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.ArrayPool = _charPool;
jsonWriter.CloseOutput = false;

var jsonSerializer = GetJsonSerializer(); //JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, result.Value);
}
}

return TaskCache.CompletedTask;
}

/// <summary>
/// Get the Json serializer
/// </summary>
/// <returns>Result</returns>
protected JsonSerializer GetJsonSerializer()
{

// Get default serializer
var serializer = new JsonSerializer();

// Add converters
serializer.Converters.Add(new ELFinderResponseBooleanValueConverter());

// Set contract resolver
serializer.ContractResolver = new ELFinderResponseContractResolver();

// Return it
return serializer;

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using ELFinder.Connector.Commands.Results.Content.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace ELFinder.Connector.ASPNetCore.ActionResults.Files.Common
{

/// <summary>
/// ELFinder content command stream result
/// </summary>
public abstract class ELFinderContentCommandStreamResult<TResult> : FileStreamResult
where TResult : BaseFileContentResult
{

#region Properties

/// <summary>
/// Command result
/// </summary>
public TResult CommandResult { get; }

#endregion

#region Constructors

/// <summary>
/// Create a new instance
/// </summary>
/// <param name="commandResult">Command result</param>
protected ELFinderContentCommandStreamResult(TResult commandResult) :
base(commandResult.GetContentStream(), commandResult.ContentType)
{

// Assign values
CommandResult = commandResult;

}

#endregion

#region Virtual methods

/// <summary>
/// Set content headers
/// </summary>
/// <param name="context">Controller context</param>
/// <param name="response">Response</param>
protected virtual void SetContentHeaders(ActionContext context, HttpResponse response)
{

// Set content type
//context.HttpContext.Response.ContentType= ContentType;
response.ContentType = ContentType;

}

#endregion

#region Overrides

/// <summary>
/// Execute result
/// </summary>
/// <param name="context">Controller context</param>
public override void ExecuteResult(ActionContext context)
{

// Validate context
if (context == null) throw new ArgumentNullException(nameof(context));

// Get response
var response = context.HttpContext.Response;

// Set content headers
SetContentHeaders(context, response);

// Write file
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

var executor = context.HttpContext.RequestServices.GetRequiredService<FileStreamResultExecutor>();
executor.ExecuteAsync(context, this);


}

#endregion

}

}
Loading