private void ThreadedSyncAndScan(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
Dictionary<string, SyncItem> serverhashes;
//get the server hashlist
AddTextToConsole("Getting server hashes (" + Settings.HashServer + ")" + Environment.NewLine);
try
{
serverhashes = Http.GetHashList();
}
catch (Exception ex)
{
AddTextToConsole("Error Reading Remote Hashes:" + ex.Message + Environment.NewLine);
return;
}
//hash all teh local files.
AddTextToConsole("Checking Local hashes" + Environment.NewLine);
SyncList LocalData = new SyncList(Settings.LocalDirectory);
List<string> FilesToDownload = new List<string>();
LocalData.saveSyncList (Settings.HashCache);
//get the delta list
foreach (KeyValuePair<string, SyncItem> kvp in serverhashes)
{
//ignore metadata.
if (kvp.Key == "__Server" || kvp.Key == "__DateGeneratedUTC")
{
;
}
else
{
//Queue downloads
if (!LocalData.HashList.ContainsKey(kvp.Key))
{
//downlaod files that dont exist.
FilesToDownload.Add(kvp.Key);
}
else
{
if (kvp.Value.Hash != LocalData.HashList[kvp.Key].Hash)
{
//download files the dont mathc the hash
FilesToDownload.Add(kvp.Key);
}
else
{
//if we get here the file exist and is right.
//get it out of the hash list so we dont delrte it.
LocalData.HashList.Remove(kvp.Key);
}
}
}
}
//Remove Files if set to
if (Settings.RemoveLocalFileIfNoRemoteFile)
{
AddTextToConsole("Generating list of local files to remove" + Environment.NewLine);
bool shouldDelete = true;
//ensure we didnt delte everything accidentally.
if (LocalData.HashList.Count > Settings.numFilesToRemoveWithNoWarning)
{
shouldDelete = false;
if (MessageBox.Show(LocalData.HashList.Count + " Files Are flagged for deletion." + Environment.NewLine + " Delete them?", "Continue?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
shouldDelete = true;
AddTextToConsole("Deleting Files" + Environment.NewLine);
}
else
{
AddTextToConsole("Files Will NOT be Removed" + Environment.NewLine);
}
}
//remove files
if (shouldDelete && LocalData.HashList.Count >0)
{
foreach (KeyValuePair<string, SyncItem> kvp in LocalData.HashList)
{
AddTextToConsole(" "+ kvp.Key + Environment.NewLine);
File.Delete(Settings.LocalDirectory + kvp.Key);
}
}
}
//DOWNLOADS
AddTextToConsole ("Need to download: " + FilesToDownload.Count +" from " +Settings.DownloadType +Environment.NewLine);
ErrorCount =0;
int tasklimit =10;
int nextFile =0;
GetObjectRequest request;
AmazonS3Client s3 = new AmazonS3Client (Settings.s3IDKey, Settings.s3SecretKey);
while(nextFile < FilesToDownload.Count || DownloadCount >0 ){
if(DownloadCount < tasklimit && nextFile<FilesToDownload.Count){
request = new GetObjectRequest();
request.BucketName = Settings.s3Bucket;
request.Key = FilesToDownload[nextFile].Substring (1);//use substring so we elminate the /
request.Timeout =1000;//wait 1 minute for a response.
AddTextToConsole(" "+(nextFile +1)+"/"+FilesToDownload.Count +" "+request.Key+Environment.NewLine);
s3.BeginGetObject(request,DownloadFile,s3);
nextFile++;
DownloadCount++;
worker.ReportProgress((int)(((float)nextFile) / ((float)FilesToDownload.Count) * 100.0f));
}
}
if (nextFile-ErrorCount < FilesToDownload.Count)
{
AddTextToConsole("WARNING: NOT ALL FILES WERE SUCCESSFULLY DOWNLOADED" + Environment.NewLine + Environment.NewLine);
}
AddTextToConsole("Download queue processed. Check log for any errors"+Environment.NewLine);
}