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(); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { Google.Apis.Drive.v2.Data.File file = _FileInfo._file; file = connection.Service.Files.Get(file.Id).Execute(); file.Title = Title; if (!String.IsNullOrEmpty(MimeType)) { file.MimeType = MimeType; } FilesResource.UpdateRequest request = connection.Service.Files.Update(file, FileId); file = request.Execute(); _FileInfo = GetFileInfo(file); 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 { 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(); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { FilesResource.TrashRequest request = connection.Service.Files.Trash(FileId); request.Fields = API.DriveService.RequestFields.FileFields; _CancellationTokenSource = new System.Threading.CancellationTokenSource(); System.Threading.Tasks.Task <File> task = request.ExecuteAsync(_CancellationTokenSource.Token); if (task.IsCompleted) { DriveService_ProgressChanged(task); } else { task.ContinueWith(thisTask => DriveService_ProgressChanged(thisTask)); } } } 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 { 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 { if (Status != StatusType.Queued) { throw new Exception("Stream has not been queued."); } base.Start(); List <File> children = null; File parent = API.DriveService._GetCachedFile(_parentId, true, false, ref children); string fileName = System.IO.Path.GetFileNameWithoutExtension(_originalTitle); string fileExtenstion = System.IO.Path.GetExtension(_originalTitle); string title = fileName; if (!String.IsNullOrEmpty(fileExtenstion)) { title += fileExtenstion; } int index = 0; while (true) { bool found = false; foreach (File child in children) { if (child.Title == title) { FileInfoType fileInfoType = API.DriveService.GetFileInfoType(child); if (IsFolder && fileInfoType == FileInfoType.Folder) { found = true; } else if (!IsFolder && fileInfoType != FileInfoType.Folder) { found = true; } } } if (!found) { break; } index++; title = fileName + " (" + index + ")"; if (!String.IsNullOrEmpty(fileExtenstion)) { title += fileExtenstion; } } _title = title; var file = new File(); var parentReference = new ParentReference { Id = _parentId }; file.Parents = new List <ParentReference> { parentReference }; file.Title = _title; file.Description = "Created by Drive Fusion Shell Extention"; file.FileExtension = System.IO.Path.GetExtension(_title); file.MimeType = _mimeType; file.ModifiedDate = _lastWriteTime.ToUniversalTime(); _FileInfo = API.DriveService.GetFileInfo(file); if (_FileInfo.IsFolder) { Lock(); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { FilesResource.InsertRequest request = connection.Service.Files.Insert(file); request.Fields = API.DriveService.RequestFields.FileFields; file = request.Execute(); _FileId = file.Id; _FileInfo = API.DriveService.GetFileInfo(file); DriveService_ProgressChanged(UploadStatus.Completed, 0, null); } } else { Lock(_filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { FilesResource.InsertMediaUpload request = connection.Service.Files.Insert(file, _FileStream, _mimeType); request.Fields = API.DriveService.RequestFields.FileFields; request.ProgressChanged += DriveService_ProgressChanged; request.ResponseReceived += DriveService_ResponseReceived; request.ChunkSize = FilesResource.InsertMediaUpload.DefaultChunkSize; request.Pinned = _parameters.Pinned; 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); } }
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); } }
protected void DriveService_ProgressChanged(IUploadProgress uploadProgress) { try { var status = StatusType.NotStarted; long bytesProcessed = uploadProgress.BytesSent; long totalBytes = FileSize; Exception processException = uploadProgress.Exception; if (uploadProgress.Status == UploadStatus.Completed) { status = StatusType.Completed; } else if (uploadProgress.Status == UploadStatus.Uploading) { status = StatusType.Processing; } else if (uploadProgress.Status == UploadStatus.Failed) { status = StatusType.Failed; } else { status = StatusType.Starting; } UpdateProgress(status, bytesProcessed, totalBytes, processException); if (Processed) { try { if (Completed) { _LastWriteTime = API.DriveService.GetFileLastWriteTime(FilePath); DateTime modifiedDate = _FileInfo.ModifiedDate; if (!API.DriveService.IsDateTimeEqual(_LastWriteTime, modifiedDate)) { try { _LastWriteTime = API.DriveService.SetFileLastWriteTime(FilePath, modifiedDate); } catch (Exception exception) { Log.Error(exception, false, false); System.Threading.Thread.Sleep(500); } } if (!API.DriveService.IsDateTimeEqual(_LastWriteTime, modifiedDate)) { try { using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { File file = _FileInfo._file; file.ModifiedDate = _LastWriteTime.ToUniversalTime(); Google.Apis.Drive.v2.FilesResource.UpdateRequest updateRequest = connection.Service.Files.Update(file, file.Id); updateRequest.SetModifiedDate = true; _FileInfo._file = updateRequest.Execute(); modifiedDate = _FileInfo.ModifiedDate; } } catch (Exception exception) { Log.Error(exception, false, false); System.Threading.Thread.Sleep(500); } } if (!API.DriveService.IsDateTimeEqual(_LastWriteTime, modifiedDate)) { try { using (API.DriveService.Connection connection = API.DriveService.Connection.Create()) { File file = _FileInfo._file; file.ModifiedDate = _LastWriteTime.ToUniversalTime(); Google.Apis.Drive.v2.FilesResource.PatchRequest patchRequest = connection.Service.Files.Patch( file, file .Id); patchRequest.SetModifiedDate = true; _FileInfo._file = patchRequest.Execute(); modifiedDate = _FileInfo.ModifiedDate; } } catch (Exception exception) { Log.Error(exception, false, false); System.Threading.Thread.Sleep(500); } } if (!API.DriveService.IsDateTimeEqual(_LastWriteTime, modifiedDate)) { // hopefully at least this will keep the upload process from firing off _FileInfo._file.ModifiedDate = _LastWriteTime.ToUniversalTime(); } } } catch (Exception exception) { Log.Error(exception, false); if (!Failed) { _ExceptionMessage = exception.Message; _Status = StatusType.Failed; } } } InvokeOnProgressEvent(); } 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); } }