L1SvrFileInfo.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using LJLib.Net.SPI.Client;
  2. using LJProxy.Models;
  3. using Microsoft.Extensions.FileProviders;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace LJProxy.Providers
  11. {
  12. public class L1SvrFileInfo : IFileInfo
  13. {
  14. private string _fullFilepath;
  15. private string _subpath;
  16. private FileInfo _file;
  17. private FileInfo _tempFile;
  18. private MemoryStream _stream;
  19. private ILJClient _ljPool;
  20. private static object _syncRoot = new object();
  21. private string _token;
  22. public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool,string token)
  23. {
  24. _fullFilepath = fullFilepath;
  25. _subpath = subpath;
  26. _ljPool = ljPool;
  27. _token = token;
  28. _stream = new MemoryStream(); ;
  29. if (File.Exists(_fullFilepath))
  30. {
  31. _tempFile = new FileInfo(_fullFilepath);
  32. CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime, true);
  33. }
  34. else
  35. {
  36. lock (_syncRoot)
  37. {
  38. if (File.Exists(_fullFilepath))
  39. {
  40. _tempFile = new FileInfo(_fullFilepath);
  41. CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime,true);
  42. }
  43. else
  44. {
  45. CheckAuthorizationOrGetRemoteFile(null);
  46. }
  47. }
  48. }
  49. }
  50. public bool Exists => _file!=null;
  51. public bool IsDirectory => false;
  52. public DateTimeOffset LastModified => _file.LastWriteTime;
  53. public long Length => _stream.Length;
  54. public string Name => _file.Name;
  55. public string PhysicalPath => _file.FullName;
  56. public Stream CreateReadStream()
  57. {
  58. return _stream;
  59. }
  60. public void CheckAuthorizationOrGetRemoteFile(DateTime? lastWriteTime, bool checkAuthorization=false)
  61. {
  62. var getFileReq = new GetFileRequest() {
  63. Filepath = _subpath,
  64. token = _token,
  65. checkAuthorization = checkAuthorization,
  66. lastWriteTime = lastWriteTime
  67. };
  68. var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(),JsonConvert.SerializeObject(getFileReq));
  69. var response = JsonConvert.DeserializeObject<GetFileResponse>(responseStr);
  70. if (response.AppErrCode == "900")
  71. {
  72. if (!checkAuthorization)
  73. {
  74. var fileDir = Path.GetDirectoryName(_fullFilepath);
  75. if (!Directory.Exists(fileDir))
  76. {
  77. Directory.CreateDirectory(fileDir);
  78. }
  79. File.WriteAllBytes(_fullFilepath, response.FileData);
  80. }
  81. else
  82. {
  83. if(response.FileData!=null && response.FileData.Length > 0)
  84. {
  85. var fileDir = Path.GetDirectoryName(_fullFilepath);
  86. if (!Directory.Exists(fileDir))
  87. {
  88. Directory.CreateDirectory(fileDir);
  89. }
  90. File.WriteAllBytes(_fullFilepath, response.FileData);
  91. }
  92. }
  93. _file = new FileInfo(_fullFilepath);
  94. byte[] buff = new byte[1024];
  95. using(var fs = _file.OpenRead())
  96. {
  97. int readCount = fs.Read(buff, 0, buff.Length);
  98. while (readCount > 0)
  99. {
  100. _stream.Write(buff, 0, readCount);
  101. readCount = fs.Read(buff, 0, buff.Length);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }