/// <summary>
        ///
        /// </summary>
        /// <param name="method"></param>
        /// <param name="remoteFilePath"></param>
        /// <param name="localPath"></param>
        /// <returns></returns>
        public bool DownloadFile(DownloadMethod method, string remoteFilePath, string localPath)
        {
            try
            {
                remoteFilePath = WebHelper.RemoveTrailingBackslash(remoteFilePath);

                var localFileFullPath = FileHelper.AddFileExtension(FileHelper.GenerateLocalPathToFile(remoteFilePath, localPath), "jpg");
                switch (method)
                {
                case DownloadMethod.WebClientAsync:
                    return(DownloadFileWebClientAsync(remoteFilePath, localFileFullPath));

                case DownloadMethod.RestSharp:
                    return(DownloadFileRestSharp(remoteFilePath, localFileFullPath));

                case DownloadMethod.WebClient:
                    return(DownloadFileWebClient(remoteFilePath, localFileFullPath));

                default:
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }
示例#2
0
        public override void PieceConnected(string pieceId, ProbeResult?result)
        {
            if (this.cancelFlag.IsCancellationRequested)
            {
                return;
            }
            try
            {
                rwLock.EnterWriteLock();
                var piece = this.pieces[pieceId];
                if (result != null) //probe result is not null only for first request of each stream type, for subsequent requests its always null
                {
                    state.LastModified = result.LastModified;
                    piece.Length       = result.ResourceSize ?? -1;
                    piece.Offset       = 0;
                    piece.Downloaded   = 0;
                    piece.State        = SegmentState.NotStarted;
                    if (piece.Length > 0)
                    {
                        totalSize += piece.Length;
                    }

                    Log.Debug("fileNameFetchMode: " + fileNameFetchMode);

                    switch (fileNameFetchMode)
                    {
                    case FileNameFetchMode.FileNameAndExtension:
                        if (result.AttachmentName != null)
                        {
                            this.TargetFileName = FileHelper.SanitizeFileName(result.AttachmentName);
                        }
                        else
                        {
                            this.TargetFileName = FileHelper.SanitizeFileName(FileHelper.GetFileName(
                                                                                  result.FinalUri, result.ContentType));
                        }
                        break;

                    case FileNameFetchMode.ExtensionOnly:
                        if (state.Init1 || state.Init2)
                        {
                            FileHelper.AddFileExtension(this.TargetFileName, result.ContentType, out string name);
                            var ext1 = Path.GetExtension(this.TargetFileName);
                            var ext2 = Path.GetExtension(name);
                            if (ext1 == ".mkv" || ext2 == ".mkv")
                            {
                                this.TargetFileName = Path.GetFileNameWithoutExtension(name) + ".mkv";
                            }
                        }
                        else
                        {
                            var name = string.Empty;
                            if (FileHelper.AddFileExtension(this.TargetFileName, result.ContentType, out name))
                            {
                                this.TargetFileName = name;
                            }
                        }
                        break;
                    }

                    if (piece.StreamType == StreamType.Primary)
                    {
                        Log.Debug("Primary initiated - length: " + piece.Length);
                        state.Init1 = true;
                    }
                    else
                    {
                        Log.Debug("Secondary initiated - length: " + piece.Length);
                        state.Init2 = true;
                    }

                    if (piece.StreamType == StreamType.Primary)
                    {
                        state.Url1 = result.FinalUri;
                    }
                    else
                    {
                        state.Url2 = result.FinalUri;
                    }

                    if (state.Init1 && state.Init2)
                    {
                        state.FileSize = totalSize;
                    }

                    SaveState();
                    SaveChunkState();

                    if (state.Init1 && state.Init2)
                    {
                        base.OnProbed();
                    }

                    if (state.Init1 && state.Init2 &&
                        totalSize > 0 &&
                        Helpers.GetFreeSpace(this.state.TempDir, out long freespace) &&
                        freespace < totalSize)
                    {
                        throw new AssembleFailedException(ErrorCode.DiskError);
                    }
                }
            }
            finally
            {
                rwLock.ExitWriteLock();
            }

            CreatePiece();
        }
示例#3
0
        public override void PieceConnected(string pieceId, ProbeResult?result)
        {
            if (this.cancelFlag.IsCancellationRequested)
            {
                return;
            }
            try
            {
                rwLock.EnterWriteLock();
                if (result != null) //probe result is not null for first request only, for subsequent requests its always null
                {
                    Log.Debug("connected: " + result.ResourceSize + " init...");
                    state.LastModified = result.LastModified;
                    this.totalSize     = result.ResourceSize ?? -1;
                    this.resumable     = result.Resumable;
                    var piece = this.pieces[pieceId];
                    piece.Length     = result.ResourceSize ?? -1;
                    piece.Offset     = 0;
                    piece.Downloaded = 0;
                    piece.State      = SegmentState.NotStarted;
                    this.init        = true;

                    Log.Debug("fileNameFetchMode: " + fileNameFetchMode);
                    Log.Debug("Attachment: " + result.AttachmentName);
                    Log.Debug("ContentType: " + result.ContentType);
                    switch (fileNameFetchMode)
                    {
                    case FileNameFetchMode.FileNameAndExtension:
                        if (result.AttachmentName != null)
                        {
                            this.TargetFileName = FileHelper.SanitizeFileName(result.AttachmentName);
                        }
                        else
                        {
                            this.TargetFileName = FileHelper.GetFileName(
                                result.FinalUri, result.ContentType);
                        }
                        break;

                    case FileNameFetchMode.ExtensionOnly:
                        var name = string.Empty;
                        if (FileHelper.AddFileExtension(this.TargetFileName, result.ContentType, out name))
                        {
                            this.TargetFileName = name;
                        }
                        break;
                    }

                    state.Url      = result.FinalUri;
                    state.FileSize = result.ResourceSize ?? -1;
                    SaveState();
                    SaveChunkState();
                    base.OnProbed();

                    //check if required disk space is available
                    if (result.ResourceSize.HasValue &&
                        result.ResourceSize.Value > 0 &&
                        Helpers.GetFreeSpace(this.state.TempDir, out long freespace) &&
                        freespace < result.ResourceSize.Value)
                    {
                        throw new AssembleFailedException(ErrorCode.DiskError);
                    }
                }
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
            CreatePiece();
        }