L1SvrFileInfo.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using LJLib.Net.SPI.Client;
  2. using LJLib.Net.SPI.Com;
  3. using LJProxy.Models;
  4. using Microsoft.Extensions.FileProviders;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace LJProxy.Providers
  13. {
  14. public class L1SvrFileInfo : IFileInfo
  15. {
  16. private string _fullFilepath;
  17. private string _subpath;
  18. private FileInfo _file;
  19. private FileInfo _tempFile;
  20. private MemoryStream _stream;
  21. private ILJClient _ljPool;
  22. private static object _syncRoot = new object();
  23. private string _token;
  24. private string annoymousSubpath = "\\anyC84F3B0582994A1184044B116EC2080B\\";
  25. public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool,string token)
  26. {
  27. _fullFilepath = fullFilepath;
  28. _subpath = subpath;
  29. _ljPool = ljPool;
  30. _token = token;
  31. _stream = new MemoryStream(); ;
  32. if (File.Exists(_fullFilepath))
  33. {
  34. _tempFile = new FileInfo(_fullFilepath);
  35. if(subpath.IndexOf(annoymousSubpath)>=0)
  36. {
  37. _file = _tempFile;
  38. writeFileStream();
  39. }
  40. else
  41. {
  42. CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime, true);
  43. }
  44. }
  45. else
  46. {
  47. lock (_syncRoot)
  48. {
  49. if (File.Exists(_fullFilepath))
  50. {
  51. _tempFile = new FileInfo(_fullFilepath);
  52. CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime,true);
  53. }
  54. else
  55. {
  56. CheckAuthorizationOrGetRemoteFile(null);
  57. }
  58. }
  59. }
  60. }
  61. public bool Exists => _file!=null;
  62. public bool IsDirectory => false;
  63. public DateTimeOffset LastModified => _file.LastWriteTime;
  64. public long Length => _stream.Length;
  65. public string Name => _file.Name;
  66. public string PhysicalPath => _file.FullName;
  67. public Stream CreateReadStream()
  68. {
  69. return _stream;
  70. }
  71. public void CheckAuthorizationOrGetRemoteFile(DateTime? lastWriteTime, bool checkAuthorization=false)
  72. {
  73. try
  74. {
  75. var getFileReq = new GetFileRequest()
  76. {
  77. Filepath = _subpath,
  78. token = _token,
  79. checkAuthorization = checkAuthorization,
  80. lastWriteTime = lastWriteTime
  81. };
  82. Stopwatch sw = Stopwatch.StartNew();
  83. Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}:request GetFileRequest");
  84. var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(), JsonConvert.SerializeObject(getFileReq));
  85. Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}:response GetFileRequest");
  86. Console.WriteLine($"GetFileRequest ellapsed:{sw.ElapsedMilliseconds} ms");
  87. sw.Restart();
  88. var response = JsonConvert.DeserializeObject<GetFileResponse>(responseStr);
  89. Console.WriteLine($"DeserializeObject ellapsed:{sw.ElapsedMilliseconds} ms");
  90. Console.WriteLine(response.AppErrCode+":"+ response.AppErrMsg);
  91. sw.Restart();
  92. if (response.AppErrCode == "900")
  93. {
  94. if (!checkAuthorization)
  95. {
  96. var fileDir = Path.GetDirectoryName(_fullFilepath);
  97. if (!Directory.Exists(fileDir))
  98. {
  99. Directory.CreateDirectory(fileDir);
  100. }
  101. File.WriteAllBytes(_fullFilepath, response.FileData);
  102. }
  103. else
  104. {
  105. if (response.FileData != null && response.FileData.Length > 0)
  106. {
  107. var fileDir = Path.GetDirectoryName(_fullFilepath);
  108. if (!Directory.Exists(fileDir))
  109. {
  110. Directory.CreateDirectory(fileDir);
  111. }
  112. File.WriteAllBytes(_fullFilepath, response.FileData);
  113. }
  114. }
  115. Console.WriteLine($"WriteAllBytes ellapsed:{sw.ElapsedMilliseconds} ms");
  116. sw.Restart();
  117. _file = new FileInfo(_fullFilepath);
  118. writeFileStream();
  119. Console.WriteLine($"OpenRead ellapsed:{sw.ElapsedMilliseconds} ms");
  120. }
  121. }catch(Exception ex)
  122. {
  123. Trace.WriteLine($"CheckAuthorizationOrGetRemoteFile:{ex}");
  124. Console.WriteLine($"CheckAuthorizationOrGetRemoteFile:{ex}");
  125. }
  126. }
  127. public void writeFileStream()
  128. {
  129. try
  130. {
  131. if (_file == null) return;
  132. byte[] buff = new byte[1024];
  133. using (var fs = _file.OpenRead())
  134. {
  135. int readCount = fs.Read(buff, 0, buff.Length);
  136. while (readCount > 0)
  137. {
  138. _stream.Write(buff, 0, readCount);
  139. readCount = fs.Read(buff, 0, buff.Length);
  140. }
  141. }
  142. }catch(Exception ex)
  143. {
  144. Trace.WriteLine($"writeFileStream:{ex}");
  145. Console.WriteLine($"writeFileStream:{ex}");
  146. }
  147. }
  148. }
  149. }