コード例 #1
0
        private async Task ExecuteInternal()
        {
            var targetPath = this.target.Value;
            var sourcePath = this.source.Value;

            if (File.Exists(targetPath))
            {
                if (File.GetLastWriteTimeUtc(sourcePath) == File.GetLastWriteTimeUtc(targetPath))
                {
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? "");
            }

            using (var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read, Consts.BufferSize, FileOptions.Asynchronous))
                using (var targetStream = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite, Consts.BufferSize, FileOptions.Asynchronous))
                    using (var decryptor = new CipherStreamDecryptor(sourceStream))
                    {
                        await decryptor.Init(
                            this.personalCard.GetRecepientId(),
                            this.personalCard.PrivateKey,
                            this.password);

                        while (decryptor.HasMore())
                        {
                            await targetStream.WriteAsync(await decryptor.GetChunk());
                        }
                    }

            File.SetLastWriteTimeUtc(targetPath, File.GetLastWriteTimeUtc(sourcePath));
        }
コード例 #2
0
        public override async Task Execute()
        {
            if (File.Exists(this.target))
            {
                if (File.GetLastWriteTimeUtc(this.source) == File.GetLastWriteTimeUtc(this.target))
                {
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(Path.GetDirectoryName(this.target) ?? "");
            }

            using (var sourceStream = new FileStream(this.source, FileMode.Open, FileAccess.Read, FileShare.Read, Consts.BufferSize, FileOptions.Asynchronous))
                using (var targetStream = new FileStream(this.target, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite, Consts.BufferSize, FileOptions.Asynchronous))
                    using (var decryptor = new CipherStreamDecryptor(sourceStream))
                    {
                        await decryptor.Init(this.credentials.RecepientId, this.credentials.PrivateKey);

                        while (decryptor.HasMore())
                        {
                            await targetStream.WriteAsync(await decryptor.GetChunk());
                        }
                    }

            File.SetLastWriteTimeUtc(this.target, File.GetLastWriteTimeUtc(this.source));
        }
コード例 #3
0
        public async Task DownloadFile(ServerPath serverFileName, LocalFolderRoot root,
                                       CancellationToken token, IProgress <double> progress = null)
        {
            token.ThrowIfCancellationRequested();

            using (var download = await this.client.Files.DownloadAsync(serverFileName.Value))
            {
                var size = download.Response.Size;

                using (var stream = await download.GetContentAsStreamAsync())
                    using (var cipherStreamDecryptor = new CipherStreamDecryptor(stream))
                    {
                        await cipherStreamDecryptor.Init(this.credentials.GetRecepientId(),
                                                         this.credentials.PrivateKey.Data, this.privateKeyPassword);

                        var originalFileName = cipherStreamDecryptor.GetEncryptedValue(OriginalFileNameKey);
                        var fromServerPath   = new UniversalPath(originalFileName);

                        var localPath     = LocalPath.CreateFromUniversal(fromServerPath, root);
                        var tempLocalName = localPath.Value + VirgilTempExtension;

                        var serverWriteTimeUtc = download.Response.ClientModified;
                        var localWriteTimeUtc  = new FileInfo(localPath.Value).LastWriteTimeUtc;

                        if (serverWriteTimeUtc.AlmostEquals(localWriteTimeUtc))
                        {
                            progress?.Report(100);
                            return;
                        }

                        using (OperationExecutionContext.Instance.IgnoreChangesTo(localPath))
                        {
                            var localDir = Path.GetDirectoryName(localPath.Value);
                            if (localDir != null && !Directory.Exists(localDir))
                            {
                                Directory.CreateDirectory(localDir);
                            }

                            try
                            {
                                using (var dest = new FileStream(
                                           tempLocalName, FileMode.Create, FileAccess.ReadWrite,
                                           FileShare.ReadWrite, BufferSize,
                                           FileOptions.Asynchronous | FileOptions.SequentialScan))
                                {
                                    while (cipherStreamDecryptor.HasMore())
                                    {
                                        var chunk = await cipherStreamDecryptor.GetChunk();

                                        await dest.WriteAsync(chunk, 0, chunk.Length, token);

                                        progress?.Report(100.0 * chunk.Length / size);
                                    }

                                    await dest.FlushAsync(token);
                                }

                                //File.Delete(localPath.Value);
                                File.Copy(tempLocalName, localPath.Value, true);
                                File.SetLastWriteTimeUtc(localPath.Value, serverWriteTimeUtc);
                            }
                            finally
                            {
                                try
                                {
                                    File.Delete(tempLocalName);
                                }
                                catch (Exception exception)
                                {
                                    Console.WriteLine(exception);
                                }
                            }
                        }
                    }
            }
        }