public FileInfo Invoke(string fileId, string title, string mimeType = null)
        {
            try
            {
                if (String.IsNullOrEmpty(fileId))
                {
                    throw new Exception("Input parameter 'fileId' is null or empty (Cannot rename root directory).");
                }

                if (String.IsNullOrEmpty(title))
                {
                    throw new Exception("Input parameter 'title' is null or empty.");
                }

                using (DriveService driveService = DriveService.Create())
                {
                    FileInfo fileInfo = driveService.GetFile(fileId);

                    if (fileInfo == null)
                    {
                        throw new Exception("File no longer exists.");
                    }

                    var renameStream = new API.DriveService.RenameStream();

                    renameStream.Init(fileInfo, title, mimeType);

                    renameStream.Visible = false;

                    driveService.RenameFile(renameStream);

                    while (!renameStream.Finished)
                    {
                        System.Threading.Thread.Sleep(250);
                    }

                    if (renameStream.Cancelled)
                    {
                        return(null);
                    }
                    if (renameStream.Failed)
                    {
                        throw new LogException("Rename file could not complete - " + renameStream.ExceptionMessage, true, true);
                    }

                    fileInfo = renameStream.FileInfo;

                    return(fileInfo);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);

                return(null);
            }
        }
Exemplo n.º 2
0
        public FileInfo Invoke(string parentId, string title, string mimeType = null)
        {
            try
            {
                if (String.IsNullOrEmpty(title))
                {
                    throw new Exception("Input parameter 'title' is null or empty.");
                }

                using (DriveService driveService = DriveService.Create())
                {
                    FileInfo parentInfo = driveService.GetFile(parentId);

                    if (parentInfo == null)
                    {
                        throw new Exception("Parent no longer exists.");
                    }

                    var insertStream = new API.DriveService.InsertStream();

                    insertStream.Init(parentInfo.Id, title, mimeType);

                    insertStream.Visible = false;

                    driveService.InsertFile(insertStream);

                    while (!insertStream.Finished)
                    {
                        System.Threading.Thread.Sleep(250);
                    }

                    if (insertStream.Cancelled)
                    {
                        return(null);
                    }
                    if (insertStream.Failed)
                    {
                        throw new LogException("Insert file could not complete - " + insertStream.ExceptionMessage, true, true);
                    }

                    FileInfo fileInfo = insertStream.FileInfo;

                    return(fileInfo);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);

                return(null);
            }
        }
Exemplo n.º 3
0
        public FileInfo Invoke(string fileId)
        {
            try
            {
                if (String.IsNullOrEmpty(fileId))
                {
                    throw new Exception("Input parameter 'fileId' is null or empty (Cannot upload root directory).");
                }

                using (DriveService driveService = DriveService.Create())
                {
                    FileInfo fileInfo = driveService.GetFile(fileId);

                    if (fileInfo == null)
                    {
                        throw new Exception("File no longer exists.");
                    }

                    var uploadStream = new API.DriveService.UploadStream();

                    uploadStream.Init(fileInfo, null);

                    driveService.UploadFile(uploadStream);

                    while (!uploadStream.Finished)
                    {
                        System.Threading.Thread.Sleep(250);
                    }

                    if (uploadStream.Cancelled)
                    {
                        return(null);
                    }
                    if (uploadStream.Failed)
                    {
                        throw new LogException("Upload could not complete - " + uploadStream.ExceptionMessage, true, true);
                    }

                    fileInfo = uploadStream.FileInfo;

                    return(fileInfo);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);

                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);
                }
            }
            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);
                }
            }
Exemplo n.º 6
0
        public List <string> Invoke(List <string> fileIds)
        {
            try
            {
                using (DriveService driveService = DriveService.Create())
                {
                    var untrashStreams = new List <API.DriveService.UntrashStream>();

                    foreach (string fileId in fileIds)
                    {
                        FileInfo fileInfo = driveService.GetFile(fileId);

                        if (fileInfo == null)
                        {
                            throw new Exception("File no longer exists.");
                        }

                        var untrashStream = new API.DriveService.UntrashStream();

                        untrashStream.Init(fileInfo);

                        untrashStream.Visible = false;

                        untrashStreams.Add(untrashStream);
                    }

                    driveService.UntrashFiles(untrashStreams);

                    while (true)
                    {
                        bool finished = true;

                        for (int i = 0; i < untrashStreams.Count; i++)
                        {
                            API.DriveService.UntrashStream untrashStream = untrashStreams[i];

                            if (!untrashStream.Finished)
                            {
                                finished = false;
                                break;
                            }
                        }

                        if (finished)
                        {
                            break;
                        }

                        System.Threading.Thread.Sleep(250);
                    }

                    var untrashedFileIds = new List <string>();

                    foreach (DriveService.UntrashStream untrashStream in untrashStreams)
                    {
                        if (untrashStream.Completed)
                        {
                            untrashedFileIds.Add(untrashStream.FileId);
                        }
                        else if (untrashStream.Cancelled)
                        {
                        }
                        else if (untrashStream.Failed)
                        {
                            throw new LogException("Untrash could not complete - " + untrashStream.ExceptionMessage, true, true);
                        }
                    }

                    return(untrashedFileIds);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);

                return(null);
            }
        }
Exemplo n.º 7
0
        public List <string> Invoke(string parentId, List <string> fileIds)
        {
            try
            {
                using (DriveService driveService = DriveService.Create())
                {
                    FileInfo parent = driveService.GetFile(parentId);

                    if (parent == null)
                    {
                        throw new Exception("Parent no longer exists.");
                    }

                    var moveStreams = new List <DriveProxy.API.DriveService.MoveStream>();

                    foreach (string fileId in fileIds)
                    {
                        FileInfo fileInfo = driveService.GetFile(fileId);

                        if (fileInfo == null)
                        {
                            throw new Exception("File no longer exists.");
                        }

                        var moveStream = new DriveProxy.API.DriveService.MoveStream();

                        moveStream.Init(parent.Id, fileInfo);

                        moveStreams.Add(moveStream);
                    }

                    driveService.MoveFiles(moveStreams);

                    while (true)
                    {
                        bool finished = true;

                        for (int i = 0; i < moveStreams.Count; i++)
                        {
                            DriveProxy.API.DriveService.MoveStream moveStream = moveStreams[i];

                            if (!moveStream.Finished)
                            {
                                finished = false;
                                break;
                            }
                        }

                        if (finished)
                        {
                            break;
                        }

                        System.Threading.Thread.Sleep(250);
                    }

                    var movedFileIds = new List <string>();

                    foreach (DriveService.MoveStream moveStream in moveStreams)
                    {
                        if (moveStream.Completed)
                        {
                            movedFileIds.Add(moveStream.FileId);
                        }
                        else if (moveStream.Cancelled)
                        {
                        }
                        else if (moveStream.Failed)
                        {
                            throw new LogException("Move could not complete - " + moveStream.ExceptionMessage, true, true);
                        }
                    }

                    return(movedFileIds);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);

                return(null);
            }
        }
Exemplo n.º 8
0
            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);
                }
            }