Beispiel #1
0
        public async Task <string> Upload(string sourcePath, string targetPath, string checksum, bool resumeUpload,
                                          CancellationToken cancellationToken)
        {
            var tmpFilePath = _networkPathInfo.GetPathMetaDataFile();

            targetPath = GetFileName(sourcePath, checksum);
            DeleteRowFromMetaDataFile(checksum, tmpFilePath);
            try
            {
                if (!IsTheSameSize(sourcePath, targetPath) && !resumeUpload)
                {
                    Clean(targetPath);
                }

                if (IsTheSameSize(sourcePath, targetPath))
                {
                    ProcessingFinishedDelegate?.Invoke(true, null);
                }
                return(await FileTransfer(sourcePath, targetPath, checksum, tmpFilePath, resumeUpload, cancellationToken));
            }
            catch (Exception ex)
            {
                ProcessingFinishedDelegate?.Invoke(false, ex);
                return(string.Empty);
            }
        }
Beispiel #2
0
        public async Task Download(string sourcePath, string targetPath, string checksum, bool resumeDownload,
                                   CancellationToken cancellationToken)
        {
            string tmpFilePath    = targetPath + @"\\" + Path.GetFileName("tmpFile.txt");
            string targetFilePath = targetPath + @"\\" + Path.GetFileName(sourcePath);

            DeleteRowFromMetaDataFile(checksum, tmpFilePath);
            try
            {
                if (!resumeDownload)
                {
                    if (!IsTheSameSize(sourcePath, targetFilePath))
                    {
                        Clean(targetFilePath);
                    }
                }
                if (IsTheSameSize(sourcePath, targetFilePath) && IsTheSameFile(checksum, targetFilePath))
                {
                    ProcessingFinishedDelegate?.Invoke(true, null);
                    return;
                }
                var path = await FileTransfer(sourcePath, targetFilePath, checksum, tmpFilePath, true, cancellationToken);
            }
            catch (Exception ex)
            {
                ProcessingFinishedDelegate?.Invoke(false, ex);
            }
        }
Beispiel #3
0
        public async Task <string> ResumeUpload(string sourcePath, string targetPath, string checksum,
                                                bool resumeUpload, CancellationToken cancellationToken)
        {
            long   totalBytes  = 0;
            string tmpFilePath = _networkPathInfo.GetPathMetaDataFile();

            targetPath = targetPath + @"\\" + Path.GetFileName(sourcePath);
            targetPath = $"{targetPath}.tmp";
            try
            {
                totalBytes = GetTotalBytes(tmpFilePath, checksum, totalBytes);
                if (!IsTheSameSize(sourcePath, targetPath) && !resumeUpload)
                {
                    Clean(targetPath);
                }

                if (IsTheSameSize(sourcePath, targetPath))
                {
                    ProcessingFinishedDelegate?.Invoke(true, null);
                }

                return(await ResumeFileTransfer(sourcePath, targetPath, totalBytes, tmpFilePath, checksum, cancellationToken));
            }
            catch (Exception ex)
            {
                ProcessingFinishedDelegate?.Invoke(false, ex);
                return(string.Empty);
            }
        }
Beispiel #4
0
        public async Task ResumeDownload(string sourcePath, string targetPath, string checksum,
                                         bool resumeDownload, CancellationToken cancellationToken)
        {
            long   totalBytes  = 0;
            string tmpFilePath = targetPath + @"\\" + Path.GetFileName("tmpFile.txt");

            targetPath = targetPath + @"\\" + Path.GetFileName(sourcePath) + ".tmp";
            try
            {
                totalBytes = GetTotalBytes(tmpFilePath, checksum, totalBytes);

                if (!IsTheSameSize(sourcePath, targetPath) && !resumeDownload)
                {
                    Clean(targetPath);
                }

                if (IsTheSameSize(sourcePath, targetPath))
                {
                    ProcessingFinishedDelegate?.Invoke(true, null);
                }
                var path = await ResumeFileTransfer(sourcePath, targetPath, totalBytes, tmpFilePath, checksum, cancellationToken);
            }
            catch (Exception ex)
            {
                ProcessingFinishedDelegate?.Invoke(false, ex);
            }
        }
Beispiel #5
0
        private async Task <string> ResumeFileTransfer(string sourcePath, string targetPath, long totalBytes, string tmpFilePath, string checksum,
                                                       CancellationToken cancellationToken)
        {
            var credentials = _userAccessCredentials.ReadCredentials();

            using (var connection = _networkConnection.Connect(_networkPathInfo.GetNetworkPath(), credentials.Login, credentials.Password))
            {
                try
                {
                    _totalBytes      = totalBytes;
                    _processingSpeed = 0;
                    _sizeOfFile      = 0;
                    byte[] buffer = new byte[16384];
                    int    readBytes;
                    using (FileStream sourceStream = File.OpenRead(sourcePath))
                        using (FileStream targetStream = File.OpenWrite(targetPath))
                        {
                            File.SetAttributes(targetPath, File.GetAttributes(targetPath) | FileAttributes.Hidden);
                            if (_totalBytes != 0)
                            {
                                targetStream.Seek(_totalBytes, SeekOrigin.Begin);
                                sourceStream.Seek(_totalBytes, SeekOrigin.Begin);
                            }
                            _sizeOfFile = sourceStream.Length;
                            using (var stateTimer = new System.Threading.Timer(FormStatusUpdate, null, 1000, 1000))

                            {
                                while ((readBytes = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                                {
                                    if (cancellationToken.IsCancellationRequested)
                                    {
                                        cancellationToken.ThrowIfCancellationRequested();
                                        return(string.Empty);
                                    }
                                    await targetStream.WriteAsync(buffer, 0, readBytes);

                                    _totalBytes += readBytes;
                                }
                            }
                            DeleteRowFromMetaDataFile(checksum, tmpFilePath);
                            File.SetAttributes(targetPath, File.GetAttributes(targetPath) & ~FileAttributes.Hidden);
                            ProcessingFinishedDelegate?.Invoke(true, null);
                        }
                    string newPath = $"{ Path.GetDirectoryName(targetPath)}\\{Path.GetFileNameWithoutExtension(targetPath)}";
                    File.Move(targetPath, newPath);
                    return(targetPath);
                }
                catch (Exception ex)
                {
                    DeleteRowFromMetaDataFile(checksum, tmpFilePath);
                    SaveFileInfo(checksum, _totalBytes, tmpFilePath, Path.GetFileName(targetPath));
                    ProcessingFinishedDelegate?.Invoke(false, ex);
                }
                return(string.Empty);
            }
        }
Beispiel #6
0
 public void ProcessPhoto(Image image, IPhotoFilter filter, ProcessingFinishedDelegate callback)
 {
 }