123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using LJLib.Net.SPI.Client;
- using LJLib.Net.SPI.Com;
- using LJProxy.Models;
- using Microsoft.Extensions.FileProviders;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace LJProxy.Providers
- {
- public class L1SvrFileInfo : IFileInfo
- {
- private string _fullFilepath;
- private string _subpath;
- private FileInfo _file;
- private FileInfo _tempFile;
- private MemoryStream _stream;
- private ILJClient _ljPool;
- private static object _syncRoot = new object();
- private string _token;
- private string annoymousSubpath = "\\anyC84F3B0582994A1184044B116EC2080B\\";
- public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool,string token)
- {
- _fullFilepath = fullFilepath;
- _subpath = subpath;
- _ljPool = ljPool;
- _token = token;
- _stream = new MemoryStream(); ;
- if (File.Exists(_fullFilepath))
- {
- _tempFile = new FileInfo(_fullFilepath);
- if(subpath.IndexOf(annoymousSubpath)>=0)
- {
- _file = _tempFile;
- writeFileStream();
- }
- else
- {
- CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime, true);
- }
- }
- else
- {
- lock (_syncRoot)
- {
- if (File.Exists(_fullFilepath))
- {
- _tempFile = new FileInfo(_fullFilepath);
- CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime,true);
- }
- else
- {
- CheckAuthorizationOrGetRemoteFile(null);
- }
- }
- }
- }
- public bool Exists => _file!=null;
- public bool IsDirectory => false;
- public DateTimeOffset LastModified => _file.LastWriteTime;
- public long Length => _stream.Length;
- public string Name => _file.Name;
- public string PhysicalPath => _file.FullName;
- public Stream CreateReadStream()
- {
- return _stream;
- }
- public void CheckAuthorizationOrGetRemoteFile(DateTime? lastWriteTime, bool checkAuthorization=false)
- {
- try
- {
- var getFileReq = new GetFileRequest()
- {
- Filepath = _subpath,
- token = _token,
- checkAuthorization = checkAuthorization,
- lastWriteTime = lastWriteTime
- };
- Stopwatch sw = Stopwatch.StartNew();
- Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}:request GetFileRequest");
- var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(), JsonConvert.SerializeObject(getFileReq));
- Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}:response GetFileRequest");
- Console.WriteLine($"GetFileRequest ellapsed:{sw.ElapsedMilliseconds} ms");
- sw.Restart();
- var response = JsonConvert.DeserializeObject<GetFileResponse>(responseStr);
- Console.WriteLine($"DeserializeObject ellapsed:{sw.ElapsedMilliseconds} ms");
- Console.WriteLine(response.AppErrCode+":"+ response.AppErrMsg);
- sw.Restart();
- if (response.AppErrCode == "900")
- {
- if (!checkAuthorization)
- {
- var fileDir = Path.GetDirectoryName(_fullFilepath);
- if (!Directory.Exists(fileDir))
- {
- Directory.CreateDirectory(fileDir);
- }
- File.WriteAllBytes(_fullFilepath, response.FileData);
- }
- else
- {
- if (response.FileData != null && response.FileData.Length > 0)
- {
- var fileDir = Path.GetDirectoryName(_fullFilepath);
- if (!Directory.Exists(fileDir))
- {
- Directory.CreateDirectory(fileDir);
- }
- File.WriteAllBytes(_fullFilepath, response.FileData);
- }
- }
- Console.WriteLine($"WriteAllBytes ellapsed:{sw.ElapsedMilliseconds} ms");
- sw.Restart();
- _file = new FileInfo(_fullFilepath);
- writeFileStream();
- Console.WriteLine($"OpenRead ellapsed:{sw.ElapsedMilliseconds} ms");
- }
- }catch(Exception ex)
- {
- Trace.WriteLine($"CheckAuthorizationOrGetRemoteFile:{ex}");
- Console.WriteLine($"CheckAuthorizationOrGetRemoteFile:{ex}");
- }
- }
- public void writeFileStream()
- {
- try
- {
- if (_file == null) return;
- byte[] buff = new byte[1024];
- using (var fs = _file.OpenRead())
- {
- int readCount = fs.Read(buff, 0, buff.Length);
- while (readCount > 0)
- {
- _stream.Write(buff, 0, readCount);
- readCount = fs.Read(buff, 0, buff.Length);
- }
- }
- }catch(Exception ex)
- {
- Trace.WriteLine($"writeFileStream:{ex}");
- Console.WriteLine($"writeFileStream:{ex}");
- }
- }
- }
- }
|