ScreenService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using Microsoft.Azure.Cosmos;
  2. using PuppeteerSharp;
  3. using PuppeteerSharp.Media;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Text.Json.Nodes;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10. using TEAMModelOS.SDK.DI;
  11. namespace HTEXScreen.Service
  12. {
  13. public class ScreenService
  14. {
  15. public static async Task UpdateStuArtPDF(IEnumerable<string> urls, ScreenshotDto screenshot, AzureRedisFactory _azureRedisFactory, AzureCosmosFactory _azureCosmosFactory) {
  16. var env = screenshot.env.Equals("release") ? "Default" : "Test";
  17. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  18. List<Task< ResponseMessage>> responses = new List<Task<ResponseMessage>>();
  19. List<Task<bool>> redisSaves = new List<Task<bool>>();
  20. foreach (var url in urls)
  21. {
  22. //https://teammodeltest.blob.core.chinacloudapi.cn/hbcn/art/e9a5ec36-7299-45dc-9517-7457960346c4/report/202106005.pdf
  23. var uri = HttpUtility.UrlDecode(url);
  24. var paths = uri.Split("/art/");
  25. if (paths.Length == 2) {
  26. var ps= paths[1].Split("/");
  27. if (ps.Length == 3) {
  28. Uri uris = new Uri(paths[0]);
  29. // 获取URL的Segments属性,这是一个String数组,包含URL中的每个部分
  30. string[] segments = uris.Segments;
  31. string key = $"ArtPDF:{ps[0]}";
  32. // 确保segments数组至少有一个元素
  33. if (segments.Length > 0)
  34. {
  35. // 获取数组中的最后一个元素,即最后一个'/'之后的部分
  36. string lastSegment = segments[segments.Length - 1];
  37. key = $"ArtPDF:{ps[0]}:{lastSegment}";
  38. }
  39. string field = ps[2].Replace(".pdf", "",StringComparison.OrdinalIgnoreCase);
  40. var value = _azureRedisFactory.GetRedisClient(8, env).HashGet(key, field);
  41. if (value.HasValue && !value.IsNullOrEmpty)
  42. {
  43. JsonNode node = JsonNode.Parse(value.ToString());
  44. if (node != null) {
  45. var pdfNode= node["pdf"];
  46. var id = node["id"];
  47. var code = node["code"];
  48. if (pdfNode != null)
  49. {
  50. pdfNode["url"] = uri;
  51. pdfNode["blob"] = $"/art/{paths[1]}";
  52. pdfNode["createTime"] = now;
  53. pdfNode["prime"] = true;//此处的作用是判断是否已经生成OK.
  54. node["pdf"] = pdfNode;
  55. }
  56. else {
  57. pdfNode = new JsonObject();
  58. pdfNode["url"] = uri;
  59. pdfNode["blob"] = $"/art/{paths[1]}";
  60. pdfNode["createTime"] = now;
  61. pdfNode["prime"] = true;//此处的作用是判断是否已经生成OK.
  62. node["pdf"] = pdfNode;
  63. }
  64. string json = node.ToJsonString();
  65. redisSaves.Add(_azureRedisFactory.GetRedisClient(8, env).HashSetAsync(key, field, json));
  66. byte[] bytes = Encoding.UTF8.GetBytes(json);
  67. var memoryStream = new MemoryStream(bytes);
  68. responses.Add(_azureCosmosFactory.GetCosmosClient(null,env).GetContainer("TEAMModelOS", "Student").ReplaceItemStreamAsync(memoryStream, $"{id}", new PartitionKey($"{code}")));
  69. }
  70. }
  71. }
  72. }
  73. }
  74. await Task.WhenAll(redisSaves);
  75. await Task.WhenAll(responses);
  76. }
  77. public static async Task<List<(string name ,string url )>> ScreenshotPdf(ScreenshotDto screenshot, AzureStorageFactory _azureStorage) {
  78. //W3C School教程 https://www.w3cschool.cn/puppeteer/puppeteer-rip537tj.html
  79. // 进入容器的命令 docker exec -it f9e27d175498 /bin/bash
  80. //依赖包 https://blog.csdn.net/weixin_45447477/article/details/115188938
  81. //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
  82. //解决ubuntu18上使用puppeteer https://blog.csdn.net/qq_42414062/article/details/114539378
  83. //https://www.hardkoded.com/blog/running-puppeteer-sharp-azure-functions 使用。
  84. //string url = "https://teammodelos.blob.core.chinacloudapi.cn/0-public/pie-borderRadius.html";
  85. Browser browser = null;
  86. try
  87. {
  88. var bfOptions = new BrowserFetcherOptions();
  89. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  90. {
  91. string dir = "/app";
  92. if (!Directory.Exists(dir))
  93. {
  94. Directory.CreateDirectory(dir);
  95. }
  96. bfOptions.Path = dir;
  97. }
  98. var bf = new BrowserFetcher(bfOptions);
  99. var revisionInfo = bf.DownloadAsync(BrowserFetcher.DefaultChromiumRevision).Result;
  100. string BrowserExecutablePath = revisionInfo.ExecutablePath;
  101. browser = await Puppeteer.LaunchAsync(new LaunchOptions
  102. {
  103. ExecutablePath = BrowserExecutablePath,
  104. Headless = true,
  105. Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }
  106. });
  107. double unitPX = 37.7813;
  108. ViewPortOptions viewPortOptions = new ViewPortOptions
  109. {
  110. // Width = (int)Math.Ceiling(unitPX * 21),
  111. // Height = (int)Math.Ceiling(unitPX * 29.7 * 1)
  112. };
  113. //ViewPortOptions viewPortOptions = new ViewPortOptions
  114. //{
  115. // Width = screenshot.width,
  116. // Height = screenshot.height
  117. //};
  118. PdfOptions pdfOptions = new PdfOptions { DisplayHeaderFooter = true, FooterTemplate = "", PreferCSSPageSize = true, Format = PaperFormat.A4 };
  119. //ScreenshotOptions screenshotOptions= new ScreenshotOptions { FullPage = fullPage, BurstMode = true };
  120. List<(string anme ,string url )> urls = new List<(string name, string url)>();
  121. if (screenshot.urls.Count <= screenshot.pagesize)
  122. {
  123. urls.AddRange(await PageToPdfStream(_azureStorage,screenshot.urls, screenshot.fileNameKey, screenshot.cnt, screenshot.root, screenshot.env, browser, viewPortOptions, pdfOptions));
  124. }
  125. else
  126. {
  127. List<Task<List<(string name ,string url )>>> tasks = new List<Task<List<(string name, string url)>>>();
  128. int pages = (screenshot.urls.Count + screenshot.pagesize) / screenshot.pagesize;
  129. for (int i = 0; i < pages; i++)
  130. {
  131. var lists = screenshot.urls.Skip((i) * screenshot.pagesize).Take(screenshot.pagesize).ToList();
  132. tasks.Add(PageToPdfStream(_azureStorage,lists, screenshot.fileNameKey, screenshot.cnt, screenshot.root, screenshot.env, browser, viewPortOptions, pdfOptions));
  133. }
  134. var tsk = await Task.WhenAll(tasks);
  135. foreach (var ts in tsk)
  136. {
  137. urls.AddRange(ts);
  138. }
  139. }
  140. //browser.NewPageAsync();
  141. //foreach (var url in screenshot.urls) {
  142. // var page = await browser.NewPageAsync();
  143. // await page.SetViewportAsync(viewPortOptions);
  144. // string file = $"E://pdfs//{Guid.NewGuid().ToString()}.pdf";
  145. // var respons = await page.GoToAsync(System.Web.HttpUtility.UrlDecode(url), WaitUntilNavigation.Networkidle2);
  146. // if (respons.Ok)
  147. // {
  148. // await page.PdfAsync(file, pdfOptions);
  149. // // string base64 = await page.ScreenshotBase64Async(screenshotOptions);
  150. // }
  151. //}
  152. //关闭浏览器
  153. await browser.CloseAsync();
  154. await browser.DisposeAsync();
  155. return urls;
  156. }
  157. catch (Exception ex)
  158. {
  159. StreamWriter file = new StreamWriter("erorr_log.txt", append: true);
  160. await file.WriteLineAsync($"ScreenService {JsonSerializer.Serialize(screenshot)}-----{ex.Message}----{ex.StackTrace}");
  161. file.Close();
  162. return new List<(string name, string url)>();
  163. //return BadRequest($"{ex.Message}\n{ex.StackTrace}");
  164. }
  165. finally
  166. {
  167. if (browser != null && !browser.IsClosed)
  168. {
  169. await browser.CloseAsync();
  170. await browser.DisposeAsync();
  171. }
  172. }
  173. }
  174. private static async Task<List<(string name ,string url )>> PageToPdfStream(AzureStorageFactory _azureStorage,List<string> urls, string fileNameKey, string cnt, string root, string env, Browser browser, ViewPortOptions viewPortOptions, PdfOptions pdfOptions)
  175. {
  176. string name = env.Equals("release") ? "Default" : "Test";
  177. List<Task<Page>> pages = new List<Task<Page>>();
  178. urls.ForEach(x => {
  179. pages.Add(browser.NewPageAsync());
  180. });
  181. var page_tasks = await Task.WhenAll(pages);
  182. List<Task<Response>> responses = new List<Task<Response>>();
  183. page_tasks.ToList().ForEach(x => {
  184. x.SetViewportAsync(viewPortOptions);
  185. });
  186. for (int i = 0; i < urls.Count; i++)
  187. {
  188. responses.Add(page_tasks[i].GoToAsync(urls[i], 30000, new WaitUntilNavigation[] { WaitUntilNavigation.Networkidle2 }));
  189. }
  190. var responses_tasks = await Task.WhenAll(responses);
  191. //List<Task<Stream>> streams = new List<Task<Stream>>();
  192. List<Task> tasks = new List<Task>();
  193. List<Task<(string name , string url )>> uploads = new List<Task<(string name, string url)>>();
  194. foreach (var page_task in page_tasks)
  195. {
  196. string url = page_task.Url;
  197. string[] paths = HttpUtility.UrlDecode(url).Split("/");
  198. var reg = $"(?<=\\b{fileNameKey}=)[^&]*";
  199. Regex regex = new Regex(reg);
  200. string decode = HttpUtility.UrlDecode(url);
  201. Match match = Regex.Match(decode, reg);
  202. string id = "";
  203. while (match.Success)
  204. {
  205. id = id + $"{match.Value}";
  206. match = match.NextMatch();
  207. }
  208. //需要解析参数。paths[paths.Length-1]
  209. Stream stream = await page_task.PdfStreamAsync(pdfOptions);
  210. if (string.IsNullOrWhiteSpace(cnt))
  211. {
  212. uploads.Add(_azureStorage.GetBlobContainerClient("teammodelos", name).UploadFileByContainerBName(stream, root, $"{id}.pdf", true));
  213. }
  214. else
  215. {
  216. uploads.Add(_azureStorage.GetBlobContainerClient(cnt, name).UploadFileByContainerBName(stream, root, $"{id}.pdf", true));
  217. }
  218. }
  219. (string name ,string url )[] uploadUrls = await Task.WhenAll(uploads);
  220. page_tasks.ToList().ForEach(x => {
  221. tasks.Add(x.DisposeAsync().AsTask());
  222. });
  223. await Task.WhenAll(tasks);
  224. return uploadUrls.ToList();
  225. }
  226. }
  227. public class ScreenshotDto
  228. {
  229. public int width { get; set; } = 1920;
  230. public int height { get; set; } = 1080;
  231. public string? url { get; set; }
  232. /// <summary>
  233. /// 批量地址
  234. /// </summary>
  235. public List<string> urls { get; set; } = new List<string>();
  236. /// <summary>
  237. /// 提取参数的唯一id作为文件名
  238. /// </summary>
  239. public string? fileNameKey { get; set; }
  240. /// <summary>
  241. /// 存在哪个容器里
  242. /// </summary>
  243. public string? cnt { get; set; }
  244. public int delay { get; set; }
  245. public int pagesize { get; set; } = 5;
  246. public string? root { get; set; }
  247. public string? env { get; set; } = "release";
  248. public string? msgId { get; set; }
  249. }
  250. }