Example #1
0
        /// <summary>
        /// 文件续传
        /// </summary>
        /// <param name="filePath">上传文件路径</param>
        /// <param name="initParamMap">上传文件的相关信息集合</param>
        /// <param name="recorder">断点续传时记录上传进度的对象</param>
        /// <returns>上传完成则返回查询视频ID返回结果的封装类,否则抛出文件未传输完成异常</returns>
        public QueryVideoIDorWatermarkIDParam uploadVideoAgainWithRecorder(string filePath, IDictionary <String, Object> initParamMap, UploadRecorder recorder)
        {
            string[] savedInfo = recorder.getRecorderInfo();

            string bucket        = savedInfo[1];
            string uploadHost    = savedInfo[2];
            string objectName    = savedInfo[3];
            long   offset        = long.Parse(savedInfo[4]);
            string context       = savedInfo[5];
            long   remainderSize = long.Parse(savedInfo[6]);
            string xNosToken     = savedInfo[7];

            /*分片上传视频*/
            FileStream fileStream = null;

            try
            {
                fileStream = FileUtil.getFileInputStream(filePath);
                fileStream.Seek(offset, SeekOrigin.Begin);
                /*上传文件剩余大小*/
                long fileLength = FileUtil.getFileLength(filePath);
                /*分片上传视频*/
                while (remainderSize > 0)
                {
                    UploadVideoFragmentParam uploadVideoParam = uploadVideoFragment(bucket, uploadHost, objectName, offset, context, fileStream, remainderSize, xNosToken);
                    if (null == uploadVideoParam.errMsg || "".Equals(uploadVideoParam.errMsg.Trim()))
                    {
                        context       = uploadVideoParam.context;
                        offset        = uploadVideoParam.offset;
                        remainderSize = fileLength - offset;

                        // string bucket = initUploadVideoParam.ret.bucket;
                        // string uploadHost = getUploadHostParam.upload[0];
                        // string objectName = initUploadVideoParam.ret.objectName;
                        string offsetStr = Convert.ToString(offset);
                        // string context = saveInfo[4];
                        string remainderSizeStr = Convert.ToString(remainderSize);
                        // string xNosToken = initUploadVideoParam.ret.xNosToken;
                        string[] saveInfo = new string[] { bucket, uploadHost, objectName, offsetStr, context, remainderSizeStr, xNosToken };
                        recorder.saveRecorderInfo(saveInfo);
                    }
                    else
                    {
                        throw new VcloudException("upload videoFragment fail. errMsg: " + uploadVideoParam.errMsg + " requestID: " + uploadVideoParam.requestId);
                    }
                }
                if (remainderSize == 0)
                {
                    recorder.deleteRecorder();
                    /* 查询上传视频的vid*/
                    List <string> objectNamesList = new List <string>();
                    objectNamesList.Add(objectName);

                    /*查询上传视屏返回结果的封装类*/
                    QueryVideoIDorWatermarkIDParam queryVideoIDParam = queryVideoID(objectNamesList);
                    return(queryVideoIDParam);
                }
                else
                {
                    throw new VcloudException("文件未传输完成");
                }
            }
            catch (Exception e)
            {
                throw new VcloudException(e.Message);
            }
        }
Example #2
0
        /// <summary>
        /// 文件第一次上传
        /// </summary>
        /// <param name="filePath">上传文件路径</param>
        /// <param name="initParamMap">上传文件的相关信息集合</param>
        /// <param name="recorder">断点续传时记录上传进度的对象</param>
        /// <returns>上传完成则返回查询视频ID返回结果的封装类,否则抛出文件未传输完成异常</returns>
        public QueryVideoIDorWatermarkIDParam uploadVideoFirstWithRecorder(string filePath, IDictionary <String, Object> initParamMap, UploadRecorder recorder)
        {
            /*视频上传初始化*/
            /*视频上传初始化返回结果的封装类*/
            InitUploadVideoParam initUploadVideoParam = initUploadVideo(initParamMap);

            if (initUploadVideoParam.code != 200)
            {
                throw new VcloudException("[VcloudClient]上传初始化失败 return code : " + initUploadVideoParam.code + " return message: " + initUploadVideoParam.msg);
            }
            /*获取上传加速节点地址*/
            /*获取上传加速节点地址返回结果的封装类*/
            GetUploadHostParam getUploadHostParam = getUploadHost(initUploadVideoParam);

            if (null == getUploadHostParam || null == getUploadHostParam.upload || getUploadHostParam.upload.Count == 0)
            {
                throw new VcloudException("[VcloudClient]获取上传加速节点地址失败 return code: " + getUploadHostParam.code + " return message: " + getUploadHostParam.Message);
            }
            /*分片上传视频*/
            /*当前分片在整个对象中的起始偏移量 */
            long offset = 0;
            /*上传上下文*/
            string context = null;
            /*上传文件的输出流 */
            FileStream fileStream = null;

            //int count = 0;
            try
            {
                fileStream = FileUtil.getFileInputStream(filePath);
                /*上传文件剩余大小*/
                long fileLength    = FileUtil.getFileLength(filePath);
                long remainderSize = fileLength;
                /*分片上传视频*/
                while (remainderSize > 0)
                {
                    UploadVideoFragmentParam uploadVideoParam = uploadVideoFragment(initUploadVideoParam, getUploadHostParam, offset, context, fileStream, remainderSize);
                    if (null == uploadVideoParam.errMsg || "".Equals(uploadVideoParam.errMsg.Trim()))
                    {
                        context       = uploadVideoParam.context;
                        offset        = uploadVideoParam.offset;
                        remainderSize = fileLength - offset;

                        string bucket     = initUploadVideoParam.ret.bucket;
                        string uploadHost = getUploadHostParam.upload[0];
                        string objectName = initUploadVideoParam.ret.objectName;
                        string offsetStr  = Convert.ToString(offset);
                        // string context = saveInfo[4];
                        string   remainderSizeStr = Convert.ToString(remainderSize);
                        string   xNosToken        = initUploadVideoParam.ret.xNosToken;
                        string[] saveInfo         = new string[] { bucket, uploadHost, objectName, offsetStr, context, remainderSizeStr, xNosToken };
                        recorder.saveRecorderInfo(saveInfo);

                        /*
                         * count++;
                         * if (count == 1)
                         * {
                         *  Console.WriteLine("******************************第一次强制中断************");
                         *  break;
                         * }*/
                    }
                    else
                    {
                        throw new VcloudException("upload videoFragment fail. errMsg: " + uploadVideoParam.errMsg + " requestID: " + uploadVideoParam.requestId);
                    }
                }
                if (remainderSize == 0)
                {
                    recorder.deleteRecorder();
                    /* 查询上传视频的vid*/
                    List <string> objectNamesList = new List <string>();
                    objectNamesList.Add(initUploadVideoParam.ret.objectName);

                    /*查询上传视屏返回结果的封装类*/
                    QueryVideoIDorWatermarkIDParam queryVideoIDParam = queryVideoID(objectNamesList);
                    return(queryVideoIDParam);
                }
                else
                {
                    throw new VcloudException("文件未传输完成");
                    //return null;
                }
            }
            catch (Exception e)
            {
                throw new VcloudException(e.Message);
            }
        }