Commit b934dc6e authored by liudong1993's avatar liudong1993

转图片优化

parent a5db7935
...@@ -48,6 +48,31 @@ namespace COSSnippet ...@@ -48,6 +48,31 @@ namespace COSSnippet
} }
} }
/// <summary>
/// 文件是否存在
/// </summary>
/// <param name="AliFileName"></param>
public bool SelectFile(string AliFileName) {
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// 判断文件是否存在。
var exist = client.DoesObjectExist(bucketName, AliFileName);
return exist;
}
catch (Aliyun.OSS.Common.OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
return false;
}
/// <summary> /// <summary>
/// 删除文件 /// 删除文件
/// </summary> /// </summary>
......
...@@ -19,7 +19,7 @@ namespace Mall.Education ...@@ -19,7 +19,7 @@ namespace Mall.Education
ServiceBase[] ServicesToRun; ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] ServicesToRun = new ServiceBase[]
{ {
new RebornTimerServer(), new EducationToImageServer(),
}; };
ServiceBase.Run(ServicesToRun); ServiceBase.Run(ServicesToRun);
//Console.WriteLine("进来了"); //Console.WriteLine("进来了");
......
...@@ -39,8 +39,8 @@ ...@@ -39,8 +39,8 @@
// //
// serviceInstaller1 // serviceInstaller1
// //
this.serviceInstaller1.Description = "RebornTimerService定时器服务"; this.serviceInstaller1.Description = "EducationToImageServer服务";
this.serviceInstaller1.ServiceName = "RebornTimerServer"; this.serviceInstaller1.ServiceName = "EducationToImageServer";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
// //
// ProjectInstaller // ProjectInstaller
......
This diff is collapsed.
...@@ -10,6 +10,7 @@ using COSSnippet; ...@@ -10,6 +10,7 @@ using COSSnippet;
using Mall.Education.Helper; using Mall.Education.Helper;
using System.Collections.Generic; using System.Collections.Generic;
using FluentScheduler; using FluentScheduler;
using System.Linq;
namespace Mall.Education.RabbitMQ namespace Mall.Education.RabbitMQ
{ {
...@@ -128,6 +129,7 @@ namespace Mall.Education.RabbitMQ ...@@ -128,6 +129,7 @@ namespace Mall.Education.RabbitMQ
if (PageNum > 0) if (PageNum > 0)
{ {
//List<string> DelFile = new List<string>();
List<GoodsImage> goodsImages = new List<GoodsImage>(); List<GoodsImage> goodsImages = new List<GoodsImage>();
//开始上传图片至服务器 //开始上传图片至服务器
if (GoodsModel.PathType == 1) if (GoodsModel.PathType == 1)
...@@ -136,7 +138,10 @@ namespace Mall.Education.RabbitMQ ...@@ -136,7 +138,10 @@ namespace Mall.Education.RabbitMQ
for (var i = 1; i <= PageNum; i++) for (var i = 1; i <= PageNum; i++)
{ {
string newName = FileName + "_" + i.ToString("000") + "." + imageFormat.ToString(); string newName = FileName + "_" + i.ToString("000") + "." + imageFormat.ToString();
m.TransferUploadFile(tempDir + "/" + newName, FileDirNot + newName); if (!m.SelectFile(FileDirNot + newName))
{
m.TransferUploadFile(tempDir + "/" + newName, FileDirNot + newName);
}
//上传之后 写入商品课程图片数据 //上传之后 写入商品课程图片数据
goodsImages.Add(new GoodsImage goodsImages.Add(new GoodsImage
...@@ -146,7 +151,8 @@ namespace Mall.Education.RabbitMQ ...@@ -146,7 +151,8 @@ namespace Mall.Education.RabbitMQ
}); });
//上传之后 删除本地临时文件 //上传之后 删除本地临时文件
File.Delete(tempDir + "/" + newName); //File.Delete(tempDir + "/" + newName);
//DelFile.Add(tempDir + "/" + newName);
} }
} }
else if (GoodsModel.PathType == 2) { else if (GoodsModel.PathType == 2) {
...@@ -154,7 +160,10 @@ namespace Mall.Education.RabbitMQ ...@@ -154,7 +160,10 @@ namespace Mall.Education.RabbitMQ
for (var i = 1; i <= PageNum; i++) for (var i = 1; i <= PageNum; i++)
{ {
string newName = FileName + "_" + i.ToString("000") + "." + imageFormat.ToString(); string newName = FileName + "_" + i.ToString("000") + "." + imageFormat.ToString();
upload.TransferUploadFile(tempDir + "/" + newName, FileDirNot + newName); if (!upload.SelectFile(FileDirNot + newName))
{
upload.TransferUploadFile(tempDir + "/" + newName, FileDirNot + newName);
}
//上传之后 写入商品课程图片数据 //上传之后 写入商品课程图片数据
goodsImages.Add(new GoodsImage goodsImages.Add(new GoodsImage
...@@ -164,7 +173,8 @@ namespace Mall.Education.RabbitMQ ...@@ -164,7 +173,8 @@ namespace Mall.Education.RabbitMQ
}); });
//上传之后 删除本地临时文件 //上传之后 删除本地临时文件
File.Delete(tempDir + "/" + newName); //File.Delete(tempDir + "/" + newName);
//DelFile.Add(tempDir + "/" + newName);
} }
} }
...@@ -173,6 +183,15 @@ namespace Mall.Education.RabbitMQ ...@@ -173,6 +183,15 @@ namespace Mall.Education.RabbitMQ
if (flag == false) { if (flag == false) {
LogHelper.Write("更新课程图片列表失败:CourseId:" + GoodsModel.CourseId); LogHelper.Write("更新课程图片列表失败:CourseId:" + GoodsModel.CourseId);
} }
//if (DelFile.Any()) {
// System.Threading.Tasks.Task.Run(() => {
// System.Threading.Thread.Sleep(100000);
// foreach (var item in DelFile) {
// File.Delete(item);
// }
// });
//}
} }
} }
} }
...@@ -198,7 +217,7 @@ namespace Mall.Education.RabbitMQ ...@@ -198,7 +217,7 @@ namespace Mall.Education.RabbitMQ
{ {
LogHelper.Write(ex, "DelTemporaryFile"); LogHelper.Write(ex, "DelTemporaryFile");
} }
}).ToRunEvery(1).Days().At(1, 10); }).ToRunNow().AndEvery(1).Hours();
} }
...@@ -219,7 +238,7 @@ namespace Mall.Education.RabbitMQ ...@@ -219,7 +238,7 @@ namespace Mall.Education.RabbitMQ
} }
foreach (FileInfo fi in dir.GetFiles()) foreach (FileInfo fi in dir.GetFiles())
{ {
if (fi.CreationTime < DateTime.Now.AddDays(-1)) if (fi.CreationTime < DateTime.Now.AddHours(-1))
fi.Delete(); fi.Delete();
} }
} }
......
namespace Mall.Education namespace Mall.Education
{ {
partial class RebornTimerServer partial class EducationToImageServer
{ {
/// <summary> /// <summary>
/// 必需的设计器变量。 /// 必需的设计器变量。
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); components = new System.ComponentModel.Container();
this.ServiceName = "RebornTimerServer"; this.ServiceName = "EducationToImageServer";
} }
#endregion #endregion
......
...@@ -12,9 +12,9 @@ using System.Threading.Tasks; ...@@ -12,9 +12,9 @@ using System.Threading.Tasks;
namespace Mall.Education namespace Mall.Education
{ {
partial class RebornTimerServer : ServiceBase partial class EducationToImageServer : ServiceBase
{ {
public RebornTimerServer() public EducationToImageServer()
{ {
InitializeComponent(); InitializeComponent();
} }
......
...@@ -118,6 +118,87 @@ namespace COSSnippet ...@@ -118,6 +118,87 @@ namespace COSSnippet
//.cssg-snippet-body-end //.cssg-snippet-body-end
} }
/// <summary>
/// 查询文件
/// </summary>
/// <param name="cosFileName"></param>
public bool SelectFile(string cosFileName) {
try
{
string bucket = bucketName; //存储桶,格式:BucketName-APPID
string key = cosFileName; //对象键
HeadObjectRequest request = new HeadObjectRequest(bucket, key);
//执行请求
HeadObjectResult result = cosXml.HeadObject(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
LogHelper.Write(result.GetResultInfo());
if (result.GetResultInfo().Contains("200 OK"))
{
return true;
}
else {
return false;
}
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
return false;
}
/// 高级接口下载对象
public void TransferDownloadObject(string filePath,string fileName, string cosPathName)
{
//.cssg-snippet-body-start:[transfer-download-object]
// 初始化 TransferConfig
TransferConfig transferConfig = new TransferConfig();
// 初始化 TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
String bucket = bucketName; //存储桶,格式:BucketName-APPID
String cosPath = cosPathName; //对象在存储桶中的位置标识符,即称对象键
string localDir = filePath;//本地文件夹
string localFileName = fileName; //指定本地保存的文件名
// 下载对象
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
localDir, localFileName);
downloadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
downloadTask.successCallback = delegate (CosResult cosResult)
{
COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = cosResult
as COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult;
Console.WriteLine(result.GetResultInfo());
string eTag = result.eTag;
};
downloadTask.failCallback = delegate (CosClientException clientEx, CosServerException serverEx)
{
if (clientEx != null)
{
Console.WriteLine("CosClientException: " + clientEx);
}
if (serverEx != null)
{
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
};
transferManager.Download(downloadTask);
//.cssg-snippet-body-end
}
/// 高级接口上传二进制数据 /// 高级接口上传二进制数据
public void TransferUploadBytes() public void TransferUploadBytes()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment