public void UniversalToLocalTransition(string root, string relative) { var folderRoot = new LocalFolderRoot(root); var localPath = new LocalPath(relative, folderRoot); var universalPath = localPath.ToUniversalPath(); var result = LocalPath.CreateFromUniversal(universalPath, folderRoot); universalPath.Value.Should().Be("/hello1/file"); result.Should().Be(localPath); }
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); } } } } } }