You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.4 KiB
C#

9 months ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JumpKick.HttpLib;
using POSV.Entity;
namespace POSV.HttpApi
{
public class DownloadSorePrintImage
{
protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static bool SorePrintImage()
{
bool result = true;
try
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory , @"images/bmp");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
List<StorePrintImage> images = null;
using(var db = Global.Instance.OpenDataBase)
{
images = db.Query<StorePrintImage>().ToList();
}
//有效的图片名称
List<string> validNames = new List<string>();
if (images != null && images.Count > 0)
{
foreach(var image in images)
{
var fileName = string.Empty;
if (!string.IsNullOrEmpty(image.StorageFileName) && image.StorageFileName.Contains("/"))
{
fileName = image.StorageFileName.Substring(image.StorageFileName.LastIndexOf('/') + 1);
}
if (!string.IsNullOrEmpty(fileName))
{
validNames.Add(fileName);
string targetFileName = filePath + Path.DirectorySeparatorChar + fileName;
if (File.Exists(targetFileName))
{
logger.Info("票头图片{0}已经下载,忽略本次操作", image.StorageFileName);
continue;
}
if (!Global.Instance.Online)
{
logger.Info("离线,无法下载图片{0}", image.StorageFileName);
continue;
}
var url = image.Url;
Http.Get(url).DownloadTo(targetFileName , onProgressChanged: (bytesCopied , totalBytes) => {
if (totalBytes.HasValue)
{
logger.Info("Downloading: " + (bytesCopied / totalBytes) * 100 + "%");
}
} ,onSuccess:(header)=> {
logger.Info("Download Complete");
}).Go();
}
}
}
//清理工作,清理下载的无用的打印图片,节省空间
DirectoryInfo root = new DirectoryInfo(filePath);
foreach (FileInfo f in root.GetFiles())
{
if (!validNames.Contains(f.Name))
{
f.Delete();
}
}
}
catch (Exception ex)
{
logger.Error(ex , "票头票尾图片下载异常");
result = false;
}
return result;
}
}
}