-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
454 additions
and
410 deletions.
There are no files selected for viewing
285 changes: 149 additions & 136 deletions
285
src/Downloader.DummyHttpServer/Controllers/DummyFileController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,171 +1,184 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Downloader.DummyHttpServer.Controllers | ||
namespace Downloader.DummyHttpServer.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
[DummyApiExceptionFilter] | ||
public class DummyFileController : ControllerBase | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
[DummyApiExceptionFilter] | ||
public class DummyFileController : ControllerBase | ||
private readonly ILogger<DummyFileController> _logger; | ||
|
||
public DummyFileController(ILogger<DummyFileController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Return the ordered bytes array according to the size. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}")] | ||
public IActionResult GetFile(long size) | ||
{ | ||
private readonly ILogger<DummyFileController> _logger; | ||
_logger.LogTrace($"file/size/{size}"); | ||
var data = new DummyLazyStream(DummyDataType.Order, size); | ||
return File(data, "application/octet-stream", true); | ||
} | ||
|
||
public DummyFileController(ILogger<DummyFileController> logger) | ||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <param name="fillByte">single byte value to fill all of file data</param> | ||
/// <returns>File stream</returns> | ||
[Route("noheader/file/{fileName}")] | ||
public IActionResult GetFileWithNameNoHeader(string fileName, [FromQuery] long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream result; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger = logger; | ||
_logger.LogTrace($"noheader/file/{fileName}?size={size}&fillByte={fillByte}"); | ||
result = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Return the ordered bytes array according to the size. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}")] | ||
public IActionResult GetFile(long size) | ||
else | ||
{ | ||
_logger.LogTrace($"file/size/{size}"); | ||
var data = new DummyLazyStream(DummyDataType.Order, size); | ||
return File(data, "application/octet-stream", true); | ||
_logger.LogTrace($"noheader/file/{fileName}?size={size}"); | ||
result = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <param name="fillByte">single byte value to fill all of file data</param> | ||
/// <returns>File stream</returns> | ||
[Route("noheader/file/{fileName}")] | ||
public IActionResult GetFileWithNameNoHeader(string fileName, [FromQuery] long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream result; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"noheader/file/{fileName}?size={size}&fillByte={fillByte}"); | ||
result = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
else | ||
{ | ||
_logger.LogTrace($"noheader/file/{fileName}?size={size}"); | ||
result = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
return Ok(result); // return stream without header data | ||
} | ||
|
||
return Ok(result); // return stream without header data | ||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <param name="fillByte">single byte value to fill all of file data</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}")] | ||
public IActionResult GetFileWithName(string fileName, [FromQuery] long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}?size={size}&fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <param name="fillByte">single byte value to fill all of file data</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}")] | ||
public IActionResult GetFileWithName(string fileName, [FromQuery] long size, [FromQuery] byte? fillByte = null) | ||
else | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}?size={size}&fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
else | ||
{ | ||
_logger.LogTrace($"file/{fileName}?size={size}"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
return File(fileData, "application/octet-stream", true); | ||
_logger.LogTrace($"file/{fileName}?size={size}"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header content-length and filename. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Size of the File</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/size/{size}")] | ||
public IActionResult GetFileWithContentDisposition(string fileName, long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}?fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
else | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
return File(fileData, "application/octet-stream", true); | ||
} | ||
|
||
return File(fileData, "application/octet-stream", fileName, true); | ||
/// <summary> | ||
/// Return the file stream with header content-length and filename. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Size of the File</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/size/{size}")] | ||
public IActionResult GetFileWithContentDisposition(string fileName, long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}?fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header content-length and filename. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Size of the File</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/size/{size}/norange")] | ||
public IActionResult GetFileWithNoAcceptRange(string fileName, long size, [FromQuery] byte? fillByte = null) | ||
else | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}/norange?fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
else | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}/norange"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
return File(fileData, "application/octet-stream", fileName, false); | ||
_logger.LogTrace($"file/{fileName}/size/{size}"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/redirect")] | ||
public IActionResult GetFileWithNameOnRedirectUrl(string fileName, [FromQuery] long size) | ||
return File(fileData, "application/octet-stream", fileName, true); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header content-length and filename. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Size of the File</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/size/{size}/norange")] | ||
public IActionResult GetFileWithNoAcceptRange(string fileName, long size, [FromQuery] byte? fillByte = null) | ||
{ | ||
DummyLazyStream fileData; | ||
if (fillByte.HasValue) | ||
{ | ||
_logger.LogTrace($"file/{fileName}/redirect?size={size}"); | ||
return LocalRedirectPermanent($"/dummyfile/file/{fileName}?size={size}"); | ||
_logger.LogTrace($"file/{fileName}/size/{size}/norange?fillByte={fillByte}"); | ||
fileData = new DummyLazyStream(DummyDataType.Single, size, fillByte.Value); | ||
} | ||
else | ||
{ | ||
_logger.LogTrace($"file/{fileName}/size/{size}/norange"); | ||
fileData = new DummyLazyStream(DummyDataType.Order, size); | ||
} | ||
|
||
return File(fileData, "application/octet-stream", fileName, false); | ||
} | ||
|
||
/// <summary> | ||
/// Return the file stream with header or not. Filename just used in URL. | ||
/// </summary> | ||
/// <param name="fileName">The file name</param> | ||
/// <param name="size">Query param of the file size</param> | ||
/// <returns>File stream</returns> | ||
[Route("file/{fileName}/redirect")] | ||
public IActionResult GetFileWithNameOnRedirectUrl(string fileName, [FromQuery] long size) | ||
{ | ||
_logger.LogTrace($"file/{fileName}/redirect?size={size}"); | ||
return LocalRedirectPermanent($"/dummyfile/file/{fileName}?size={size}"); | ||
} | ||
|
||
/// <summary> | ||
/// Return the filled stream according to the size and failure after specific offset. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <param name="offset">failure offset</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}/failure/{offset}")] | ||
public FileStreamResult GetOverflowedFile(long size, int offset = 0) | ||
/// <summary> | ||
/// Return the filled stream according to the size and failure after specific offset. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <param name="offset">failure offset</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}/failure/{offset}")] | ||
public IActionResult GetOverflowedFile(long size, int offset = 0) | ||
{ | ||
try | ||
{ | ||
_logger.LogTrace($"file/size/{size}/failure/{offset}"); | ||
return File(new MockMemoryStream(size, offset), "application/octet-stream", true); | ||
} | ||
catch (DummyApiException) | ||
{ | ||
return new StatusCodeResult(500); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Return the filled stream according to the size and timeout after specific offset. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <param name="offset">timeout offset</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}/timeout/{offset}")] | ||
public FileStreamResult GetSlowFile(long size, int offset = 0) | ||
/// <summary> | ||
/// Return the filled stream according to the size and timeout after specific offset. | ||
/// </summary> | ||
/// <param name="size">Size of the data</param> | ||
/// <param name="offset">timeout offset</param> | ||
/// <returns>File stream</returns> | ||
[HttpGet] | ||
[Route("file/size/{size}/timeout/{offset}")] | ||
public IActionResult GetSlowFile(long size, int offset = 0) | ||
{ | ||
try | ||
{ | ||
_logger.LogTrace($"file/size/{size}/timeout/{offset}"); | ||
return File(new MockMemoryStream(size, offset, true), "application/octet-stream", true); | ||
} | ||
catch (DummyApiException) | ||
{ | ||
return new StatusCodeResult(500); | ||
} | ||
} | ||
} |
Oops, something went wrong.