ScreenController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using HTEXScreen.Service;
  2. using Microsoft.AspNetCore.Mvc;
  3. using PuppeteerSharp;
  4. using PuppeteerSharp.Media;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10. namespace HTEXScreen.Controllers
  11. {
  12. [ApiController]
  13. [Route("screen")]
  14. public class ScreenController : ControllerBase
  15. {
  16. private readonly AzureStorageFactory _azureStorage;
  17. private readonly HttpClient _httpClient;
  18. public ScreenController(HttpClient httpClient, AzureStorageFactory azureStorage) {
  19. _httpClient = httpClient;
  20. _azureStorage = azureStorage;
  21. }
  22. [HttpPost("testbase64")]
  23. public async Task<IActionResult> TestBase64(JsonElement json ) {
  24. JsonSerializerOptions option = new System.Text.Json.JsonSerializerOptions
  25. {
  26. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  27. };
  28. string name = "dataid";
  29. var reg = $"(?<=\\b{name}=)[^&]*";
  30. Regex regex = new Regex(reg);
  31. string decode= HttpUtility.UrlDecode("https://localhost:7298/swagger/index.html?dataid=%E5%BC%A0%E5%90%8C%E5%AD%A6&bs64=1234567&dataid=%E6%9D%8E%E5%90%8C%E5%AD%A6");
  32. Match match= Regex.Match(decode, reg);
  33. string a = "";
  34. while (match.Success)
  35. {
  36. a =a+$"{match.Value}";
  37. match = match.NextMatch();
  38. }
  39. string s = JsonSerializer.Serialize(json, option);
  40. string das= HttpUtility.UrlEncode(s,Encoding.UTF8);
  41. byte[] byts = Encoding.UTF8.GetBytes(s);
  42. var rdata = Convert.ToBase64String(byts);
  43. byte[] bs = Convert.FromBase64String(rdata);
  44. string data = Encoding.UTF8.GetString(bs);
  45. return Ok(new { a, rdata,das });
  46. }
  47. [HttpGet("download")]
  48. public async Task<IActionResult> Download([FromQuery] ScreenshotDto screenshot) {
  49. try {
  50. HttpResponseMessage response = await _httpClient.GetAsync(screenshot.url);
  51. if (!string.IsNullOrWhiteSpace(screenshot?.url))
  52. {
  53. string? url = screenshot?.url;
  54. string[] path = url.Split("/");
  55. string fileName = path[path.Length - 1];
  56. fileName=HttpUtility.UrlDecode(fileName);
  57. Stream stream = response.Content.ReadAsStream();
  58. if (!Directory.Exists("Download")){
  59. Directory.CreateDirectory("Download");
  60. }
  61. FileStream fs = new FileStream($"Download/{fileName}", FileMode.Create);
  62. byte[] bytes = new byte[stream.Length];
  63. stream.Read(bytes, 0, bytes.Length);
  64. stream.Seek(0, SeekOrigin.Begin);
  65. BinaryWriter bw = new BinaryWriter(fs);
  66. bw.Write(bytes);
  67. bw.Close();
  68. fs.Close();
  69. return Ok(screenshot);
  70. }
  71. else {
  72. return Ok("链接为空");
  73. }
  74. } catch (Exception ex) {
  75. return Ok($"{ex.Message}{ex.StackTrace}");
  76. }
  77. }
  78. /// <summary>
  79. /// C#使用Puppeteer http://t.zoukankan.com/zhaotianff-p-13528507.html
  80. /// 文档https://learnku.com/docs/puppeteer/3.1.0/class-request/8559
  81. /// https://www.w3cschool.cn/puppeteer/puppeteer-gp1737se.html
  82. /// </summary>
  83. /// <param name="screenshot"></param>
  84. /// <returns></returns>
  85. [HttpGet("screenshot-png")]
  86. public async Task<IActionResult> ScreenshotPng([FromQuery] ScreenshotDto screenshot)
  87. {
  88. // 进入容器的命令 docker exec -it f9e27d175498 /bin/bash
  89. //依赖包 https://blog.csdn.net/weixin_45447477/article/details/115188938
  90. //sudo apt-get install libgdk-pixbuf2.0-0 libgdk-pixbuf-xlib-2.0-0 libdbusmenu-gtk3-4 libdbusmenu-glib4 libindicator3-7 ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils -y
  91. //解决ubuntu18上使用puppeteer https://blog.csdn.net/qq_42414062/article/details/114539378
  92. //https://www.hardkoded.com/blog/running-puppeteer-sharp-azure-functions 使用。
  93. //string url = "https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html";
  94. try
  95. {
  96. var bfOptions = new BrowserFetcherOptions();
  97. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  98. {
  99. string dir = "/app";
  100. if (!Directory.Exists(dir))
  101. {
  102. Directory.CreateDirectory(dir);
  103. }
  104. bfOptions.Path = dir;
  105. }
  106. var bf = new BrowserFetcher(bfOptions);
  107. var revisionInfo = bf.DownloadAsync(BrowserFetcher.DefaultChromiumRevision).Result;
  108. string BrowserExecutablePath = revisionInfo.ExecutablePath;
  109. var browser = await Puppeteer.LaunchAsync(new LaunchOptions
  110. {
  111. ExecutablePath = BrowserExecutablePath,
  112. Headless = true,
  113. Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }
  114. });
  115. var page = await browser.NewPageAsync();
  116. bool fullPage = true;
  117. await page.SetViewportAsync(new ViewPortOptions
  118. {
  119. Width = screenshot.width,
  120. Height = screenshot.height
  121. });;
  122. fullPage = false;
  123. await page.GoToAsync(System.Web.HttpUtility.UrlDecode(screenshot.url));
  124. Thread.Sleep(screenshot.delay);
  125. string base64 = await page.ScreenshotBase64Async(new ScreenshotOptions { FullPage = fullPage, BurstMode = true });
  126. //关闭浏览器
  127. await browser.CloseAsync();
  128. await browser.DisposeAsync();
  129. return Ok(new { url = base64 ,type= "data:image/png;base64," });
  130. }
  131. catch (Exception ex)
  132. {
  133. return BadRequest($"{ex.Message}\n{ex.StackTrace}");
  134. }
  135. }
  136. [HttpPost("screenshot-pdf")]
  137. public async Task<IActionResult> ScreenshotPdf(ScreenshotDto screenshot)
  138. {
  139. //W3C School教程 https://www.w3cschool.cn/puppeteer/puppeteer-rip537tj.html
  140. // 进入容器的命令 docker exec -it f9e27d175498 /bin/bash
  141. //依赖包 https://blog.csdn.net/weixin_45447477/article/details/115188938
  142. //sudo apt-get install libgdk-pixbuf2.0-0 libgdk-pixbuf-xlib-2.0-0 libdbusmenu-gtk3-4 libdbusmenu-glib4 libindicator3-7 ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils -y
  143. //解决ubuntu18上使用puppeteer https://blog.csdn.net/qq_42414062/article/details/114539378
  144. //https://www.hardkoded.com/blog/running-puppeteer-sharp-azure-functions 使用。
  145. //string url = "https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html";
  146. Browser browser = null;
  147. try
  148. {
  149. var bfOptions = new BrowserFetcherOptions();
  150. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  151. {
  152. string dir = "/app";
  153. if (!Directory.Exists(dir))
  154. {
  155. Directory.CreateDirectory(dir);
  156. }
  157. bfOptions.Path = dir;
  158. }
  159. var bf = new BrowserFetcher(bfOptions);
  160. var revisionInfo = bf.DownloadAsync(BrowserFetcher.DefaultChromiumRevision).Result;
  161. string BrowserExecutablePath = revisionInfo.ExecutablePath;
  162. browser = await Puppeteer.LaunchAsync(new LaunchOptions
  163. {
  164. ExecutablePath = BrowserExecutablePath,
  165. Headless = true,
  166. Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }
  167. });
  168. double unitPX = 37.7813;
  169. ViewPortOptions viewPortOptions = new ViewPortOptions
  170. {
  171. // Width = (int)Math.Ceiling(unitPX * 21),
  172. // Height = (int)Math.Ceiling(unitPX * 29.7 * 1)
  173. };
  174. //ViewPortOptions viewPortOptions = new ViewPortOptions
  175. //{
  176. // Width = screenshot.width,
  177. // Height = screenshot.height
  178. //};
  179. PdfOptions pdfOptions = new PdfOptions { DisplayHeaderFooter = true, FooterTemplate = "", PreferCSSPageSize = true, Format = PaperFormat.A4 };
  180. //ScreenshotOptions screenshotOptions= new ScreenshotOptions { FullPage = fullPage, BurstMode = true };
  181. List<string> urls = new List<string>();
  182. if (screenshot.urls.Count <= screenshot.pagesize)
  183. {
  184. urls.AddRange(await PageToPdfStream(screenshot.urls, screenshot.fileNameKey, screenshot.cnt, screenshot.root, screenshot.env, browser, viewPortOptions, pdfOptions));
  185. }
  186. else
  187. {
  188. List<Task<List<string>>> tasks = new List<Task<List<string>>>();
  189. int pages = (screenshot.urls.Count + screenshot.pagesize) / screenshot.pagesize;
  190. for (int i = 0; i < pages; i++)
  191. {
  192. var lists = screenshot.urls.Skip((i) * screenshot.pagesize).Take(screenshot.pagesize).ToList();
  193. tasks.Add(PageToPdfStream(lists, screenshot.fileNameKey, screenshot.cnt, screenshot.root, screenshot.env, browser, viewPortOptions, pdfOptions));
  194. }
  195. var tsk = await Task.WhenAll(tasks);
  196. foreach (var ts in tsk)
  197. {
  198. urls.AddRange(ts);
  199. }
  200. }
  201. //browser.NewPageAsync();
  202. //foreach (var url in screenshot.urls) {
  203. // var page = await browser.NewPageAsync();
  204. // await page.SetViewportAsync(viewPortOptions);
  205. // string file = $"E://pdfs//{Guid.NewGuid().ToString()}.pdf";
  206. // var respons = await page.GoToAsync(System.Web.HttpUtility.UrlDecode(url), WaitUntilNavigation.Networkidle2);
  207. // if (respons.Ok)
  208. // {
  209. // await page.PdfAsync(file, pdfOptions);
  210. // // string base64 = await page.ScreenshotBase64Async(screenshotOptions);
  211. // }
  212. //}
  213. //关闭浏览器
  214. await browser.CloseAsync();
  215. await browser.DisposeAsync();
  216. return Ok(new { urls });
  217. }
  218. catch (Exception ex)
  219. {
  220. StreamWriter file = new StreamWriter("erorr_log.txt", append: true);
  221. await file.WriteLineAsync($"{this.GetType().Name} {JsonSerializer.Serialize(screenshot)}-----{ex.Message}----{ex.StackTrace}");
  222. file.Close();
  223. return BadRequest($"{ex.Message}\n{ex.StackTrace}");
  224. }
  225. finally {
  226. if (browser != null && !browser.IsClosed) {
  227. await browser.CloseAsync();
  228. await browser.DisposeAsync();
  229. }
  230. }
  231. }
  232. private async Task<List<string>> PageToPdfStream(List<string> urls,string fileNameKey,string cnt,string root,string env , Browser browser, ViewPortOptions viewPortOptions, PdfOptions pdfOptions)
  233. {
  234. string name= env.Equals("release")? "Default" : "Test";
  235. List<Task<Page>> pages = new List<Task<Page>>();
  236. urls.ForEach(x => {
  237. pages.Add(browser.NewPageAsync());
  238. });
  239. var page_tasks = await Task.WhenAll(pages);
  240. List<Task<Response>> responses = new List<Task<Response>>();
  241. page_tasks.ToList().ForEach(x => {
  242. x.SetViewportAsync(viewPortOptions);
  243. });
  244. for (int i = 0; i < urls.Count; i++)
  245. {
  246. responses.Add(page_tasks[i].GoToAsync(urls[i],30000, new WaitUntilNavigation[] { WaitUntilNavigation.Networkidle2 }));
  247. }
  248. var responses_tasks = await Task.WhenAll(responses);
  249. //List<Task<Stream>> streams = new List<Task<Stream>>();
  250. List<Task> tasks = new List<Task>();
  251. List<Task<string>> uploads = new List<Task<string>>();
  252. foreach (var page_task in page_tasks) {
  253. string url = page_task.Url;
  254. string[] paths= HttpUtility.UrlDecode(url).Split("/");
  255. var reg = $"(?<=\\b{fileNameKey}=)[^&]*";
  256. Regex regex = new Regex(reg);
  257. string decode = HttpUtility.UrlDecode(url);
  258. Match match = Regex.Match(decode, reg);
  259. string id = "";
  260. while (match.Success)
  261. {
  262. id = id + $"{match.Value}";
  263. match = match.NextMatch();
  264. }
  265. //需要解析参数。paths[paths.Length-1]
  266. Stream stream = await page_task.PdfStreamAsync(pdfOptions);
  267. if (string.IsNullOrWhiteSpace(cnt))
  268. {
  269. uploads.Add(_azureStorage.GetBlobContainerClient("teammodelos", name).UploadFileByContainer(stream, root, $"{id}.pdf", true));
  270. }
  271. else {
  272. uploads.Add(_azureStorage.GetBlobContainerClient(cnt, name).UploadFileByContainer(stream, root, $"{id}.pdf", true));
  273. }
  274. }
  275. string[] uploadUrls= await Task.WhenAll(uploads);
  276. page_tasks.ToList().ForEach(x => {
  277. tasks.Add(x.DisposeAsync().AsTask());
  278. });
  279. await Task.WhenAll(tasks);
  280. return uploadUrls.ToList();
  281. }
  282. private async Task PageToPdf(List<string> urls, Browser browser, ViewPortOptions viewPortOptions, PdfOptions pdfOptions)
  283. {
  284. List<Task<Page>> pages = new List<Task<Page>>();
  285. urls.ForEach(x => {
  286. pages.Add(browser.NewPageAsync());
  287. });
  288. var page_tasks = await Task.WhenAll(pages);
  289. List<Task<Response>> responses = new List<Task<Response>>();
  290. page_tasks.ToList().ForEach(x => {
  291. x.SetViewportAsync(viewPortOptions);
  292. });
  293. for (int i = 0; i < urls.Count; i++)
  294. {
  295. responses.Add(page_tasks[i].GoToAsync(System.Web.HttpUtility.UrlDecode(urls[i]), WaitUntilNavigation.Networkidle2));
  296. }
  297. var responses_tasks = await Task.WhenAll(responses);
  298. List<Task> tasks = new List<Task>();
  299. page_tasks.ToList().ForEach(x => {
  300. string file = $"{Guid.NewGuid()}.pdf";
  301. tasks.Add(x.PdfAsync(file, pdfOptions));
  302. });
  303. await Task.WhenAll(tasks);
  304. tasks.Clear();
  305. page_tasks.ToList().ForEach(x => {
  306. tasks.Add(x.DisposeAsync().AsTask());
  307. });
  308. await Task.WhenAll(tasks);
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 网页截图参数
  314. /// </summary>