|
- using Aliyun.OSS;
- using System;
- using log4net;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.IO;
- using static THUploadOss.Common;
- using Newtonsoft.Json.Linq;
- using Aliyun.OSS.Common;
- using System.Runtime.InteropServices;
- using System.Runtime.InteropServices.ComTypes;
- using System.Security.Policy;
-
- namespace THUploadOss
- {
- public class THUploadFile
- {
- public string m_accessKeyId;
- public string m_accessKeySecret;
- public string m_endpoint;
- public string m_bucketName;
-
- public string m_objectName;
- public string m_localPicPath;
- public string m_localVideoPath;
- private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- public static float m_uploadProgressAdd = 0;
- public static OssClient client = null;
- int partSize = 1024 * 1024; //分片上传大小
- public bool InitUploadFile()
- {
- if (!getMqttConfig())
- return false;
- return true;
- }
-
- public void UploadFile()
- {
- if (DownloadFinsh == 1)
- {
- client = new OssClient(m_endpoint, m_accessKeyId, m_accessKeySecret);
- string[] files1 = Directory.GetFiles(m_localVideoPath + RequestId);
- string[] files2 = Directory.GetFiles(m_localPicPath + RequestId);
- string[] files = files1.Concat(files2).ToArray();
-
- lock (lockFileList)
- {
- for(int i= FileList.Count-1; i>=0 ; i--)
- {
- FileList.Remove(FileList[i]);
- }
- foreach (string file in files1)
- { // 设置URL过期时间为1小时
- FileStates fileState = new FileStates();
- int lastIndex = file.LastIndexOf('\\');
- fileState.fileName = file.Substring(lastIndex + 1);
- string[] endpoint = m_endpoint.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- string url = "https://" + m_bucketName + "." + endpoint[endpoint.Length - 1] + "/" + RequestId + "/" + fileState.fileName;
- fileState.url = url; //"oss://" +m_bucketName+"/"+ RequestId + "/" + fileState.fileName;
- fileState.status = 5;
- fileState.progress = "0.0000";
- fileState.fileType = "video";
- FileList.Add(fileState);
- }
- foreach (string file in files2)
- {
- FileStates fileState = new FileStates();
- int lastIndex = file.LastIndexOf('\\');
- fileState.fileName = file.Substring(lastIndex + 1);
- string[] endpoint = m_endpoint.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- string url = "https://" + m_bucketName + "." + endpoint[endpoint.Length - 1] + "/" + RequestId + "/" + fileState.fileName;
- fileState.url = url;
- fileState.status = 5;
- fileState.progress = "0.0000";
- fileState.fileType = "pic";
- FileList.Add(fileState);
- }
- }
-
- int index = 0;
- foreach (string file in files)
- {
- FileInfo fileinfo = new FileInfo(file);
- string[] result = file.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
- string name = result[result.Length - 1];
- string objectPath = m_objectName + RequestId + "/" + name;
- if (fileinfo.Length == 0)
- {
- lock (lockFileList)
- {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 15,
- progress = "1.0000",
- });
- FileList.Remove(FileList[index + 1]);
- }
- index++;
- continue;
- }
- if (StsrtDownload == -1)
- {
- Status = (int)UPLOAD_STATUS.Ccancellation;
- return ;
- }
- try
- {
- var uploadId = InitiateMultipartUpload(this.m_bucketName, objectPath);
- var partETags = UploadParts(this.m_bucketName, objectPath, file, uploadId, partSize, index);
- if(partETags != null)
- {
- CompleteUploadPart(this.m_bucketName, objectPath, uploadId, partETags);
- bool doesexist = client.DoesObjectExist(this.m_bucketName, objectPath);
-
- if (doesexist)
- {
- lock (lockFileList)
- {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 15,
- progress = "1.0000",
- });
- FileList.Remove(FileList[index + 1]);
- }
- Logger.WriteInfo("THUploadFile: " + FileList[index].fileName + " UploadFile-上传成功。");
- }
- else
- {
- lock (lockFileList)
- {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 20,
- progress = (UploadPercent).ToString(),
-
- });
- FileList.Remove(FileList[index + 1]);
- }
- Logger.WriteError("THUploadFile: " + FileList[index].fileName + " UploadFile-上传失败。");
- }
- }
- }
- catch (OssException ex)
- {
- lock (lockFileList)
- {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 20,
- progress = (UploadPercent).ToString(),
-
- });
- FileList.Remove(FileList[index + 1]);
- }
- Logger.WriteError("THUploadFile: " + FileList[index].fileName + " UploadFile-上传失败。" + ex.Message + "。");
- }
- index++;
- }
- DownloadFinsh = -1;
- StsrtDownload = -1;
- Status = (int)UPLOAD_STATUS.Complete;
- }
- else
- System.Threading.Thread.Sleep(1000);
- }
- private static string InitiateMultipartUpload(String bucketName, String objectName)
- {
- try
- {
- var request = new InitiateMultipartUploadRequest(bucketName, objectName);
- var result = client.InitiateMultipartUpload(request);
- return result.UploadId;
- }
- catch
- {
- Logger.WriteError("THUploadFile: InitiateMultipartUpload-初始化失败");
- }
- return null;
- }
-
- private static List<PartETag> UploadParts(String bucketName, String objectName, String fileToUpload, String uploadId, int partSize,int index)
- {
- m_uploadProgressAdd = 0;
- var fi = new FileInfo(fileToUpload);
- var fileSize = fi.Length;//单个文件大小
- var partCount = fileSize / partSize;
- if (fileSize % partSize != 0)
- {
- partCount++;
- }
-
- var partETags = new List<PartETag>();
-
- try
- {
- using (var fs = File.Open(fileToUpload, FileMode.Open))
- {
- for (var i = 0; i < partCount; i++)
- {
- var skipBytes = (long)partSize * i;
- fs.Seek(skipBytes, 0);
- var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
-
- m_uploadProgressAdd += size;
- UploadPercent = Math.Round((Double)m_uploadProgressAdd / (Double)fileSize, 4);
- Logger.WriteInfo("THUploadFile: UploadParts-UploadPercent= " + UploadPercent);
- if (StsrtDownload == -1)
- {
- Status = (int)UPLOAD_STATUS.Ccancellation;
- return null;
- }
- lock (lockFileList) {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 10,
- progress = (UploadPercent).ToString(),
- });
- FileList.Remove(FileList[index + 1]);
- }
- //FileList[].progress = (UploadPercent).ToString();
- var request = new UploadPartRequest(bucketName, objectName, uploadId)
- {
- InputStream = fs,
- PartSize = size,
- PartNumber = i + 1
- };
- var result = client.UploadPart(request);
- partETags.Add(result.PartETag);
- }
- }
- }
- catch (Exception ex)
- {
- lock (lockFileList)
- {
- FileList.Insert(index, new FileStates()
- {
- fileName = FileList[index].fileName,
- fileType = FileList[index].fileType,
- url = FileList[index].url,
- status = 20,
- progress = (UploadPercent).ToString(),
-
- });
- FileList.Remove(FileList[index + 1]);
- }
- Logger.WriteError(ex.ToString());
- }
- return partETags;
- }
-
- private static CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName, String uploadId, List<PartETag> partETags)
- {
- try
- {
- var completeMultipartUploadRequest =
- new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);
- foreach (var partETag in partETags)
- {
- completeMultipartUploadRequest.PartETags.Add(partETag);
- }
-
- return client.CompleteMultipartUpload(completeMultipartUploadRequest);
- }catch (Exception ex)
- {
- Logger.WriteError(ex.ToString());
- }
- return null;
- }
-
- //实现删除指定文件夹下的所有文件
- public bool RemoveAllFile(string path)
- {
- try
- {
- Directory.Delete(path, true);
- Logger.WriteInfo("THUploadFile: RemoveAllFile(" + path + ")-操作完成");
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteError("THUploadFile: RemoveAllFile(" + path + ")-发生异常:" + ex.Message);
- return false;
- }
- }
-
- public bool getMqttConfig()
- {
- if (System.IO.File.Exists(ConfigPath))
- {
- try
- {
- StringBuilder accessKeyId = new StringBuilder(200);
- GetPrivateProfileString("ALIYUN", "accessKeyId", "配置文件存在,读取未成功!", accessKeyId, 255, ConfigPath);
- m_accessKeyId = accessKeyId.ToString();
-
- StringBuilder accessKeySecret = new StringBuilder(200);
- GetPrivateProfileString("ALIYUN", "accessKeySecret", "配置文件存在,读取未成功!", accessKeySecret, 255, ConfigPath);
- m_accessKeySecret = accessKeySecret.ToString();
-
- StringBuilder endpoint = new StringBuilder(200);
- GetPrivateProfileString("ALIYUN", "endpoint", "配置文件存在,读取未成功!", endpoint, 255, ConfigPath);
- m_endpoint = endpoint.ToString();
-
- StringBuilder bucketName = new StringBuilder(200);
- GetPrivateProfileString("ALIYUN", "bucketName", "配置文件存在,读取未成功!", bucketName, 255, ConfigPath);
- m_bucketName = bucketName.ToString();
-
- StringBuilder objectName = new StringBuilder(200);
- GetPrivateProfileString("ALIYUN", "objectName", "配置文件存在,读取未成功!", objectName, 255, ConfigPath);
- m_objectName = objectName.ToString();
-
- StringBuilder localPicPath = new StringBuilder(10);
- GetPrivateProfileString("SMBPATH", "localPicPath", "配置文件存在,读取未成功!", localPicPath, 255, ConfigPath);
- m_localPicPath = localPicPath.ToString();
-
- StringBuilder localVideoPath = new StringBuilder(10);
- GetPrivateProfileString("SMBPATH", "localVideoPath", "配置文件存在,读取未成功!", localVideoPath, 255, ConfigPath);
- m_localVideoPath = localVideoPath.ToString();
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteError("THUploadFile: "+ ex.Message.ToString());
- return false;
- }
- }
- return false;
- }
- }
- }
-
- //public int UploadFile()
- //{
- // if (DownloadFinsh == 1)
- // {
- // string[] files = Directory.GetFiles(m_localPath + RequestId);
- // long fileSize = 0;
- // foreach (string file in files)
- // {
- // FileInfo fileinfo = new FileInfo(file);
- // fileSize += fileinfo.Length;
- // }
- // foreach (string file in files)
- // {
- // FileInfo fileinfo = new FileInfo(file);
- // long len = fileinfo.Length;
- // float process = 0;
-
- // Console.WriteLine(file);
- // string[] result = file.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
- // string name = result[result.Length - 1];
- // Console.WriteLine(name);
- // string objectPath = m_objectName;
- // objectPath = objectPath + RequestId + "/" + name;
- // var fs = File.Open(file, FileMode.Open);
- // var client = new OssClient(m_endpoint, m_accessKeyId, m_accessKeySecret);
- // PutObjectRequest putObjectRequest = new PutObjectRequest(m_bucketName, objectPath, fs);
- // putObjectRequest.StreamTransferProgress += (obg, args) =>
- // {
- // float putProcess = (args.TransferredBytes * 100 / args.TotalBytes) / 100.0f;
- // // action.Invoke(putProcess);
- // if (putProcess >= 1)
- // {
- // // complete?.Invoke();
- // putObjectRequest.StreamTransferProgress = null;
- // }
-
- // process = (putProcess * len) / fileSize;
-
- // if ((m_uploadProgressAdd + putProcess) >= 0.01)
- // {
- // if (putProcess == 1)
- // {
- // m_uploadProgressAdd += process;
- // UploadPercent = m_uploadProgressAdd * 100;
- // }
- // else
- // {
- // UploadPercent = (m_uploadProgressAdd + process) *100;
- // }
- // Console.WriteLine(UploadPercent.ToString());
- // }
-
- // };
- // try
- // {
- // client.PutObject(putObjectRequest);
- // } catch (Exception ex)
- // {
- // Console.WriteLine(ex.Message);
- // return -1;
- // }
- // }
- // DownloadFinsh = 0;
- // UploadFinsh = 1;
- // Status = (int)UPLOAD_STATUS.Complete;
- // return 0;
-
- // }
- // return -1;
- //}
|