|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
using PuppeteerSharp;
|
|
|
using System.Runtime.InteropServices;
|
|
|
+using System.Web;
|
|
|
|
|
|
namespace HTEXScreen.Controllers
|
|
|
{
|
|
@@ -9,8 +10,44 @@ namespace HTEXScreen.Controllers
|
|
|
[Route("screen")]
|
|
|
public class ScreenController : ControllerBase
|
|
|
{
|
|
|
- public ScreenController() { }
|
|
|
-
|
|
|
+ private readonly HttpClient _httpClient;
|
|
|
+ public ScreenController(HttpClient httpClient) {
|
|
|
+ _httpClient = httpClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpGet("download")]
|
|
|
+ public async Task<IActionResult> Download([FromQuery] ScreenshotDto screenshot) {
|
|
|
+ try {
|
|
|
+ HttpResponseMessage response = await _httpClient.GetAsync(screenshot.url);
|
|
|
+ if (!string.IsNullOrWhiteSpace(screenshot?.url))
|
|
|
+ {
|
|
|
+ string? url = screenshot?.url;
|
|
|
+ string[] path = url.Split("/");
|
|
|
+ string fileName = path[path.Length - 1];
|
|
|
+ fileName=HttpUtility.UrlDecode(fileName);
|
|
|
+ Stream stream = response.Content.ReadAsStream();
|
|
|
+ if (!Directory.Exists("Download")){
|
|
|
+ Directory.CreateDirectory("Download");
|
|
|
+ }
|
|
|
+ FileStream fs = new FileStream($"Download/{fileName}", FileMode.Create);
|
|
|
+ byte[] bytes = new byte[stream.Length];
|
|
|
+ stream.Read(bytes, 0, bytes.Length);
|
|
|
+ stream.Seek(0, SeekOrigin.Begin);
|
|
|
+ BinaryWriter bw = new BinaryWriter(fs);
|
|
|
+ bw.Write(bytes);
|
|
|
+ bw.Close();
|
|
|
+ fs.Close();
|
|
|
+ return Ok(screenshot);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return Ok("链接为空");
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ return Ok($"{ex.Message}{ex.StackTrace}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// C#使用Puppeteer http://t.zoukankan.com/zhaotianff-p-13528507.html
|
|
|
/// 文档https://learnku.com/docs/puppeteer/3.1.0/class-request/8559
|
|
@@ -73,7 +110,7 @@ namespace HTEXScreen.Controllers
|
|
|
return BadRequest($"{ex.Message}\n{ex.StackTrace}");
|
|
|
}
|
|
|
}
|
|
|
- [HttpPost("screenshot-pngs")]
|
|
|
+ [HttpPost("screenshot-pdf")]
|
|
|
public async Task<IActionResult> ScreenshotPngs(ScreenshotDto screenshot)
|
|
|
{
|
|
|
|
|
@@ -109,7 +146,6 @@ namespace HTEXScreen.Controllers
|
|
|
Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }
|
|
|
});
|
|
|
|
|
|
- bool fullPage = true;
|
|
|
ViewPortOptions viewPortOptions = new ViewPortOptions
|
|
|
{
|
|
|
Width = screenshot.width,
|
|
@@ -199,7 +235,7 @@ namespace HTEXScreen.Controllers
|
|
|
{
|
|
|
public int width { get; set; } = 1920;
|
|
|
public int height { get; set; } = 1080;
|
|
|
- public string url { get; set; }
|
|
|
+ public string? url { get; set; }
|
|
|
public List<string> urls { get; set; } = new List<string>();
|
|
|
public int delay { get; set; }
|
|
|
}
|