/// <summary> /// 获得已初始化的文件信息。 /// </summary> /// <param name="request"></param> private FileInfo GetServerFileInfo(ServerFileListRequest request) { var serverFileList = List(request); if (serverFileList.Count == 1) { return serverFileList[0]; } else if (serverFileList.Count == 0) { return null; } else { throw new PispowerAPIException("count error:There are " + serverFileList.Count + " in server."); } }
/// <summary> /// 列出服务器上已初始化的文件信息 /// </summary> /// <param name="request"></param> /// <returns>服务器上已初始化的文件信息</returns> public List<FileInfo> List(ServerFileListRequest request) { var qs = new QueryString(); if (null != request.FileNameLike && !"".Equals(request.FileNameLike)) { qs.Add("fileNameLike", request.FileNameLike); } if (null != request.FileMD5Equal && !"".Equals(request.FileMD5Equal)) { qs.Add("fileMD5Equal", request.FileMD5Equal); } var list = client.HttpGet("/video/multipartUpload/list.api", qs); var jo = JObject.Parse(list); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var serverFileList = new List<FileInfo>(); var multipartUploads = jo["multipartUploads"]; foreach (var jObj in multipartUploads) { var fileInfo = new FileInfo(); fileInfo.FileMD5 = jObj["fileMD5"].ToString(); fileInfo.FileName = jObj["fileName"].ToString(); fileInfo.UploadId = jObj["uploadId"].ToString(); serverFileList.Add(fileInfo); } return serverFileList; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 断点续传,查询已经上传的分片,与待上传文件分片MD5比较,记录未上传的分片,上传未上传的分片,最后合并分片。 /// </summary> /// <param name="fileUploadRequest"></param> /// <returns> /// Video 包含id,name,size,type,status,duration,catalogId,catalogName,VideoEmbedCode列表 。 ///</returns> public Video Upload(FileUploadRequest fileUploadRequest) { var file = fileUploadRequest.FileInfo; var partSize = fileUploadRequest.PartSize; //对文件分片 var files = SplitFile(file, partSize); //本地文件分片列表 var localParts = GetLocalParts(files); //文件MD5 var fileMD5 = MD5Utility.Compute(file).ToLower(); var fileListRequest = new ServerFileListRequest(); fileListRequest.FileMD5Equal = fileMD5; fileListRequest.FileNameLike = file.Name; //获取服务器上已初始化过的文件 var serverFileInfo = GetServerFileInfo(fileListRequest); //获得服务器上的分片 var serverParts = GetServerPartInfos(serverFileInfo); //区分上传完成的分片,未上传的分片,错误的分片 var multipartFileparts = getMultipartFileParts(localParts, serverParts); //删除服务器上出错的分片 DeleteParts(multipartFileparts.ErrorParts); // 已经上传完成的分片记录到一个列表中备用 var finishedParts = multipartFileparts.FinishedParts; //获取uploadId var uploadId = GetUploadId(file.Name, fileMD5, serverFileInfo); // 上传未完成的分片 var uploadedParts = UploadParts(uploadId, multipartFileparts.UnfinishedParts); // 将新上传的数据和已经在服务器的数据进行合并。 finishedParts.AddRange(uploadedParts); var CatalogId = ""; if (fileUploadRequest.CatalogId != 0) { CatalogId = fileUploadRequest.CatalogId.ToString(); } var video = CompleteParts(uploadId, finishedParts, CatalogId); //清除临时文件 Delete(files[0].FullName); return video; }