- //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, 2);
-
- 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;
- }
-
- }