protected FileInfoLock LockFile(DriveService.Stream stream, string fileId, string filePath) { try { Mutex.Wait(); try { var fileInfoLock = new FileInfoLock(this, fileId, filePath); Items.Add(new Item(fileInfoLock, stream)); if (OnLockFile != null) { OnLockFile(this, fileInfoLock); } return(fileInfoLock); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception, true, false); return(null); } }
public void FinishStream(DriveService.Stream stream) { try { Mutex.Wait(); try { int index = 0; bool found = false; foreach (Item item in Items) { if (item.Stream == stream) { found = true; Items.RemoveAt(index); break; } index++; } if (!found) { return; } DriveService.Stream.Factory.Dispose(stream); if (stream.Visible) { try { if (OnFinishedStream != null) { OnFinishedStream(this, stream); } } catch { } } TryFinishProcessing(); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception); } }
public void QueueStream(DriveService.Stream stream) { try { QueueStreams(new[] { stream }); } catch (Exception exception) { Log.Error(exception); } }
private void uploadStream_ProgressChanged(DriveService.Stream stream) { try { Log.Information("Uploading file " + stream.FileId + " - " + stream.PercentCompleted + "% completed"); } catch (Exception exception) { Log.Error(exception, false); } }
public FileInfoLock LockStream(DriveService.Stream stream, string filePath) { try { return(LockFile(stream, stream.FileId, filePath)); } catch (Exception exception) { Log.Error(exception); return(null); } }
private void uploadStream_ProgressStarted(DriveService.Stream stream) { try { _processCount++; Log.Information("Started uploading file " + stream.FileId); } catch (Exception exception) { Log.Error(exception, false); } }
public FileInfoLock LockStream(DriveService.Stream stream, string filePath, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare) { try { return(LockFile(stream, stream.FileId, filePath, fileMode, fileAccess, fileShare)); } catch (Exception exception) { Log.Error(exception); return(null); } }
public bool IsStreamLocked(string fileId, string filePath, ref DriveService.Stream stream) { try { Item lockedItem = null; bool result = IsStreamLocked(fileId, filePath, ref lockedItem); if (result) { stream = lockedItem.Stream; } return(result); } catch (Exception exception) { Log.Error(exception); return(false); } }
private void uploadStream_ProgressFinished(DriveService.Stream stream) { try { _processCount--; if (stream.Completed) { Log.Information("Completed uploading file " + stream.FileId); } else if (stream.Cancelled) { Log.Information("Cancelled uploading file " + stream.FileId); } else if (stream.Failed) { Log.Information("Failed uploading file " + stream.FileId + " - " + stream.ExceptionMessage); } } catch (Exception exception) { Log.Error(exception, false); } }
public void UploadStream_ProgressFinished(DriveService.Stream stream) { UploadFileWatcher.UploadStream_ProgressFinished(UploadStream, this); }
protected bool ProcessFile(string fullPath, bool handleLockedItems) { try { Mutex.Wait(); try { string folderPath = System.IO.Path.GetDirectoryName(fullPath); string rootPath = System.IO.Path.GetDirectoryName(folderPath); if (!String.Equals(rootPath, FolderPath, StringComparison.CurrentCultureIgnoreCase)) { return(false); } Log.Information("UploadFileWatcher attempting to process file " + fullPath); string filePath = fullPath; string fileExtension = System.IO.Path.GetExtension(filePath); if (String.Equals(".download", fileExtension, StringComparison.CurrentCultureIgnoreCase)) { Log.Information("File " + fullPath + " is downloading."); return(false); } int itemIndex = -1; foreach (Item item in Items) { itemIndex++; if (String.Equals(item.FilePath, filePath, StringComparison.CurrentCultureIgnoreCase)) { DateTime lastWriteTime = GetFileLastWriteTime(filePath); if (IsDateTimeEqual(item.LastWriteTime, lastWriteTime)) { Log.Information("File " + fullPath + " last write time " + lastWriteTime + " has not changed since the last time it was processed."); return(false); } else if (item.Status == DriveService.Stream.StatusType.Starting) { Log.Warning("File " + fullPath + " is starting uploading."); return(false); } else if (item.Status == DriveService.Stream.StatusType.Processing) { Log.Warning("File " + fullPath + " is uploading " + item.PercentCompleted + "% completed."); return(false); } else if (item.Status == DriveService.Stream.StatusType.Cancelling) { Log.Warning("File " + fullPath + " is cancelling uploading."); return(false); } item.Status = DriveService.Stream.StatusType.NotStarted; item.PercentCompleted = 0; break; } } if (!DriveService.IsSignedIn) { Log.Warning("Drive service is not signed in."); return(false); } using (DriveService driveService = DriveService.Create()) { string fileId = System.IO.Path.GetFileName(folderPath); FileInfo fileInfo = driveService.GetFile(fileId); if (fileInfo != null) { FileInfoStatus fileInfoStatus = GetFileInfoStatus(fileInfo); DriveService.Stream stream = null; filePath = fileInfo.FilePath; if (fileInfoStatus != FileInfoStatus.ModifiedOnDisk) { Log.Information("File " + fileId + " is not modified on disk"); return(true); } else if (fileInfo.IsGoogleDoc) { Log.Information("File " + fileId + " is a google doc"); return(true); } else if (IsStreamLocked(fileInfo.Id, fileInfo.FilePath, ref stream)) { Log.Warning("Stream " + fileId + " is locked (type=" + stream.Type + ")"); } else if (IsFileLocked(fileInfo.Id, fileInfo.FilePath)) { Log.Warning("File " + fileId + " is locked"); } else if (!CanOpenFile(filePath, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite, 1)) { Log.Warning("File " + fullPath + " is being used by another application."); if (handleLockedItems) { AddLockedItem(filePath); } } else { Log.Information("Attempting to upload file " + fileId); if (handleLockedItems) { RemoveLockedItem(filePath); } Item item = null; if (itemIndex >= 0) { item = Items[itemIndex]; } else { item = new Item(this); item.FilePath = filePath; Items.Add(item); } item.UploadStream = new DriveService.UploadStream(); item.UploadStream.OnProgressStarted += item.UploadStream_ProgressStarted; item.UploadStream.OnProgressChanged += item.UploadStream_ProgressChanged; item.UploadStream.OnProgressFinished += item.UploadStream_ProgressFinished; item.Status = DriveService.Stream.StatusType.Starting; try { item.UploadStream.Init(fileInfo, null); driveService.UploadFile(item.UploadStream); return(true); } catch (Exception exception) { if (item.Status == DriveService.Stream.StatusType.Starting) { item.Status = DriveService.Stream.StatusType.Failed; } throw exception; } } } else { Log.Information("File " + fileId + " no longer exists"); return(true); } } return(false); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception, false); return(false); } }
public Item(DriveService.Stream stream) { Stream = stream; }
public Item(FileInfoLock fileInfoLock, DriveService.Stream stream) { FileInfoLock = fileInfoLock; Stream = stream; }
protected FileInfoLock LockFile(DriveService.Stream stream, string fileId, string filePath, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare) { try { Mutex.Wait(); try { System.IO.FileStream fileStream = null; Exception lastException = null; for (int i = 0; i < 10; i++) { try { // Open file will try and execute for 1 sec, // for this function we will wait up to 10 sec fileStream = OpenFile(filePath, fileMode, fileAccess, fileShare); lastException = null; break; } catch (Exception exception) { lastException = exception; } System.Threading.Thread.Sleep(100); } if (lastException != null) { throw lastException; } FileInfoLock fileInfoLock = null; try { fileInfoLock = LockFile(stream, fileId, filePath); fileInfoLock.FileStream = fileStream; } catch (Exception exception) { if (fileStream != null) { fileStream.Dispose(); fileStream = null; } throw exception; } return(fileInfoLock); } finally { Mutex.Release(); } } catch (Exception exception) { Log.Error(exception, true, false); return(null); } }
private void ProcessFolder(DriveService driveService, string folderPath) { try { Log.Information("CleanupFileWatcher attempting to process folder " + folderPath); string fileId = System.IO.Path.GetFileName(folderPath); FileInfo fileInfo = null; try { fileInfo = driveService.GetFile(fileId); } catch (Exception exception) { Log.Error(exception, false, false); } if (fileInfo != null) { DriveService.Stream stream = null; if (IsStreamLocked(fileInfo.Id, fileInfo.FilePath, ref stream)) { Log.Warning("Stream " + fileId + " is locked (type=" + stream.Type + ")"); } else if (IsFileLocked(fileInfo.Id, fileInfo.FilePath)) { Log.Warning("File " + fileId + " is locked"); } else if (!CanOpenFile(fileInfo.FilePath, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite, 1)) { Log.Warning("File " + fileInfo.FilePath + " is being used by another application."); } else if (!HasFileOnDiskTimedout(fileInfo.FilePath)) { Log.Warning("File " + fileId + " has not timed out on disk"); } else { FileInfoStatus fileInfoStatus = GetFileInfoStatus(fileInfo); if (fileInfoStatus == FileInfoStatus.ModifiedOnDisk) { Log.Information("Attempting to upload file " + fileId); var uploadStream = new DriveService.UploadStream(); uploadStream.OnProgressStarted += uploadStream_ProgressStarted; uploadStream.OnProgressChanged += uploadStream_ProgressChanged; uploadStream.OnProgressFinished += uploadStream_ProgressFinished; uploadStream.Init(fileInfo, null); driveService.UploadFile(uploadStream); } else { Log.Information("Attempting to clean-up file " + fileId); driveService.CleanupFile(fileInfo, fileInfoStatus, false); } } } else { Log.Information("Attempting to clean-up directory " + folderPath + ": File was not found in google drive"); System.IO.Directory.Delete(folderPath, true); } } catch (Exception exception) { Log.Error(exception, false); } }