- var account = cloudFile.Account;
- var container = cloudFile.Container;
-
- if (cloudFile.Content_Type == @"application/directory")
- {
- if (!Directory.Exists(localPath))
- Directory.CreateDirectory(localPath);
- }
- else
- {
- //Retrieve the hashmap from the server
- var serverHash = await client.GetHashMap(account, container, url);
- //If it's a small file
- if (serverHash.Hashes.Count == 1)
- //Download it in one go
- await
- DownloadEntireFileAsync(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
- //Otherwise download it block by block
- else
- await DownloadWithBlocks(accountInfo, client, cloudFile, relativeUrl, localPath, serverHash);
-
- if (cloudFile.AllowedTo == "read")
- {
- var attributes = File.GetAttributes(localPath);
- File.SetAttributes(localPath, attributes | FileAttributes.ReadOnly);
- }
- }
-
- //Now we can store the object's metadata without worrying about ghost status entries
- StatusKeeper.StoreInfo(localPath, cloudFile);
-
- }
- }
-
- //Download a small file with a single GET operation
- private async Task DownloadEntireFileAsync(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath,TreeHash serverHash)
- {
- if (client == null)
- throw new ArgumentNullException("client");
- if (cloudFile==null)
- throw new ArgumentNullException("cloudFile");
- if (relativeUrl == null)
- throw new ArgumentNullException("relativeUrl");
- if (String.IsNullOrWhiteSpace(filePath))
- throw new ArgumentNullException("filePath");
- if (!Path.IsPathRooted(filePath))
- throw new ArgumentException("The localPath must be rooted", "filePath");
- Contract.EndContractBlock();
-
- var localPath = Pithos.Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
- //If the file already exists
- if (File.Exists(localPath))
- {
- //First check with MD5 as this is a small file
- var localMD5 = Signature.CalculateMD5(localPath);
- var cloudHash=serverHash.TopHash.ToHashString();
- if (localMD5==cloudHash)
- return;
- //Then check with a treehash
- var localTreeHash = Signature.CalculateTreeHash(localPath, serverHash.BlockSize, serverHash.BlockHash);
- var localHash = localTreeHash.TopHash.ToHashString();
- if (localHash==cloudHash)
- return;
- }
-
- var fileAgent = GetFileAgent(accountInfo);
- //Calculate the relative file path for the new file
- var relativePath = relativeUrl.RelativeUriToFilePath();
- //The file will be stored in a temporary location while downloading with an extension .download
- var tempPath = Path.Combine(fileAgent.CachePath, relativePath + ".download");
- //Make sure the target folder exists. DownloadFileTask will not create the folder
- var tempFolder = Path.GetDirectoryName(tempPath);
- if (!Directory.Exists(tempFolder))
- Directory.CreateDirectory(tempFolder);
-
- //Download the object to the temporary location
- await client.GetObject(cloudFile.Account, cloudFile.Container, relativeUrl.ToString(), tempPath);
-
- //Create the local folder if it doesn't exist (necessary for shared objects)
- var localFolder = Path.GetDirectoryName(localPath);
- if (!Directory.Exists(localFolder))
- Directory.CreateDirectory(localFolder);
- //And move it to its actual location once downloading is finished
- if (File.Exists(localPath))
- File.Replace(tempPath,localPath,null,true);
- else
- File.Move(tempPath,localPath);
- //Notify listeners that a local file has changed
- StatusNotification.NotifyChangedFile(localPath);
-
-
- }
-
- //Download a file asynchronously using blocks
- public async Task DownloadWithBlocks(AccountInfo accountInfo, CloudFilesClient client, ObjectInfo cloudFile, Uri relativeUrl, string filePath, TreeHash serverHash)
- {
- if (client == null)
- throw new ArgumentNullException("client");
- if (cloudFile == null)
- throw new ArgumentNullException("cloudFile");
- if (relativeUrl == null)
- throw new ArgumentNullException("relativeUrl");
- if (String.IsNullOrWhiteSpace(filePath))
- throw new ArgumentNullException("filePath");
- if (!Path.IsPathRooted(filePath))
- throw new ArgumentException("The filePath must be rooted", "filePath");
- if (serverHash == null)
- throw new ArgumentNullException("serverHash");
- Contract.EndContractBlock();
-
- var fileAgent = GetFileAgent(accountInfo);
- var localPath = Interfaces.FileInfoExtensions.GetProperFilePathCapitalization(filePath);
-
- //Calculate the relative file path for the new file
- var relativePath = relativeUrl.RelativeUriToFilePath();
- var blockUpdater = new BlockUpdater(fileAgent.CachePath, localPath, relativePath, serverHash);
-
-
-
- //Calculate the file's treehash
- var treeHash = await Signature.CalculateTreeHashAsync(localPath, serverHash.BlockSize, serverHash.BlockHash);
-
- //And compare it with the server's hash
- var upHashes = serverHash.GetHashesAsStrings();
- var localHashes = treeHash.HashDictionary;
- for (int i = 0; i < upHashes.Length; i++)
- {
- //For every non-matching hash
- var upHash = upHashes[i];
- if (!localHashes.ContainsKey(upHash))
- {
- if (blockUpdater.UseOrphan(i, upHash))
- {
- Log.InfoFormat("[BLOCK GET] ORPHAN FOUND for {0} of {1} for {2}", i, upHashes.Length, localPath);
- continue;
- }
- Log.InfoFormat("[BLOCK GET] START {0} of {1} for {2}", i, upHashes.Length, localPath);
- var start = i*serverHash.BlockSize;
- //To download the last block just pass a null for the end of the range
- long? end = null;
- if (i < upHashes.Length - 1 )
- end= ((i + 1)*serverHash.BlockSize) ;
-
- //Download the missing block
- var block = await client.GetBlock(cloudFile.Account, cloudFile.Container, relativeUrl, start, end);