//files that exist in the cloud but not on disk -- assume they were deleted public static int DeleteRemoteFiles(List <FileInfoX> localFiles, List <FileInfoX> remoteFiles, bool confirmDelete) { var toDelete = remoteFiles.Where(x => !localFiles.Select(y => y.CloudFileName).Contains(x.CloudFileName)).ToList(); if (toDelete.Count > 0) { NLogger.Current.Info("Found Deletes:"); foreach (var d in toDelete) { NLogger.Current.Info(d.LocalFileName); } } List <FileInfoX> confirmedToDelete = toDelete; if (confirmDelete) { confirmedToDelete = FillConfirmationList(toDelete, ConfirmRequest.Delete); } foreach (var x in confirmedToDelete) { if (string.IsNullOrEmpty(x.CloudFileName)) { continue; } OwnCloudClient.DeleteFile(x.CloudFileNameWithEmbeddedData); } return(confirmedToDelete.Count); }
//files that exist on disk and not in the cloud -- assume new file and upload //files that exist on disk and the lastmodified date is greater than our last sweep -- assume locally modified and upload public static int UploadNewLocalFiles(List <FileInfoX> localFiles, List <FileInfoX> remoteFiles, bool askUpload, DateTime lastSweepUtc) { var toUpload = localFiles.Where(x => !remoteFiles.Select(y => y.CloudFileName).Contains(x.CloudFileName) || x.LastModifiedUtc > lastSweepUtc).ToList(); if (toUpload.Count > 0) { NLogger.Current.Info("Found Uploads:"); foreach (var u in toUpload) { NLogger.Current.Info(u.LocalFileName); } } List <FileInfoX> confirmedToUpload = toUpload; if (askUpload) { confirmedToUpload = FillConfirmationList(toUpload, ConfirmRequest.Upload); } foreach (var x in confirmedToUpload) { if (x.LastModifiedUtc > lastSweepUtc) { var toDeleteX = remoteFiles.Where(y => y.CloudFileName == x.CloudFileName).FirstOrDefault(); OwnCloudClient.DeleteFile(toDeleteX.CloudFileNameWithEmbeddedData); } string f = Settings.WatchDir + x.LocalFileName; OwnCloudClient.UploadFile(f); } return(confirmedToUpload.Count); }
//files that have a newer version in the cloud -- download them and replace local files public static int ReplaceOutDatedLocalFiles(List <FileInfoX> localFiles, List <FileInfoX> remoteFiles, bool confirmDownload) { var localQuery = from l in localFiles join r in remoteFiles on l.LocalFileName equals r.LocalFileName where (r.LastModifiedUtc - l.LastModifiedUtc).TotalSeconds >= 2 select r; var shouldDownloadLocalQuery = localQuery.Where(f => OwnCloudClient.ShouldDownload(f)).ToList(); if (shouldDownloadLocalQuery.Count > 0) { NLogger.Current.Info("Found Downloads:"); foreach (var x in localQuery) { NLogger.Current.Info(x.LocalFileName); } } List <FileInfoX> confirmedToDownload = shouldDownloadLocalQuery; if (confirmDownload) { confirmedToDownload = FillConfirmationList(shouldDownloadLocalQuery, ConfirmRequest.Download); } foreach (var x in confirmedToDownload) { OwnCloudClient.DownloadFile(x.CloudFileNameWithEmbeddedData); } return(confirmedToDownload.Count); }
public static List <FileInfoX> GetLocalFileList() { List <FileInfoX> files = new List <FileInfoX>(); foreach (var item in GetFiles(Settings.WatchDir)) { if (!System.IO.File.Exists(item)) { continue; } else if (System.Text.RegularExpressions.Regex.IsMatch(item, @"\.enc\.\d{12}$")) { continue; } var info = new System.IO.FileInfo(item); FileInfoX x = new FileInfoX(); DateTime lastWriteUtc = new DateTime(info.LastWriteTime.Year, info.LastWriteTime.Month, info.LastWriteTime.Day, info.LastWriteTime.Hour, info.LastWriteTime.Minute, info.LastWriteTime.Second).ToUniversalTime(); //do it this way to avoid milisecond comparison problemsinfo.LastWriteTime; //!FileNameProcessing x.CloudFileName = item.Replace(Settings.WatchDir, "").Replace(System.IO.Path.DirectorySeparatorChar, '~') + ".enc"; x.LastModifiedUtc = lastWriteUtc; x.LocalFileName = item.Replace(Settings.WatchDir, ""); x.CloudFileNameWithEmbeddedData = x.CloudFileName + "." + OwnCloudClient.GetUnixUtcTimeStamp(lastWriteUtc); x.EncryptedCloudFileNameWithEmbeddedData = EncryptFileName(x.CloudFileNameWithEmbeddedData); files.Add(x); } return(files); }
//files that have older versions in the cloud -- upload the new ones and delete the old outdated cloud files public static int ReplaceOutDatedRemoteFiles(List <FileInfoX> localFiles, List <FileInfoX> remoteFiles, bool confirmUpload) { var remoteQuery = (from l in localFiles join r in remoteFiles on l.CloudFileName equals r.CloudFileName where (l.LastModifiedUtc - r.LastModifiedUtc).TotalSeconds >= 2 select new { OldCloudNamePlusDate = r.CloudFileNameWithEmbeddedData, NewFileName = r.LocalFileName }).ToList(); bool assumeConfirmed = !confirmUpload; Dictionary <string, string> confirmed = new Dictionary <string, string>(); if (remoteQuery.Count > 0) { NLogger.Current.Info("Found Uploads:"); foreach (var uploadFile in remoteQuery) { NLogger.Current.Info(uploadFile.NewFileName); } } foreach (var uploadFile in remoteQuery) { Console.Write(ConfirmRequest.Upload.ToString() + " " + uploadFile.NewFileName + " "); if (assumeConfirmed) { Console.WriteLine(); confirmed.Add(uploadFile.OldCloudNamePlusDate, uploadFile.NewFileName); continue; } ConfirmAnswer a = GetAnswer(uploadFile.NewFileName); if (a == ConfirmAnswer.Confirm) { confirmed.Add(uploadFile.OldCloudNamePlusDate, uploadFile.NewFileName); } else if (a == ConfirmAnswer.Skip) { continue; } else if (a == ConfirmAnswer.SkipAll) { break; } else if (a == ConfirmAnswer.ConfirmAll) { assumeConfirmed = true; confirmed.Add(uploadFile.OldCloudNamePlusDate, uploadFile.NewFileName); } } foreach (var d in confirmed) { OwnCloudClient.DeleteFile(d.Key); //OldCloudNamePlusDate OwnCloudClient.UploadFile(Settings.WatchDir + d.Value); //NewFileName } return(confirmed.Count); }
public static void DownloadAll() { foreach (var f in GetRemoteFileList()) { if (ShouldDownload(f)) { OwnCloudClient.DownloadFile(f.CloudFileNameWithEmbeddedData); } } }
static void Main(string[] args) { //System.Net.ServicePointManager.ServerCertificateValidationCallback = Validator; try { NLogger.Current.Trace("Getting Settings"); if (!SetSettings(args)) { return; } DisplayCurrentSettings(); NLogger.Current.Info("Logging In"); if (!OwnCloudClient.Login(Settings.UserName, Settings.Password)) { NLogger.Current.Warn("Invalid username or password"); return; } if (Settings.ListRemoteFiles) { OwnCloudClient.PrintRemoteFileList(); return; } if (Settings.DownloadAll || !string.IsNullOrEmpty(Settings.DownloadAllPrefix)) { //OwnCloudClient.DownloadAll("vccdrom~"); ConsoleColor currentColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Warning: This may overwrite files in your local directory: Continue? [y/n]: "); Console.ForegroundColor = currentColor; ConsoleKeyInfo k = Console.ReadKey(); Console.WriteLine(); if (k.KeyChar == 'y' || k.KeyChar == 'Y') { if (!string.IsNullOrEmpty(Settings.DownloadAllPrefix)) { OwnCloudClient.DownloadAll(Settings.DownloadAllPrefix); } else { OwnCloudClient.DownloadAll(); } } return; //bail out of program } NLogger.Current.Trace("Loading locals"); List <FileInfoX> localFiles = OwnCloudClient.GetLocalFileList(); NLogger.Current.Trace("Loading Remotes"); List <FileInfoX> remoteFiles = OwnCloudClient.GetRemoteFileList(); //outdated checks only need to run at the beginning since we are checking with lastSweep //NOTE: if used with multiple users on the same cloud account this becomes problematic int updatedRemoteFiles = FileHelpers.ReplaceOutDatedRemoteFiles(localFiles, remoteFiles, !Settings.NoConfirmUpload); if (updatedRemoteFiles > 0) { NLogger.Current.Trace("Refreshing remotes"); remoteFiles = OwnCloudClient.GetRemoteFileList(); //refresh } if (!Settings.UploadOnly) { FileHelpers.ReplaceOutDatedLocalFiles(localFiles, remoteFiles, !Settings.NoConfirmDownload); } DateTime lastSweepUtc = DateTime.UtcNow; while (true) { NLogger.Current.Trace("Refreshing locals"); localFiles = OwnCloudClient.GetLocalFileList(); DateTime currentSweepUtc = DateTime.UtcNow; //this is the correct place to take snapshot, but we need the previous one for the upload check below int uploadCount = FileHelpers.UploadNewLocalFiles(localFiles, remoteFiles, !Settings.NoConfirmUpload, lastSweepUtc); int deleteCount = 0; if (!Settings.UploadOnly) { deleteCount = FileHelpers.DeleteRemoteFiles(localFiles, remoteFiles, !Settings.NoConfirmDelete); } if (Settings.RunOnce) { return; //bail out of program } if (uploadCount > 0 || deleteCount > 0) { NLogger.Current.Trace("Refreshing remotes"); remoteFiles = OwnCloudClient.GetRemoteFileList(); } lastSweepUtc = currentSweepUtc; NLogger.Current.Trace(string.Format("Sleeping for {0} seconds", Settings.SleepSeconds)); System.Threading.Thread.Sleep(1000 * Settings.SleepSeconds); } } catch (Exception ex) { NLogger.Current.FatalException("Main() Exception", ex); } }