public FileInfo Invoke(string fileId) { try { if (String.IsNullOrEmpty(fileId)) { throw new Exception("Input parameter 'fileId' is null or empty (Cannot download root directory)."); } using (API.DriveService driveService = DriveProxy.API.DriveService.Create()) { FileInfo fileInfo = driveService.GetFile(fileId); if (fileInfo == null) { throw new Exception("File no longer exists."); } FileInfoStatus fileInfoStatus = DriveProxy.API.DriveService.GetFileInfoStatus(fileInfo); if (fileInfoStatus == FileInfoStatus.ModifiedOnDisk || fileInfoStatus == FileInfoStatus.OnDisk) { return(fileInfo); } var downloadStream = new DriveProxy.API.DriveService.DownloadStream(); downloadStream.Init(fileInfo); driveService.DownloadFile(downloadStream); while (!downloadStream.Finished) { System.Threading.Thread.Sleep(250); } if (downloadStream.Cancelled) { return(null); } if (downloadStream.Failed) { throw new LogException("Download could not complete - " + downloadStream.ExceptionMessage, true, true); } fileInfo = downloadStream.FileInfo; return(fileInfo); } } catch (Exception exception) { Log.Error(exception, breakWithDebugger: false); return(null); } }
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); } }
protected override void Start() { try { if (Status != StatusType.Queued) { throw new Exception("Stream has not been queued."); } base.Start(); _FileInfo = _DriveService.GetFile(FileId); if (_FileInfo == null) { throw new Exception("File '" + FileId + "' no longer exists."); } Lock(); if (_FileInfo.ParentId == ParentId) { DriveService_ProgressChanged(StatusType.Completed, null); } else { FileInfoStatus currentStatus = API.DriveService.GetFileInfoStatus(_FileInfo); Google.Apis.Drive.v2.Data.File file = _FileInfo._file; using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { file = connection.Service.Files.Get(file.Id).Execute(); if (file.Parents.Count > 0) { file.Parents.RemoveAt(0); } var parentReference = new ParentReference { Id = _ParentId }; file.Parents.Insert(0, parentReference); FilesResource.UpdateRequest request = connection.Service.Files.Update(file, FileId); file = request.Execute(); _FileInfo = GetFileInfo(file); FileInfoStatus newStatus = API.DriveService.GetFileInfoStatus(_FileInfo); if (currentStatus == FileInfoStatus.OnDisk) { DateTime modifiedDate = API.DriveService.GetDateTime(file.ModifiedDate); try { API.DriveService.SetFileLastWriteTime(_FileInfo.FilePath, modifiedDate); } catch { } _FileInfo = GetFileInfo(file); newStatus = API.DriveService.GetFileInfoStatus(_FileInfo); } DriveService_ProgressChanged(StatusType.Completed, null); } } } catch (Exception exception) { try { _Status = StatusType.Failed; _ExceptionMessage = exception.Message; DriveService_ProgressChanged(StatusType.Failed, exception); } catch { Debugger.Break(); } Log.Error(exception); } }
protected override void Start() { try { try { if (Status != StatusType.Queued) { throw new Exception("Stream has not been queued."); } base.Start(); _FileInfo = _DriveService.GetFile(FileId); if (_FileInfo == null) { throw new Exception("File '" + FileId + "' no longer exists."); } if (String.IsNullOrEmpty(_FileInfo.FilePath)) { throw new Exception("FileInfo.FilePath is blank."); } } catch (Exception exception) { Log.Error(exception); } try { if (_FileInfo.IsGoogleDoc) { try { API.DriveService.WriteGoogleDoc(_FileInfo); DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else if (String.IsNullOrEmpty(_FileInfo.DownloadUrl)) { try { DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else if (_FileInfo.FileSize == 0) { try { API.DriveService.CreateFile(FileInfo); DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } else { FileInfoStatus fileInfoStatus = API.DriveService.GetFileInfoStatus(_FileInfo); if (fileInfoStatus == FileInfoStatus.ModifiedOnDisk || fileInfoStatus == FileInfoStatus.OnDisk) { if (CheckIfAlreadyDownloaded) { try { DriveService_ProgressChanged(DownloadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } } } } catch (Exception exception) { Log.Error(exception); } try { API.DriveService.DeleteFile(_FileInfo.FilePath); _DownloadFilePath = _FileInfo.FilePath + ".download"; Lock(_DownloadFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); } catch (Exception exception) { Log.Error(exception); } try { _FileSize = API.DriveService.GetLong(_FileInfo.FileSize); _LastWriteTime = API.DriveService.GetFileLastWriteTime(_DownloadFilePath); } catch (Exception exception) { Log.Error(exception); } try { using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { var request = new MediaDownloader(connection.Service); request.ProgressChanged += DriveService_ProgressChanged; if (ChunkSize <= 0) { request.ChunkSize = API.DriveService.Settings.DownloadFileChunkSize; } else if (ChunkSize > MediaDownloader.MaximumChunkSize) { request.ChunkSize = MediaDownloader.MaximumChunkSize; } else { request.ChunkSize = ChunkSize; } _CancellationTokenSource = new System.Threading.CancellationTokenSource(); System.Threading.Tasks.Task <IDownloadProgress> task = request.DownloadAsync(_FileInfo.DownloadUrl, _FileStream, _CancellationTokenSource.Token); } } catch (Exception exception) { Log.Error(exception); } } catch (Exception exception) { try { _Status = StatusType.Failed; _ExceptionMessage = exception.Message; DriveService_ProgressChanged(DownloadStatus.Failed, 0, exception); } catch { Debugger.Break(); } Log.Error(exception); } }
protected FileInfo CopyFile(string parentId, FileInfo fileInfo) { try { FileInfoStatus currentStatus = API.DriveService.GetFileInfoStatus(fileInfo); File file = fileInfo._file; if (file.Parents.Count > 0) { file.Parents.RemoveAt(0); } var parentReference = new ParentReference { Id = parentId }; if (file.Parents == null) { file.Parents = new List <ParentReference>(); } file.Parents.Insert(0, parentReference); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { FilesResource.CopyRequest request = connection.Service.Files.Copy(file, file.Id); request.Fields = API.DriveService.RequestFields.FileFields; _CancellationTokenSource = new System.Threading.CancellationTokenSource(); file = request.Execute(); fileInfo = GetFileInfo(file); FileInfoStatus newStatus = API.DriveService.GetFileInfoStatus(fileInfo); if (currentStatus == FileInfoStatus.OnDisk) { DateTime modifiedDate = API.DriveService.GetDateTime(file.ModifiedDate); try { API.DriveService.SetFileLastWriteTime(fileInfo.FilePath, modifiedDate); } catch { } fileInfo = GetFileInfo(file); newStatus = API.DriveService.GetFileInfoStatus(fileInfo); } return(fileInfo); } } catch (Exception exception) { Log.Error(exception); 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); } }
protected override void Start() { try { if (Status != StatusType.Queued) { throw new Exception("Stream has not been queued."); } base.Start(); _FileInfo = _DriveService.GetFile(FileId); if (_FileInfo == null) { throw new Exception("File '" + FileId + "' no longer exists."); } if (String.IsNullOrEmpty(_FileInfo.FilePath)) { throw new Exception("FileInfo.FilePath is blank."); } if (!System.IO.File.Exists(_FileInfo.FilePath)) { throw new Exception("FileInfo.FilePath '" + _FileInfo.FilePath + "' does not exist."); } Lock(_FileInfo.FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); _FileSize = _FileStream.Length; _LastWriteTime = API.DriveService.GetFileLastWriteTime(_FileInfo.FilePath); if (_FileInfo.IsGoogleDoc) { DriveService_ProgressChanged(UploadStatus.Completed, FileSize, null); } else { FileInfoStatus fileInfoStatus = API.DriveService.GetFileInfoStatus(_FileInfo); if (fileInfoStatus != FileInfoStatus.ModifiedOnDisk) { if (CheckIfAlreadyUploaded) { try { DriveService_ProgressChanged(UploadStatus.Completed, 0, null); return; } catch (Exception exception) { Log.Error(exception); } } } using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { Google.Apis.Drive.v2.Data.File file = _FileInfo._file; file.ModifiedDate = _LastWriteTime.ToUniversalTime(); FilesResource.UpdateMediaUpload request = connection.Service.Files.Update(file, file.Id, _FileStream, file.MimeType); request.ProgressChanged += DriveService_ProgressChanged; request.ResponseReceived += DriveService_ResponseReceived; request.Fields = API.DriveService.RequestFields.FileFields; if (ChunkSize <= 0) { request.ChunkSize = FilesResource.UpdateMediaUpload.DefaultChunkSize; } else if (ChunkSize < FilesResource.UpdateMediaUpload.MinimumChunkSize) { request.ChunkSize = FilesResource.UpdateMediaUpload.MinimumChunkSize; } else { request.ChunkSize = ChunkSize; } request.Convert = _Parameters.Convert; request.NewRevision = true; request.Ocr = _Parameters.Ocr; request.OcrLanguage = _Parameters.OcrLanguage; request.Pinned = _Parameters.Pinned; request.SetModifiedDate = true; request.TimedTextLanguage = _Parameters.TimedTextLanguage; request.TimedTextTrackName = _Parameters.TimedTextTrackName; request.UpdateViewedDate = _Parameters.UpdateViewedDate; request.UseContentAsIndexableText = _Parameters.UseContentAsIndexableText; _CancellationTokenSource = new System.Threading.CancellationTokenSource(); System.Threading.Tasks.Task <IUploadProgress> task = request.UploadAsync(_CancellationTokenSource.Token); } } } catch (Exception exception) { try { _Status = StatusType.Failed; _ExceptionMessage = exception.Message; DriveService_ProgressChanged(UploadStatus.Failed, 0, exception); } catch { Debugger.Break(); } Log.Error(exception); } }