/// <summary> /// 获取服务器上文件分片的信息 /// </summary> /// <param name="uploadId">当前上传事件的唯一标识</param> /// <returns>服务器上的分片信息列表</returns> public List<FilePart> GetParts(String uploadId) { var qs = new QueryString(); qs.Add("uploadId", uploadId); var getParts = client.HttpGet("/video/multipartUpload/getParts.api", qs); var jo = JObject.Parse(getParts); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var serverParts = new List<FilePart>(); foreach (var jObj in jo["uploadedParts"]) { var filePart = new FilePart(); filePart.PartKey = jObj["partKey"].ToString(); filePart.PartMD5 = jObj["partMD5"].ToString(); filePart.PartNum = System.Int32.Parse(jObj["partNumber"].ToString()); serverParts.Add(filePart); } return serverParts; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 删除一个视频 /// </summary> /// <param name="request">Video的唯一标识</param> public void Delete(Int32 videoId) { var qs = new QueryString(); qs.Add("videoId", videoId.ToString()); var deleteVideo = client.HttpPost("/video/delete.api", qs); var jo = JObject.Parse(deleteVideo); if (jo["statusCode"].ToString().CompareTo("0") != 0) { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
public string HttpUpload(string apiContext, QueryString queryString, FileStream fileStream) { var finalParams = AddAdditionParams(queryString).GetEncodedString(); var url = GetUrl(apiContext) + "?" + finalParams; var request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = true; request.Headers["accept-encoding"] = "gzip,deflate"; request.UserAgent = "onecloud-video-dot-net-client"; var boundary = MD5Utility.Compute(DateTime.Now.Ticks.ToString("X") + "=="); request.ContentType = "multipart/form-data; boundary=" + boundary; var multipartFileHeader = Encoding.UTF8.GetBytes("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"uploadFile\"; filename=\"" + Path.GetFileName(fileStream.Name) + "\"\r\n" + "Content-Type: application/octet-stream\r\n" + "Content-Transfer-Encoding:binary\r\n\r\n"); var endBoundary = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); request.ContentLength = multipartFileHeader.Length + fileStream.Length + endBoundary.Length; using (var stream = request.GetRequestStream()) { // write header stream.Write(multipartFileHeader, 0, multipartFileHeader.Length); // write file var buff = new byte[10240]; var byteRead = 0; var byteWrite = 0; while ((byteRead = fileStream.Read(buff, 0, buff.Length)) != 0) { byteWrite += byteRead; stream.Write(buff, 0, byteRead); } // write end boundary stream.Write(endBoundary, 0, endBoundary.Length); } using (var reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
/// <summary> /// 中止一个上传进程 /// </summary> /// <param name="uploadId">当前上传事件的唯一标识</param> /// <returns>上传文件的名字</returns> public String Abort(String uploadId) { var qs = new QueryString(); qs.Add("uploadId", uploadId); var abortParts = client.HttpPost("/video/getParts.api", qs); var jo = JObject.Parse(abortParts); if (jo["statusCode"].ToString().CompareTo("0") == 0) { return jo["fileName"].ToString(); } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 根据id查询视频 /// </summary> /// <param name="videoId">Video的唯一标识</param> /// <returns> /// Video 包含id,name,size,type,status,duration,catalogId,catalogName,VideoEmbedCode列表。 /// </returns> public Video Get(Int32 videoId) { var qs = new QueryString(); qs.Add("videoId", videoId.ToString()); var getVideo = client.HttpGet("/video/get.api", qs); var jo = JObject.Parse(getVideo); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var video = new Video(); video.CatalogId = System.Int32.Parse(jo["catalogId"].ToString()); video.CatalogName = jo["catalogName"].ToString(); video.Name = jo["name"].ToString(); video.Size = long.Parse(jo["size"].ToString()); video.Status = jo["status"].ToString(); video.Type = jo["type"].ToString(); video.Duration = long.Parse(jo["duration"].ToString()); var embedCodes = new List<VideoEmbedCode>(); foreach (var jObj in jo["embedCodes"]) { var embedCode = new VideoEmbedCode(); embedCode.AutoAdaptionCode = jObj["autoAdaptionCode"].ToString(); embedCode.Clarity = jObj["clarity"].ToString(); embedCode.FilePath = jObj["filePath"].ToString(); embedCode.FlashCode = jObj["flashCode"].ToString(); embedCode.Html5Code = jObj["html5Code"].ToString(); embedCode.PortableCode = jObj["portableCode"].ToString(); embedCode.Resolution = jObj["resolution"].ToString(); embedCodes.Add(embedCode); } video.EmbedCodes = embedCodes; return video; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 上传一个分片 /// </summary> /// <param name="uploadId">当前上传事件的唯一标识</param> /// <param name="partNumber">分片num编号</param> /// <param name="part">分片文件</param> private FilePart UploadPart(String uploadId, int partNumber, System.IO.FileInfo part) { QueryString qs = new QueryString(); qs.Add("uploadId", uploadId); qs.Add("partNumber", partNumber.ToString()); using (var fs = File.OpenRead(part.FullName)) { var uploadPart = client.HttpUpload("/video/multipartUpload/uploadPart.api", qs, fs); var jo = JObject.Parse(uploadPart); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var filePart = new FilePart(); filePart.PartNum = partNumber; filePart.PartMD5 = jo["partMD5"].ToString(); filePart.PartKey = jo["partKey"].ToString(); return filePart; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } } }
/// <summary> /// 对待上传文件进行初始化 /// </summary> /// <param name="fileName">文件的名字</param> /// <param name="fileMD5">文件的MD5</param> private String Init(String fileName, String fileMD5) { QueryString qs = new QueryString(); qs.Add("fileName", fileName); qs.Add("fileMD5", fileMD5); var initUpload = client.HttpPost("/video/multipartUpload/init.api", qs); var jo = JObject.Parse(initUpload); if (jo["statusCode"].ToString().CompareTo("0") == 0) { return jo["uploadId"].ToString(); } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 删除服务器上错误的分片 /// </summary> /// <param name="errorParts">服务器上错误的分片列表</param> private void DeleteParts(List<FilePart> errorParts) { if (errorParts.Count == 0) { return; } var qs = new QueryString(); foreach (var filePart in errorParts) { qs.Add("partKeys", filePart.PartKey); } var deleteParts = client.HttpPost("/video/multipartUpload/deleteParts.api", qs); var jo = JObject.Parse(deleteParts); if (jo["statusCode"].ToString().CompareTo("0") != 0) { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 合并分片 /// </summary> /// <param name="uploadId">当前上传事件的唯一标识</param> /// <param name="fileParts"></param> /// <param name="catalogId">上传到该catalogId的Catalog中</param> private Video CompleteParts(String uploadId, List<FilePart> fileParts, String catalogId) { var qs = new QueryString(); qs.Add("uploadId", uploadId); qs.Add("catalogId", catalogId); foreach (var filePart in fileParts) { var partNum = filePart.PartNum; var partKey = filePart.PartKey; qs.Add("part" + partNum, partKey); } var completeParts = client.HttpPost("/video/multipartUpload/complete.api", qs); var jo = JObject.Parse(completeParts); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var video = new Video(); video.CatalogId = System.Int32.Parse(jo["catalogId"].ToString()); video.CatalogName = jo["catalogName"].ToString(); video.Name = jo["name"].ToString(); video.Size = long.Parse(jo["size"].ToString()); video.Status = jo["status"].ToString(); video.Type = jo["type"].ToString(); video.Duration = long.Parse(jo["duration"].ToString()); var embedCodes = new List<VideoEmbedCode>(); foreach (var jObj in jo["embedCodes"]) { var embedCode = new VideoEmbedCode(); embedCode.AutoAdaptionCode = jObj["autoAdaptionCode"].ToString(); embedCode.Clarity = jObj["clarity"].ToString(); embedCode.FilePath = jObj["filePath"].ToString(); embedCode.FlashCode = jObj["flashCode"].ToString(); embedCode.Html5Code = jObj["html5Code"].ToString(); embedCode.PortableCode = jObj["portableCode"].ToString(); embedCode.Resolution = jObj["resolution"].ToString(); embedCodes.Add(embedCode); } video.EmbedCodes = embedCodes; return video; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
private string Sign(QueryString queryString) { var sign = _appSecret; foreach (var item in queryString.GetSortedDictionary()) { foreach (var val in item.Value) { sign += item.Key + val; } } sign += _appSecret; return MD5Utility.Compute(sign); }
/// <summary> /// 上传一个视频 /// </summary> /// <param name="request"></param> public Video Upload(VideoUploadRequest request) { var qs = new QueryString(); qs.Add("name", request.Name); if (null != request.Description && !"".Equals(request.Description)) { qs.Add("description", request.Description); } if (request.CatalogId > 0) { qs.Add("catalogId", request.CatalogId.ToString()); } using (var fs = File.OpenRead(request.FileInfo.FullName)) { var uploadVideo = client.HttpUpload("/video/upload.api", qs, fs); var jo = JObject.Parse(uploadVideo); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var video = new Video(); video.CatalogId = System.Int32.Parse(jo["catalogId"].ToString()); video.CatalogName = jo["catalogName"].ToString(); video.Name = jo["name"].ToString(); video.Size = long.Parse(jo["size"].ToString()); video.Status = jo["status"].ToString(); video.Type = jo["type"].ToString(); video.Duration = long.Parse(jo["duration"].ToString()); var embedCodes = new List<VideoEmbedCode>(); foreach (var jObj in jo["embedCodes"]) { var embedCode = new VideoEmbedCode(); embedCode.AutoAdaptionCode = jObj["autoAdaptionCode"].ToString(); embedCode.Clarity = jObj["clarity"].ToString(); embedCode.FilePath = jObj["filePath"].ToString(); embedCode.FlashCode = jObj["flashCode"].ToString(); embedCode.Html5Code = jObj["html5Code"].ToString(); embedCode.PortableCode = jObj["portableCode"].ToString(); embedCode.Resolution = jObj["resolution"].ToString(); embedCodes.Add(embedCode); } video.EmbedCodes = embedCodes; return video; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } } }
/// <summary> /// 更新视频的名字或描述信息 /// </summary> /// <param name="request"></param> public void Update(VideoUpdateRequest request) { var qs = new QueryString(); qs.Add("videoId", request.VideoId.ToString()); qs.Add("name", request.Name); if (null != request.Description && !"".Equals(request.Description)) { qs.Add("description", request.Description); } var updateVideo = client.HttpPost("/video/update.api", qs); var jo = JObject.Parse(updateVideo); if (jo["statusCode"].ToString().CompareTo("0") != 0) { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
/// <summary> /// 列出视频 /// </summary> /// <param name="request"></param> /// <returns> /// Video列表 每个Video对象包含id,name,size,type,status,duration,catalogId,catalogName。 ///</returns> public List<Video> List(VideoListRequest request) { var qs = new QueryString(); if (null != request.NameLike && !"".Equals(request.NameLike)) { qs.Add("nameLike", request.NameLike); } if (null != request.CatalogNameLike && !"".Equals(request.CatalogNameLike)) { qs.Add("catalogNameLike", request.CatalogNameLike); } if (request.CatalogId > 0) { qs.Add("catalogId", request.CatalogId.ToString()); } if (request.Page > 0) { qs.Add("page", request.Page.ToString()); } if (request.MaxResult > 0) { qs.Add("maxResult", request.MaxResult.ToString()); } var listVideo = client.HttpGet("/video/list.api", qs); var jo = JObject.Parse(listVideo); if (jo["statusCode"].ToString().CompareTo("0") == 0) { var videos = jo["videos"]; var list = new List<Video>(); foreach(var jObject in videos) { var video = new Video(); video.Id = System.Int32.Parse(jObject["id"].ToString()); video.CatalogId = System.Int32.Parse(jObject["catalogId"].ToString()); video.CatalogName = jObject["catalogName"].ToString(); video.Name = jObject["name"].ToString(); video.Size = long.Parse(jObject["size"].ToString()); video.Status = jObject["status"].ToString(); video.Type = jObject["type"].ToString(); video.Duration = long.Parse(jObject["duration"].ToString()); list.Add(video); } return list; } else { throw new PispowerAPIException(System.Int32.Parse(jo["statusCode"].ToString()), jo["message"].ToString()); } }
private QueryString AddAdditionParams(QueryString queryStr) { var qur = queryStr.Clone(); qur.Add("accessKey",_appKey); qur.Add("time", Convert.ToInt64((DateTime.UtcNow - DateTime.Parse("1970-1-1")).TotalMilliseconds) + ""); qur.Add("sign", Sign(qur)); return qur; }
public string HttpPost(string apiContext, QueryString queryString) { return Connect(apiContext, queryString, "POST"); }
public string HttpGet(string apiContext, QueryString queryString) { return Connect(apiContext, queryString, "GET"); }
/// <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()); } }
private string Connect(string apiContext, QueryString queryString, string method) { var finalParams = AddAdditionParams(queryString).GetEncodedString(); var url = GetUrl(apiContext) + "?" + finalParams; var request = WebRequest.Create(url) as HttpWebRequest; request.Method = method; request.KeepAlive = true; request.ContentType = "application/x-www-form-urlencoded"; var response = request.GetResponse() as HttpWebResponse; var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var result = reader.ReadToEnd(); reader.Close(); return result; }