- //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);
-
- //and store it
- blockUpdater.StoreBlock(i, block);
-
-
- Log.InfoFormat("[BLOCK GET] FINISH {0} of {1} for {2}", i, upHashes.Length, localPath);
- }
- }
-
- //Want to avoid notifications if no changes were made
- var hasChanges = blockUpdater.HasBlocks;
- blockUpdater.Commit();
-
- if (hasChanges)
- //Notify listeners that a local file has changed
- StatusNotification.NotifyChangedFile(localPath);
-
- Log.InfoFormat("[BLOCK GET] COMPLETE {0}", localPath);
- }
-
-
- private async Task UploadCloudFile(CloudAction action)
- {
- if (action == null)
- throw new ArgumentNullException("action");
- Contract.EndContractBlock();
-
- try
- {
- var accountInfo = action.AccountInfo;
-
- var fileInfo = action.LocalFile;
-
- if (fileInfo.Extension.Equals("ignore", StringComparison.InvariantCultureIgnoreCase))
- return;
-
- var relativePath = fileInfo.AsRelativeTo(accountInfo.AccountPath);
- if (relativePath.StartsWith(FolderConstants.OthersFolder))
- {
- var parts = relativePath.Split('\\');
- var accountName = parts[1];
- var oldName = accountInfo.UserName;
- var absoluteUri = accountInfo.StorageUri.AbsoluteUri;
- var nameIndex = absoluteUri.IndexOf(oldName);
- var root = absoluteUri.Substring(0, nameIndex);
-
- accountInfo = new AccountInfo
- {
- UserName = accountName,
- AccountPath = Path.Combine(accountInfo.AccountPath, parts[0], parts[1]),
- StorageUri = new Uri(root + accountName),
- BlockHash = accountInfo.BlockHash,
- BlockSize = accountInfo.BlockSize,
- Token = accountInfo.Token
- };
- }
-
-
- var fullFileName = fileInfo.GetProperCapitalization();
- using (var gate = NetworkGate.Acquire(fullFileName, NetworkOperation.Uploading))
- {
- //Abort if the file is already being uploaded or downloaded
- if (gate.Failed)
- return;
-
- var cloudFile = action.CloudFile;
- var account = cloudFile.Account ?? accountInfo.UserName;
-
- var client = new CloudFilesClient(accountInfo);
- //Even if GetObjectInfo times out, we can proceed with the upload
- var info = client.GetObjectInfo(account, cloudFile.Container, cloudFile.Name);
-
- //If this is a read-only file, do not upload changes
- if (info.AllowedTo == "read")
- return;
-
- //TODO: Check how a directory hash is calculated -> All dirs seem to have the same hash
- if (fileInfo is DirectoryInfo)
- {
- //If the directory doesn't exist the Hash property will be empty
- if (String.IsNullOrWhiteSpace(info.Hash))
- //Go on and create the directory
- await client.PutObject(account, cloudFile.Container, cloudFile.Name, fullFileName, String.Empty, "application/directory");
- }
- else
- {
-
- var cloudHash = info.Hash.ToLower();
-
- var hash = action.LocalHash.Value;
- var topHash = action.TopHash.Value;
-
- //If the file hashes match, abort the upload
- if (hash == cloudHash || topHash == cloudHash)
- {
- //but store any metadata changes
- StatusKeeper.StoreInfo(fullFileName, info);
- Log.InfoFormat("Skip upload of {0}, hashes match", fullFileName);
- return;
- }
-
-
- //Mark the file as modified while we upload it
- StatusKeeper.SetFileOverlayStatus(fullFileName, FileOverlayStatus.Modified);
- //And then upload it
-
- //Upload even small files using the Hashmap. The server may already contain
- //the relevant block
-
- //First, calculate the tree hash
- var treeHash = await Signature.CalculateTreeHashAsync(fullFileName, accountInfo.BlockSize,
- accountInfo.BlockHash);
-
- await UploadWithHashMap(accountInfo, cloudFile, fileInfo as FileInfo, cloudFile.Name, treeHash);
- }
- //If everything succeeds, change the file and overlay status to normal
- StatusKeeper.SetFileState(fullFileName, FileStatus.Unchanged, FileOverlayStatus.Normal);
- }
- //Notify the Shell to update the overlays
- NativeMethods.RaiseChangeNotification(fullFileName);
- StatusNotification.NotifyChangedFile(fullFileName);
- }
- catch (AggregateException ex)
- {
- var exc = ex.InnerException as WebException;
- if (exc == null)
- throw ex.InnerException;
- if (HandleUploadWebException(action, exc))
- return;
- throw;
- }
- catch (WebException ex)
- {
- if (HandleUploadWebException(action, ex))
- return;
- throw;
- }
- catch (Exception ex)
- {
- Log.Error("Unexpected error while uploading file", ex);
- throw;
- }
-
- }
-
- private bool IsDeletedFile(CloudAction action)
- {
- var key = GetFileKey(action.CloudFile);
- DateTime entryDate;
- if (_deletedFiles.TryGetValue(key, out entryDate))
- {
- //If the delete entry was created after this action, abort the action
- if (entryDate > action.Created)
- return true;
- //Otherwise, remove the stale entry
- _deletedFiles.TryRemove(key, out entryDate);
- }
- return false;
- }
-
- private bool HandleUploadWebException(CloudAction action, WebException exc)
- {
- var response = exc.Response as HttpWebResponse;
- if (response == null)
- throw exc;
- if (response.StatusCode == HttpStatusCode.Unauthorized)
- {
- Log.Error("Not allowed to upload file", exc);
- var message = String.Format("Not allowed to uplad file {0}", action.LocalFile.FullName);
- StatusKeeper.SetFileState(action.LocalFile.FullName, FileStatus.Unchanged, FileOverlayStatus.Normal);
- StatusNotification.NotifyChange(message, TraceLevel.Warning);
- return true;
- }
- return false;
- }
-
- public async Task UploadWithHashMap(AccountInfo accountInfo,ObjectInfo cloudFile,FileInfo fileInfo,string url,TreeHash treeHash)
- {
- if (accountInfo == null)
- throw new ArgumentNullException("accountInfo");
- if (cloudFile==null)
- throw new ArgumentNullException("cloudFile");
- if (fileInfo == null)
- throw new ArgumentNullException("fileInfo");
- if (String.IsNullOrWhiteSpace(url))
- throw new ArgumentNullException(url);
- if (treeHash==null)
- throw new ArgumentNullException("treeHash");
- if (String.IsNullOrWhiteSpace(cloudFile.Container) )
- throw new ArgumentException("Invalid container","cloudFile");
- Contract.EndContractBlock();
-
- var fullFileName = fileInfo.GetProperCapitalization();
-
- var account = cloudFile.Account ?? accountInfo.UserName;
- var container = cloudFile.Container ;
-
- var client = new CloudFilesClient(accountInfo);
- //Send the hashmap to the server
- var missingHashes = await client.PutHashMap(account, container, url, treeHash);
- //If the server returns no missing hashes, we are done
- while (missingHashes.Count > 0)
- {
-
- var buffer = new byte[accountInfo.BlockSize];
- foreach (var missingHash in missingHashes)
- {
- //Find the proper block
- var blockIndex = treeHash.HashDictionary[missingHash];
- var offset = blockIndex*accountInfo.BlockSize;
-
- var read = fileInfo.Read(buffer, offset, accountInfo.BlockSize);
-
- try
- {
- //And upload the block
- await client.PostBlock(account, container, buffer, 0, read);
- Log.InfoFormat("[BLOCK] Block {0} of {1} uploaded", blockIndex, fullFileName);
- }
- catch (Exception exc)
- {
- Log.ErrorFormat("[ERROR] uploading block {0} of {1}\n{2}", blockIndex, fullFileName, exc);
- }
-
- }
-
- //Repeat until there are no more missing hashes
- missingHashes = await client.PutHashMap(account, container, url, treeHash);
- }
- }