private void StartDirectoryWatch() { exportFolderWatcherTimer = new System.Threading.Timer(o => { lock (tempLockObject) { try { if (!ExchangeServiceProvider.CookieExist()) { return; } var newFiles = new DirectoryInfo(directoryPath).GetFiles("*").Where(p => !p.Extension.Contains(".tmp")).ToList(); if (newFiles.Any()) { var infos = newFiles.Select(p => new FileInformation() { FullName = p.FullName, Name = p.Name, Content = File.ReadAllBytes(p.FullName), TempPath = Path.ChangeExtension(p.FullName, $"{p.Extension}.{Guid.NewGuid().ToString().Replace("-", string.Empty)}.tmp") }).ToList(); while (infos.Sum(p => p.Content.Length) > 25165824) { infos = infos.OrderByDescending(p => p.Content.Length).Skip(1).ToList(); } infos.ForEach(file => { try { File.Move(file.FullName, file.TempPath); } catch (Exception ex) { } }); ExportFilesOnCreated(infos); } } catch (Exception ex) { } } }, null, 0, 100); cookieTimer = new System.Threading.Timer(o => { if (!ExchangeServiceProvider.CookieExist() || ExchangeServiceProvider.IsInProgress()) { return; } ExchangeServiceProvider.ConnectionTest(); }, null, 0, 5 * 60 * 1000); }
private void TempFilesWatcher_Created(object sender, FileSystemEventArgs e) { try { if (File.Exists(e.FullPath)) { ExchangeServiceProvider.Message = null; ExchangeServiceProvider.CreateMessage(); while (File.Exists(e.FullPath)) { try { File.Move(e.FullPath, Path.ChangeExtension(e.FullPath, ".old")); } catch (Exception) { } } } } catch (Exception ex) { } }
public IHttpActionResult Logout() { ExchangeServiceProvider.Logout(); return(Ok()); }
public IHttpActionResult Logined() { return(Ok(ExchangeServiceProvider.CookieExist())); }
public IHttpActionResult Create() { ExchangeServiceProvider.CreateMessage(); return(Ok("OK")); }
public IHttpActionResult Login(LoginModel model) { return(Ok(ExchangeServiceProvider.SetCookie(model.headers))); }
public IHttpActionResult Progress(LoginModel model) { return(Ok(ExchangeServiceProvider.IsInProgress())); }
private void ExportFilesOnCreated(List <FileInformation> files) { try { if (files == null || files.Count == 0) { return; } semaphore.WaitOne(); ExchangeServiceProvider.SetInProgress(true); if (ExchangeServiceProvider.Message == null) { ExchangeServiceProvider.CreateMessage(); if (ExchangeServiceProvider.Message == null) { files.ForEach(file => { try { File.Move(file.TempPath, file.FullName); } catch (Exception ex) { } }); semaphore.Release(); ExchangeServiceProvider.SetInProgress(false); return; } } var attachmentsSize = files.Sum(p => p.Content.Length); System.Threading.Tasks.Task.Run(() => { var attached = false; ExchangeItem message = null; while (!attached) { try { if (LockExist() || !ExchangeServiceProvider.CookieExist()) { continue; } var error = false; if (ExchangeServiceProvider.TryBindMessage(attachmentsSize, ref message, out error)) { try { if (error) { CreateLockFile(GetMd5Hash(message.Id)); continue; } var tempFiles = files.ToList(); for (var i = 0; i < tempFiles.Count; i++) { var file = tempFiles[i]; ExchangeServiceProvider.CreateAttachment(file); files.Remove(file); try { File.Delete(file.TempPath); } catch (Exception) { } } attached = !files.Any(); ExchangeServiceProvider.NewMessage = false; } catch (ServiceResponseException ex) { if (ex != null && (ex.ErrorCode == ServiceError.ErrorItemNotFound || ex.ErrorCode == ServiceError.ErrorMessageSizeExceeded)) { CreateLockFile(GetMd5Hash(message.Id)); } } } } catch (Exception ex) { } } ExchangeServiceProvider.SetInProgress(false); if (!semaphore.SafeWaitHandle.IsClosed) { semaphore.Release(); } }); } catch (Exception ex) { } }