Example #1
0
        public static IUploadTask NewTask(string filePath, string token, string uploadAddress)
        {
            FileInfo fileInfo = new FileInfo(filePath);

            if (fileInfo.Length < SliceUploadWorker.BLOCKSIZE)
            {
                SimpleUploadTask task = new SimpleUploadTask(filePath, token, uploadAddress);
                simpleUploadWorker.AddTask(task);
                return(task);
            }
            else
            {
                SliceUploadTask task = new SliceUploadTask(filePath, token, uploadAddress);
                sliceUploadWorker.AddTask(task);
                return(task);
            }
        }
 private IEnumerable <JobInformation> GetJobInformation()
 {
     if (currentTask != null && File.Exists(currentTask.FilePath))
     {
         SliceUploadTask task         = currentTask;
         FileStream      fileStream   = new FileStream(task.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
         BinaryReader    binaryReader = new BinaryReader(fileStream);
         try
         {
             long blockIndex = 0;
             do
             {
                 byte[] data = binaryReader.ReadBytes(BLOCKSIZE);
                 if ((task.TotalContents[blockIndex] == null || task.TotalContents[blockIndex] == "") && task.UploadTaskStatus == UploadTaskStatus.Active)
                 {
                     yield return(new JobInformation
                     {
                         BlockIndex = blockIndex,
                         Data = data,
                         UploadTask = task
                     });
                 }
                 else if (task.UploadTaskStatus != UploadTaskStatus.Active)
                 {
                     yield break;
                 }
                 blockIndex++;
             } while (blockIndex < task.TotalBlockCount);
         }
         finally
         {
             binaryReader.Close();
             fileStream.Close();
         }
     }
     else
     {
         yield break;
     }
     yield break;
 }
        /// <summary>
        /// 从等待队列拉取一个激活态的任务
        /// </summary>
        private void PullTask()
        {
            bool isSuccess        = false;
            int  waitingTaskCount = 0;

            do
            {
                isSuccess = workbook.TryDequeue(out SliceUploadTask nextUploadTask);
                if (isSuccess)
                {
                    switch (nextUploadTask.UploadTaskStatus)
                    {
                    //丢弃和完成的任务移出队列
                    case UploadTaskStatus.Abort:
                    case UploadTaskStatus.Completed:
                        isSuccess = false;
                        break;

                    //暂停和失败任务重新移入队列尾
                    case UploadTaskStatus.Error:
                    case UploadTaskStatus.Pause:
                        isSuccess = false;
                        waitingTaskCount++;
                        workbook.Enqueue(nextUploadTask);
                        break;

                    case UploadTaskStatus.Active:
                        currentTask = nextUploadTask;
                        break;
                    }
                }
            } while (!isSuccess && workbook.Count >= waitingTaskCount);
            if (currentTask.UploadTaskStatus == UploadTaskStatus.Completed || currentTask.UploadTaskStatus == UploadTaskStatus.Abort || currentTask.UploadTaskStatus == UploadTaskStatus.Error)
            {
                currentTask = null;
            }
        }
 public void AddTask(SliceUploadTask uploadTask)
 {
     uploadTask.UploadTaskStatus = UploadTaskStatus.Active;
     workbook.Enqueue(uploadTask);
 }
        private void CarryOn()
        {
            foreach (JobInformation jobs in GetJobInformation())
            {
                try
                {
                    if (jobs.BlockIndex == 0)
                    {
                        jobs.UploadTask.TotalContents[jobs.BlockIndex] = UploadFirstBlock(jobs.Data, jobs.BlockIndex, jobs.UploadTask.Token, jobs.UploadTask.Address, jobs.UploadTask.UploadBatch, Path.GetFileName(jobs.UploadTask.FilePath));
                        jobs.UploadTask.CompletedBlockCount++;
#if DEBUG
                        Console.WriteLine("成功上传首个片");
#endif
                    }
                    else
                    {
                        jobs.UploadTask.TotalContents[jobs.BlockIndex] = UploadBlock(jobs.Data, jobs.BlockIndex, jobs.UploadTask.Token, jobs.UploadTask.Address, jobs.UploadTask.UploadBatch, Path.GetFileName(jobs.UploadTask.FilePath));
                        jobs.UploadTask.CompletedBlockCount++;
#if DEBUG
                        Console.WriteLine($"成功上传第{jobs.BlockIndex}个片");
#endif
                    }
                }
                catch (Exception)
                {
                    jobs.UploadTask.UploadTaskStatus = UploadTaskStatus.Pause;
                }
            }
            SliceUploadTask task = currentTask;

            if (task.CompletedBlockCount == task.TotalBlockCount)
            {
                foreach (string content in task.TotalContents)
                {
                    if (content == null || content == "")
                    {
                        return;
                    }
                }
                HttpResult result = MakeFile(new FileInfo(task.FilePath).Length, Path.GetFileName(task.FilePath), task.TotalContents, task.Token, task.Address, task.UploadBatch);

#if DEBUG
                Console.WriteLine($"成功合成文件{task.FilePath}");
#endif
                JObject jo = JObject.Parse(result.Text);
                if (jo["hash"].ToString() != ETag.ComputeEtag(task.FilePath))
                {
                    task.UploadTaskStatus = UploadTaskStatus.Error;
#if DEBUG
                    Console.WriteLine("上传校验失败");
#endif
                }
                else
                {
                    task.Hash             = jo["hash"].ToString();
                    task.UploadTaskStatus = UploadTaskStatus.Completed;
                }
                return;
            }

            if (task.CompletedBlockCount >= task.TotalBlockCount)
            {
                task.UploadTaskStatus = UploadTaskStatus.Error;
            }
        }