|
@@ -14,7 +14,7 @@ namespace XHWK.WKTool.DAL
|
14
|
14
|
using system;
|
15
|
15
|
|
16
|
16
|
/// <summary>
|
17
|
|
- /// 上传相关方法 创建人:赵耀 创建时间:2020年9月4日
|
|
17
|
+ /// 上传相关方法 创建人:赵耀 创建时间:2020年9月4日
|
18
|
18
|
/// </summary>
|
19
|
19
|
public class DalUpload
|
20
|
20
|
{
|
|
@@ -22,13 +22,13 @@ namespace XHWK.WKTool.DAL
|
22
|
22
|
|
23
|
23
|
//private string schoolfileRequestAddress = APP.showImageUrl;
|
24
|
24
|
/// <summary>
|
25
|
|
- /// 文件是否允许上传
|
|
25
|
+ /// 文件是否允许上传
|
26
|
26
|
/// </summary>
|
27
|
27
|
/// <param name="md5">
|
28
|
|
- /// MD5
|
|
28
|
+ /// MD5
|
29
|
29
|
/// </param>
|
30
|
30
|
/// <param name="message">
|
31
|
|
- /// 错误消息
|
|
31
|
+ /// 错误消息
|
32
|
32
|
/// </param>
|
33
|
33
|
/// <returns>
|
34
|
34
|
/// </returns>
|
|
@@ -65,27 +65,31 @@ namespace XHWK.WKTool.DAL
|
65
|
65
|
Thread.Sleep(1000);
|
66
|
66
|
}
|
67
|
67
|
}
|
68
|
|
-
|
69
|
68
|
string errMessage = "【文件是否存在】(IsUploaded):请求失败。" + message;
|
70
|
69
|
LogHelper.Logerror.Error(errMessage, ex);
|
71
|
70
|
return false;
|
72
|
71
|
}
|
73
|
72
|
|
74
|
73
|
/// <summary>
|
75
|
|
- /// 上报文件合并指令
|
|
74
|
+ /// 上报文件合并指令
|
76
|
75
|
/// </summary>
|
77
|
76
|
/// <param name="savefolder">
|
78
|
|
- /// 保存文件位置 学校id/resource
|
|
77
|
+ /// 保存文件位置 学校id/resource
|
79
|
78
|
/// </param>
|
80
|
79
|
/// <param name="fileCode">
|
81
|
|
- /// 文件唯一编号 Guid
|
|
80
|
+ /// 文件唯一编号 Guid
|
82
|
81
|
/// </param>
|
83
|
82
|
/// <param name="message">
|
84
|
|
- /// 错误信息
|
|
83
|
+ /// 错误信息
|
85
|
84
|
/// </param>
|
86
|
85
|
/// <returns>
|
87
|
86
|
/// </returns>
|
88
|
|
- public bool ReportFileMerge(string savefolder, string fileCode, out string message)
|
|
87
|
+ public bool ReportFileMerge
|
|
88
|
+ (
|
|
89
|
+ string savefolder,
|
|
90
|
+ string fileCode,
|
|
91
|
+ out string message
|
|
92
|
+ )
|
89
|
93
|
{
|
90
|
94
|
Exception ex = null;
|
91
|
95
|
message = ""; //请求重试5次 共5秒
|
|
@@ -119,14 +123,13 @@ namespace XHWK.WKTool.DAL
|
119
|
123
|
Thread.Sleep(1000);
|
120
|
124
|
}
|
121
|
125
|
}
|
122
|
|
-
|
123
|
126
|
string errMessage = "【上报合并文件】(ReportFileMerge):请求失败。" + message;
|
124
|
127
|
LogHelper.Logerror.Error(errMessage, ex);
|
125
|
128
|
return false;
|
126
|
129
|
}
|
127
|
130
|
|
128
|
131
|
/// <summary>
|
129
|
|
- /// 上传视频
|
|
132
|
+ /// 上传视频
|
130
|
133
|
/// </summary>
|
131
|
134
|
/// <returns>
|
132
|
135
|
/// </returns>
|
|
@@ -155,13 +158,11 @@ namespace XHWK.WKTool.DAL
|
155
|
158
|
break;
|
156
|
159
|
}
|
157
|
160
|
}
|
158
|
|
-
|
159
|
161
|
if (videoInfo == null)
|
160
|
162
|
{
|
161
|
163
|
errMessage = "未找到课程!";
|
162
|
164
|
return false;
|
163
|
165
|
}
|
164
|
|
-
|
165
|
166
|
if (videoInfo.IsUpload)
|
166
|
167
|
{
|
167
|
168
|
errMessage = "视频已上传";
|
|
@@ -177,7 +178,7 @@ namespace XHWK.WKTool.DAL
|
177
|
178
|
}
|
178
|
179
|
|
179
|
180
|
/// <summary>
|
180
|
|
- /// 上传视频
|
|
181
|
+ /// 上传视频
|
181
|
182
|
/// </summary>
|
182
|
183
|
/// <returns>
|
183
|
184
|
/// </returns>
|
|
@@ -206,7 +207,6 @@ namespace XHWK.WKTool.DAL
|
206
|
207
|
break;
|
207
|
208
|
}
|
208
|
209
|
}
|
209
|
|
-
|
210
|
210
|
if (videoInfo == null)
|
211
|
211
|
{
|
212
|
212
|
errMessage = "未找到课程!";
|
|
@@ -218,142 +218,122 @@ namespace XHWK.WKTool.DAL
|
218
|
218
|
errMessage = "视频已上传";
|
219
|
219
|
return false;
|
220
|
220
|
}
|
221
|
|
- else
|
|
221
|
+ if (string.IsNullOrWhiteSpace(videoInfo.FileMD5))
|
222
|
222
|
{
|
223
|
|
- if (string.IsNullOrWhiteSpace(videoInfo.FileMD5))
|
224
|
|
- {
|
225
|
|
- videoInfo.FileMD5 = AesHelper.AesEncrypt(FileToolsCommon.ReadBigFileStr(videoInfo.VideoPath, 1024));
|
226
|
|
- }
|
|
223
|
+ videoInfo.FileMD5 = AesHelper.AesEncrypt(FileToolsCommon.ReadBigFileStr(videoInfo.VideoPath, 1024));
|
|
224
|
+ }
|
227
|
225
|
|
228
|
|
- //视频长度
|
229
|
|
- long filelen = FileToolsCommon.GetFileSize(videoInfo.VideoPath);
|
230
|
|
- //每片的长度
|
231
|
|
- double uploadSliceLenMb = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
|
232
|
|
- string fileName = FileToolsCommon.GetFileName(videoInfo.VideoPath);
|
233
|
|
- //是否续传
|
234
|
|
- //分块数为0时 从未上传过
|
235
|
|
- if (videoInfo.Block > 0)
|
|
226
|
+ //视频长度
|
|
227
|
+ long filelen = FileToolsCommon.GetFileSize(videoInfo.VideoPath);
|
|
228
|
+ //每片的长度
|
|
229
|
+ double uploadSliceLenMb = double.Parse(FileToolsCommon.GetConfigValue("UploadSliceLen"));
|
|
230
|
+ string fileName = FileToolsCommon.GetFileName(videoInfo.VideoPath);
|
|
231
|
+ //是否续传
|
|
232
|
+ //分块数为0时 从未上传过
|
|
233
|
+ if (videoInfo.Block > 0)
|
|
234
|
+ {
|
|
235
|
+ //是否已上传所有分块
|
|
236
|
+ if (videoInfo.Block > videoInfo.Uploaded)
|
236
|
237
|
{
|
237
|
|
- //是否已上传所有分块
|
238
|
|
- if (videoInfo.Block > videoInfo.Uploaded)
|
239
|
|
- {
|
240
|
|
- //已上传,未上传完
|
241
|
|
- }
|
242
|
|
- else
|
243
|
|
- {
|
244
|
|
- //上传完,未合并
|
245
|
|
-
|
246
|
|
- #region 合并文件
|
247
|
|
-
|
248
|
|
- //合并文件
|
249
|
|
- bool isres = ReportFileMerge
|
250
|
|
- (
|
251
|
|
- App.UserInfo.cloudcode + "/" + App.UserInfo.Schoolid.ToString() + "/resource",
|
252
|
|
- videoInfo.FileGuid,
|
253
|
|
- out errMessage
|
254
|
|
- );
|
255
|
|
- if (isres)
|
256
|
|
- {
|
257
|
|
- videoInfo.IsUpload = true;
|
258
|
|
- return true;
|
259
|
|
- }
|
260
|
|
- else
|
261
|
|
- {
|
262
|
|
- return false;
|
263
|
|
- }
|
264
|
|
-
|
265
|
|
- #endregion 合并文件
|
266
|
|
- }
|
|
238
|
+ //已上传,未上传完
|
267
|
239
|
}
|
268
|
240
|
else
|
269
|
241
|
{
|
270
|
|
- //是否允许上传
|
271
|
|
- if (IsAllowUploaded(videoInfo.FileMD5, out errMessage))
|
272
|
|
- {
|
273
|
|
- if (videoInfo.SliceLen == 0)
|
274
|
|
- {
|
275
|
|
- videoInfo.SliceLen = (long)(uploadSliceLenMb * 1024 * 1024);
|
276
|
|
- videoInfo.Block = (int)(filelen / videoInfo.SliceLen +
|
277
|
|
- (filelen % videoInfo.SliceLen > 0 ? 1 : 0));
|
278
|
|
- }
|
279
|
|
- }
|
280
|
|
- else
|
281
|
|
- {
|
282
|
|
- //文件在服务器上已存在 空间是否存在未知 若增加空间是否存在判断在此处添加
|
283
|
|
- videoInfo.Block = 1;
|
284
|
|
- videoInfo.Uploaded = 1;
|
285
|
|
-
|
286
|
|
- videoInfo.IsUpload = true;
|
287
|
|
- return true;
|
288
|
|
- }
|
289
|
|
- }
|
290
|
|
-
|
291
|
|
- ////临时修改 没有断点续传,每次上传都重新上传。
|
292
|
|
- //VideoInfo.Uploaded = 0;
|
293
|
|
- {
|
294
|
|
- //已上传长度
|
295
|
|
- long len;
|
296
|
|
-
|
297
|
|
- //分块
|
298
|
|
- do
|
299
|
|
- {
|
300
|
|
- len = videoInfo.Uploaded * videoInfo.SliceLen;
|
301
|
|
- //取指定长度的流
|
302
|
|
- byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(videoInfo.VideoPath, len,
|
303
|
|
- (int)videoInfo.SliceLen);
|
304
|
|
- //参数
|
305
|
|
- NameValueCollection formFields = new NameValueCollection
|
306
|
|
- {
|
307
|
|
- { "identifier", videoInfo.FileGuid },
|
308
|
|
- { "chunkNumber", (videoInfo.Uploaded + 1).ToString() },
|
309
|
|
- { "filename", fileName },
|
310
|
|
- { "totalchunk", videoInfo.Block.ToString() },
|
311
|
|
- { "md5", videoInfo.FileMD5 }
|
312
|
|
- };
|
313
|
|
- //formFields.Add();///教材
|
314
|
|
- //if()//章节若没选不传
|
315
|
|
- //{
|
316
|
|
- // formFields.Add();
|
317
|
|
- //}
|
318
|
|
- JObject jo = ZHttpUtil.UploadFile(
|
319
|
|
- uploadUrl,
|
320
|
|
- byteArray,
|
321
|
|
- fileName,
|
322
|
|
- formFields
|
323
|
|
- );
|
324
|
|
- //0成功,1失败
|
325
|
|
- if (jo["code"]?.ToString() != "0")
|
326
|
|
- {
|
327
|
|
- errMessage = jo["msg"]?.ToString();
|
328
|
|
- return false;
|
329
|
|
- }
|
330
|
|
- else
|
331
|
|
- {
|
332
|
|
- videoInfo.Uploaded++;
|
333
|
|
- }
|
334
|
|
- } while (len + videoInfo.SliceLen < filelen);
|
|
242
|
+ //上传完,未合并
|
335
|
243
|
|
336
|
244
|
#region 合并文件
|
337
|
245
|
|
338
|
246
|
//合并文件
|
339
|
|
- bool isres = ReportFileMerge
|
340
|
|
- (
|
|
247
|
+ bool isres = ReportFileMerge(
|
341
|
248
|
App.UserInfo.cloudcode + "/" + App.UserInfo.Schoolid.ToString() + "/resource",
|
342
|
249
|
videoInfo.FileGuid,
|
343
|
250
|
out errMessage
|
344
|
251
|
);
|
345
|
252
|
if (isres)
|
346
|
253
|
{
|
347
|
|
- //VideoInfo.IsUpload = true;
|
348
|
254
|
return true;
|
349
|
255
|
}
|
350
|
|
- else
|
|
256
|
+ return false;
|
|
257
|
+
|
|
258
|
+ #endregion 合并文件
|
|
259
|
+ }
|
|
260
|
+ }
|
|
261
|
+ else
|
|
262
|
+ {
|
|
263
|
+ //是否允许上传
|
|
264
|
+ if (IsAllowUploaded(videoInfo.FileMD5, out errMessage))
|
|
265
|
+ {
|
|
266
|
+ if (videoInfo.SliceLen == 0)
|
|
267
|
+ {
|
|
268
|
+ videoInfo.SliceLen = (long)(uploadSliceLenMb * 1024 * 1024);
|
|
269
|
+ videoInfo.Block = (int)(filelen / videoInfo.SliceLen + (filelen % videoInfo.SliceLen > 0 ? 1 : 0));
|
|
270
|
+ }
|
|
271
|
+ }
|
|
272
|
+ else
|
|
273
|
+ {
|
|
274
|
+ //文件在服务器上已存在 空间是否存在未知 若增加空间是否存在判断在此处添加
|
|
275
|
+ videoInfo.Block = 1;
|
|
276
|
+ videoInfo.Uploaded = 1;
|
|
277
|
+ return true;
|
|
278
|
+ }
|
|
279
|
+ }
|
|
280
|
+
|
|
281
|
+ ////临时修改 没有断点续传,每次上传都重新上传。
|
|
282
|
+ //VideoInfo.Uploaded = 0;
|
|
283
|
+ {
|
|
284
|
+ //已上传长度
|
|
285
|
+ long len;
|
|
286
|
+
|
|
287
|
+ //分块
|
|
288
|
+ do
|
|
289
|
+ {
|
|
290
|
+ len = videoInfo.Uploaded * videoInfo.SliceLen;
|
|
291
|
+ //取指定长度的流
|
|
292
|
+ byte[] byteArray = FileToolsCommon.ReadBigFileSpecifyLength(
|
|
293
|
+ videoInfo.VideoPath,
|
|
294
|
+ len,
|
|
295
|
+ (int)videoInfo.SliceLen
|
|
296
|
+ );
|
|
297
|
+ //参数
|
|
298
|
+ NameValueCollection formFields = new NameValueCollection
|
|
299
|
+ {
|
|
300
|
+ { "identifier", videoInfo.FileGuid },
|
|
301
|
+ { "chunkNumber", (videoInfo.Uploaded + 1).ToString() },
|
|
302
|
+ { "filename", fileName },
|
|
303
|
+ { "totalchunk", videoInfo.Block.ToString() },
|
|
304
|
+ { "md5", videoInfo.FileMD5 }
|
|
305
|
+ };
|
|
306
|
+ JObject jo = ZHttpUtil.UploadFile(
|
|
307
|
+ uploadUrl,
|
|
308
|
+ byteArray,
|
|
309
|
+ fileName,
|
|
310
|
+ formFields
|
|
311
|
+ );
|
|
312
|
+ //0成功,1失败
|
|
313
|
+ if (jo["code"]?.ToString() != "0")
|
351
|
314
|
{
|
|
315
|
+ errMessage = jo["msg"]?.ToString();
|
352
|
316
|
return false;
|
353
|
317
|
}
|
|
318
|
+ videoInfo.Uploaded++;
|
|
319
|
+ }
|
|
320
|
+ while (len + videoInfo.SliceLen < filelen);
|
354
|
321
|
|
355
|
|
- #endregion 合并文件
|
|
322
|
+ #region 合并文件
|
|
323
|
+
|
|
324
|
+ //合并文件
|
|
325
|
+ bool isres = ReportFileMerge(
|
|
326
|
+ App.UserInfo.cloudcode + "/" + App.UserInfo.Schoolid + "/resource",
|
|
327
|
+ videoInfo.FileGuid,
|
|
328
|
+ out errMessage
|
|
329
|
+ );
|
|
330
|
+ if (isres)
|
|
331
|
+ {
|
|
332
|
+ return true;
|
356
|
333
|
}
|
|
334
|
+ return false;
|
|
335
|
+
|
|
336
|
+ #endregion 合并文件
|
357
|
337
|
}
|
358
|
338
|
}
|
359
|
339
|
catch (Exception ex)
|
|
@@ -363,4 +343,4 @@ namespace XHWK.WKTool.DAL
|
363
|
343
|
return false;
|
364
|
344
|
}
|
365
|
345
|
}
|
366
|
|
-}
|
|
346
|
+}
|