using LJLib.Net.SPI.Client; using LJProxy.Models; using Microsoft.Extensions.FileProviders; using Newtonsoft.Json; using System; using System.Collections.Generic; 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 Stream _stream; private ILJClient _ljPool; private static object _syncRoot = new object(); public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool) { _fullFilepath = fullFilepath; _subpath = subpath; _ljPool = ljPool; if (File.Exists(_fullFilepath)) { _file = new FileInfo(_fullFilepath); _stream = _file.OpenRead(); } else { lock (_syncRoot) { if (File.Exists(_fullFilepath)) { _file = new FileInfo(_fullFilepath); _stream = _file.OpenRead(); } else { GetRemoteFile(); if (File.Exists(_fullFilepath)) { _file = new FileInfo(_fullFilepath); _stream = _file.OpenRead(); } } } } } 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 GetRemoteFile() { var getFileReq = new GetFileRequest() { Filepath = _subpath }; var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(),JsonConvert.SerializeObject(getFileReq)); var response = JsonConvert.DeserializeObject(responseStr); if (string.IsNullOrEmpty(response.ErrMsg)) { var fileDir = Path.GetDirectoryName(_fullFilepath); if (!Directory.Exists(fileDir)){ Directory.CreateDirectory(fileDir); } File.WriteAllBytes(_fullFilepath, response.FileData); } } } }