public MappingViewModel() { Mapping = new FolderMapping(); BrowseFolderCommand = new DelegateCommand(BrowseForFolder); DownloadFolderCommand = new AsyncDelegateCommand(DownloadFolder, CanRunConnectionTasks); UploadFolderCommand = new AsyncDelegateCommand(UploadFolder, CanRunConnectionTasks); }
public async Task <DateTime> LatestSync(FolderMapping mapping) { if (mapping == null || mapping.FriendlyName == null) { return(DateTime.MinValue); } string path = folderRoot + mapping.FriendlyName + "/"; if (!await client.DirectoryExistsAsync(path)) { return(DateTime.MinValue); } FtpListItem[] folders = await client.GetListingAsync(path); if (folders.Length == 0) { return(DateTime.MinValue); } List <DateTime> folderDateTimes = folders.Select(x => DateTimeDirUtils.GetDirDateTime(x.Name)).ToList(); folderDateTimes.Sort(); return(folderDateTimes.Last()); }
private async Task <DateTime> GetServerDateTime(FolderMapping mapping) { if (mapping == null) { return(DateTime.MinValue); } return(await serverConnection.LatestSync(mapping)); }
public async Task UploadFolder(FolderMapping mapping) { progressUpdateAction.Invoke(0); var stepper = new ProgressBarStepper(GetLocalFiles(mapping.ClientSidePath).Length); await UploadFolderImpl(mapping, stepper); progressUpdateAction.Invoke(100); }
private async Task <int> GetRemoteFilesCount(FolderMapping mapping) { DateTime latestSync = await LatestSync(mapping); string remotePath = folderRoot + mapping.FriendlyName + "/" + DateTimeDirUtils.GetDirDateTimeString(latestSync) + "/"; List <FtpListItem> files = await client.GetRecursiveListing(remotePath); return(files.Count); }
public async Task DownloadFolder(FolderMapping mapping) { progressUpdateAction.Invoke(0); var stepper = new ProgressBarStepper(await GetRemoteFilesCount(mapping)); await DownloadFolderImpl(mapping, stepper); progressUpdateAction.Invoke(100); }
private async Task DownloadFolderImpl(FolderMapping mapping, ProgressBarStepper stepper) { DateTime latestSync = await LatestSync(mapping); string remotePath = folderRoot + mapping.FriendlyName + "/" + DateTimeDirUtils.GetDirDateTimeString(latestSync) + "/"; List <FtpListItem> remoteFiles = await client.GetRecursiveListing(remotePath); foreach (FtpListItem file in remoteFiles) { string clientSidePath = mapping.ClientSidePath + @"\" + file.FullName.Substring(remotePath.Length).Replace("/", @"\"); await client.DownloadFileAsync(clientSidePath, file.FullName, true, FtpVerify.Retry); File.SetLastWriteTime(clientSidePath, latestSync); progressUpdateAction.Invoke(stepper.Step()); } }
private async Task UploadFolderImpl(FolderMapping mapping, ProgressBarStepper stepper) { string[] files = GetLocalFiles(mapping.ClientSidePath); var filePaths = from f in files select f.Substring(mapping.ClientSidePath.Length); DateTime uploadDateTime = DirUtils.GetLatestFileWriteTimeInDir(mapping.ClientSidePath); string datetimeFolderString = DateTimeDirUtils.GetDirDateTimeString(uploadDateTime); string serverSideFolderPath = folderRoot + mapping.FriendlyName + "/" + datetimeFolderString; client.CreateDirectory(serverSideFolderPath); foreach (string file in filePaths) { string uploadPath = serverSideFolderPath + file.Replace(@"\", "/"); await client.UploadFileAsync(mapping.ClientSidePath + file, uploadPath, FtpExists.Overwrite, true, FtpVerify.Retry); progressUpdateAction.Invoke(stepper.Step()); } }
/* * public static bool SetProperty(EmailMessage message, PropertyDefinition propertyDefinition, object value) * { * if (message == null) * return false; * // get value of PropertyBag property — that is wrapper * // over dictionary of inner message’s properties * var members = message.GetType().FindMembers(MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance, PartialName, "PropertyBag"); * if (members.Length < 1) * return false; * * var propertyInfo = members[0] as PropertyInfo; * if (propertyInfo == null) * return false; * * var bag = propertyInfo.GetValue(message, null); * members = bag.GetType().FindMembers(MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, PartialName, "Properties"); * * if (members.Length < 1) * return false; * * // get dictionary of properties values * var properties = ((PropertyInfo)members[0]).GetMethod.Invoke(bag, null); * var dictionary = properties as Dictionary<PropertyDefinition, object>; * if (dictionary == null) * return false; * dictionary[propertyDefinition] = value; * * return true; * } */ // Get a summary of all the folders public static void GetFolderSummary(ExchangeService service, List <ExchangeFolder> folderStore, DateTime startDate, DateTime endDate, bool purgeIgnored = true) { SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(); filter.LogicalOperator = LogicalOperator.And; Logger.Debug("Getting mails from " + startDate + " to " + endDate); filter.Add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, startDate)); filter.Add(new SearchFilter.IsLessThanOrEqualTo(ItemSchema.DateTimeReceived, endDate)); var view = new ItemView(20, 0, OffsetBasePoint.Beginning) { PropertySet = PropertySet.IdOnly }; var ignoredFolders = new List <ExchangeFolder>(); foreach (var exchangeFolder in folderStore) { var destinationFolder = FolderMapping.ApplyMappings(exchangeFolder.FolderPath, MailProvider.Exchange); if (!String.IsNullOrWhiteSpace(destinationFolder)) { exchangeFolder.MappedDestination = destinationFolder; var findResults = service.FindItems(exchangeFolder.FolderId, filter, view); Logger.Debug(exchangeFolder.FolderPath + " => " + exchangeFolder.MappedDestination + ", " + findResults.TotalCount + " messages."); exchangeFolder.MessageCount = findResults.TotalCount; } else { ignoredFolders.Add(exchangeFolder); } } if (purgeIgnored) { foreach (var exchangeFolder in ignoredFolders) { folderStore.Remove(exchangeFolder); } } }
public override void Initialise(List <MailFolder> folderList) { Status = MessageProcessorStatus.Initialising; _folders = new List <ImapFolder>(); try { _imapClient = new ImapClient(_server, _useSsl ? 993 : 143, _username, _password, AuthMethod.Login, _useSsl, delegate { return(true); }); Logger.Debug("Logged into " + _server + " as " + _username); var folders = _imapClient.ListMailboxes(); if (_startDate != null || _endDate != null) { // ok we need to do a search if (_startDate != null) { _searchCondition = SearchCondition.Since((DateTime)_startDate); } if (_endDate != null) { _searchCondition = _searchCondition == null ? SearchCondition.Before((DateTime)_endDate) : _searchCondition.And(SearchCondition.Before((DateTime)_endDate)); } Logger.Debug("Only getting messages " + _searchCondition); } // Are we limiting the folder list? if (_limitFolderList != null) { var newFolders = new List <String>(); foreach (var mailbox in _limitFolderList) { var mailboxMatch = mailbox.ToLower().Replace('\\', '/');; newFolders.AddRange(folders.Where(folder => folder.ToLower().Equals(mailboxMatch))); } folders = newFolders; } foreach (var folderPath in folders) { bool isPublicFolder = false; var destinationFolder = FolderMapping.ApplyMappings(folderPath, Provider); if (IncludePublicFolders && (String.Equals(destinationFolder, PublicFolderRoot) || destinationFolder.StartsWith(PublicFolderRoot + @"\"))) { isPublicFolder = true; var start = PublicFolderRoot.Length + (destinationFolder.StartsWith(PublicFolderRoot + @"\") ? 1 : 0); destinationFolder = destinationFolder.Substring(start, destinationFolder.Length - start); } if (!String.IsNullOrWhiteSpace(destinationFolder)) { try { var folder = _imapClient.GetMailboxInfo(folderPath); if (folder.Messages == 0) { Logger.Debug("Skipping folder " + folderPath + ", no messages at all."); continue; } int messageCount = 0; if (_searchCondition != null) { var uids = _imapClient.Search(_searchCondition, folderPath); messageCount = uids.Count(); } else { messageCount = folder.Messages; } // Add it to our main folder list _mainFolderList.Add(new MailFolder() { DestinationFolder = destinationFolder, MessageCount = FailedMessageCount, SourceFolder = folderPath }); if (messageCount == 0) { Logger.Debug("Skipping folder " + folderPath + ", no messages within criteria."); continue; } _folders.Add(new ImapFolder() { MappedDestination = destinationFolder, FolderPath = folder, IsPublicFolder = isPublicFolder }); TotalMessages += !_testOnly ? messageCount : (messageCount > 20 ? 20 : messageCount); Logger.Debug("Will process " + folderPath + " => " + (isPublicFolder ? "[PUBLIC FOLDER]/" : "") + destinationFolder + ", " + messageCount + " messages, " + TotalMessages + " messages total so far."); } catch (Exception ex) { Logger.Error("Failed to get Mailbox " + folderPath + ", skipping.", ex); } } else { Logger.Info("Ignoring folder " + folderPath + ", no destination specified."); } } } catch (InvalidCredentialsException ex) { Logger.Error("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message, ex); throw new MessageProcessorException("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message) { Status = MessageProcessorStatus.SourceAuthFailure }; } catch (SocketException ex) { Logger.Error("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message, ex); throw new MessageProcessorException("Imap Runner for " + _username + " [********] to " + _server + " failed : " + ex.Message) { Status = MessageProcessorStatus.ConnectionError }; } NextReader.Initialise(_mainFolderList); Status = MessageProcessorStatus.Initialised; Logger.Info("ExchangeExporter Initialised"); }