|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- using Common;
- using Common.system;
-
- using Newtonsoft.Json.Linq;
-
- using System;
- using System.Collections.Specialized;
- using System.Threading;
-
- using XHWK.Model;
-
- namespace XHWK.WKTool.DAL
- {
-
-
-
-
-
- public class DAL_Upload
- {
-
- private string FileRequestAddress = APP.uploadUrl;
-
-
-
-
-
-
-
- public bool IsAllowUploaded(string MD5, out string Message)
- {
- Exception ex = null;
- Message = "";
- for (int num = 0; num < 5; num++)
- {
- try
- {
- JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/isexist", @"application/x-www-form-urlencoded", @"md5=" + MD5, "");
- if(jo==null)
- {
- Message = "无法访问文件服务器,请检查网络或文件服务器地址。";
- return false;
- }
-
-
- if (jo["code"].ToString() == "0")
- {
- Message = "";
-
- Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString());
-
- APP.ResourceAddTwo = resultObj.obj;
-
- return false;
-
-
-
-
-
-
-
-
-
-
- }
- else
- {
- Message = "";
-
- return true;
- }
- }
- catch (Exception e)
- {
- Message = "上传失败,无法访问到服务器:"+e.Message;
- ex = e;
- Thread.Sleep(1000);
- }
- }
- string ErrMessage = "【文件是否存在】(IsUploaded):请求失败。" + Message;
- LogHelper.WriteErrLog(ErrMessage, ex);
- return false;
- }
-
-
-
-
-
-
-
- public bool ReportFileMerge(string Savefolder, string FileCode, out string Message)
- {
- Exception ex = null;
- Message = "";
- for (int num = 0; num < 5; num++)
- {
- try
- {
- JObject jo = HttpHelper.PostFunction(FileRequestAddress + @"/chunkdb/mergechunk", @"application/x-www-form-urlencoded", @"savefolder=" + Savefolder + "&identifier=" + FileCode, "");
- if (jo == null)
- {
- Message = "无法访问文件服务器,请检查网络或文件服务器地址。";
- return false;
- }
- Model.ResultVo<Model_ResourceAddTwo> resultObj = JsonHelper.JsonToObj<Model.ResultVo<Model_ResourceAddTwo>>(jo.ToString());
- APP.ResourceAddTwo = new Model_ResourceAddTwo();
-
- if (resultObj.code == 0 && resultObj.obj != null)
- {
- APP.ResourceAddTwo = resultObj.obj;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return true;
- }
- else
- {
-
- Message = jo["msg"].ToString();
- return false;
- }
- }
- catch (Exception e)
- {
- Message = e.Message;
- ex = e;
- Thread.Sleep(1000);
- }
- }
- string ErrMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + Message;
- LogHelper.WriteErrLog(ErrMessage, ex);
- return false;
- }
-
-
-
-
-
- public bool UploadVideo(string VideoGuid, out string ErrMessage)
- {
- ErrMessage = "";
- try
- {
- Model_Video VideoInfo = null;
- foreach (Model_WKData Vdata in APP.WKDataList)
- {
- if (Vdata.VideoList == null)
- {
- continue;
- }
-
- foreach (Model_Video videoinfo in Vdata.VideoList)
- {
- if (videoinfo.FileGuid == VideoGuid)
- {
- VideoInfo = videoinfo;
- break;
- }
- }
- if (VideoInfo != null)
- {
- break;
- }
- }
- if (VideoInfo == null)
- {
- ErrMessage = "未找到课程!";
- return false;
- }
- string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";
- if (VideoInfo.IsUpload)
- {
- ErrMessage = "视频已上传";
- return false;
- }
- else
- {
- return true;
- }
-
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
- }
- return false;
- }
-
-
-
-
- public bool UploadVideoTwo(string VideoGuid, out string ErrMessage)
- {
- ErrMessage = "";
- try
- {
- Model_Video VideoInfo = null;
- foreach (Model_WKData Vdata in APP.WKDataList)
- {
- if (Vdata.VideoList == null)
- {
- continue;
- }
-
- foreach (Model_Video videoinfo in Vdata.VideoList)
- {
- if (videoinfo.FileGuid == VideoGuid)
- {
- VideoInfo = videoinfo;
- break;
- }
- }
- if (VideoInfo != null)
- {
- break;
- }
- }
- if (VideoInfo == null)
- {
- ErrMessage = "未找到课程!";
- return false;
- }
- string UploadUrl = FileRequestAddress + "/chunkdb/upchunk";
- if (VideoInfo.IsUpload)
- {
- ErrMessage = "视频已上传";
- return false;
- }
- else
- {
- if (string.IsNullOrWhiteSpace(VideoInfo.FileMD5))
- {
- VideoInfo.FileMD5 = AESHelper.AESEncrypt(FileToolsCommon.ReadBigFileStr(VideoInfo.VideoPath, 1024));
- }
-
-
- long filelen = FileToolsCommon.GetFileSize(VideoInfo.VideoPath);
-
- double UploadSliceLenMB = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
- string fileName = FileToolsCommon.GetFileName(VideoInfo.VideoPath);
-
- bool IsSequel = true;
-
- if(VideoInfo.Block>0)
- {
-
- if(VideoInfo.Block> VideoInfo.Uploaded)
- {
-
- IsSequel = true;
- }
- else
- {
-
- IsSequel = false;
-
- #region 合并文件
-
- bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
- if (isres)
- {
- VideoInfo.IsUpload = true;
- return true;
- }
- else
- {
- return false;
- }
- #endregion
-
- }
- }
- else
- {
-
- if (IsAllowUploaded(VideoInfo.FileMD5, out ErrMessage))
- {
- IsSequel = true;
- if (VideoInfo.SliceLen == 0)
- {
- VideoInfo.SliceLen = (long)(UploadSliceLenMB * 1024 * 1024);
- VideoInfo.Block = (int)(filelen / VideoInfo.SliceLen + (filelen % VideoInfo.SliceLen > 0 ? 1 : 0));
- }
- }
- else
- {
-
- IsSequel = false;
- VideoInfo.Block = 1;
- VideoInfo.Uploaded = 1;
-
- VideoInfo.IsUpload = true;
- return true;
- }
-
- }
-
-
-
- if (IsSequel)
- {
-
- long len = VideoInfo.Uploaded * VideoInfo.SliceLen;
-
- #region 废弃
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
-
-
- do
- {
- len = VideoInfo.Uploaded * VideoInfo.SliceLen;
-
- byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(VideoInfo.VideoPath, len, (int)VideoInfo.SliceLen);
-
- NameValueCollection formFields = new NameValueCollection
- {
- { "identifier", VideoInfo.FileGuid },
- { "chunkNumber", (VideoInfo.Uploaded + 1).ToString() },
- { "filename", fileName },
- { "totalchunk", VideoInfo.Block.ToString() },
- { "md5", VideoInfo.FileMD5 }
- };
-
-
-
-
-
-
- JObject jo = HttpHelper.UploadRequestflow(UploadUrl, byteArray, fileName, formFields);
-
- if (jo["code"].ToString() != "0")
- {
- ErrMessage = jo["msg"].ToString();
- return false;
- }
- else
- {
- VideoInfo.Uploaded++;
- }
- } while (len + VideoInfo.SliceLen < filelen);
-
- #region 合并文件
-
- bool isres = ReportFileMerge(APP.UserInfo.Schoolid.ToString() + "/resource", VideoInfo.FileGuid, out ErrMessage);
- if (isres)
- {
-
- return true;
- }
- else
- {
- return false;
- }
- #endregion
- }
- else
- {
- if (string.IsNullOrWhiteSpace(ErrMessage))
- {
-
- return true;
- }
- else
- {
- return false;
- }
- }
- }
-
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【视频上传】(UploadVideo)视频上传失败:" + ex.Message, ex);
- }
- return false;
- }
- }
- }
|