Пример #1
0
        //TODO: Rename to account controller

        public AccountController(IOptions <AegisOptions> openIdOptions,
                                 IOptions <ServerOptions> serverOptions,
                                 IOptions <DriveOptions> driveOptions,
                                 IFolderManager <FileDocument> folderManager,
                                 IDocumentService <FileDocument> documentService,
                                 ILogger <AccountController> logger,
                                 TagManager tags,
                                 MenuProvider menuProvider,
                                 UserGroupManager userGroupManager,
                                 SiteContextAccessor siteContextAccessor,
                                 UserManager userManager,
                                 PageMasterManager masterPageManager,
                                 UserContextAccessor userContextAccessor,
                                 SecurityPoolManager poolManager,
                                 GroupManager groupManager) : base(logger)
        {
            _oidConfig           = openIdOptions.Value;
            _serverOptions       = serverOptions.Value;
            _driveOptions        = driveOptions.Value;
            _tags                = tags;
            _menuProvider        = menuProvider;
            _folderManager       = folderManager;
            _documentService     = documentService;
            _userGroupManager    = userGroupManager;
            _userManager         = userManager;
            _poolManager         = poolManager;
            _userContextAccessor = userContextAccessor;
            _siteContextAccessor = siteContextAccessor;
            _masterPageManager   = masterPageManager;
            _groupManager        = groupManager;
        }
Пример #2
0
 public LibraryDataController(
     IFolderManager <FileDocument> folderManager,
     IDocumentService <FileDocument> documentService,
     IOptions <DriveOptions> driveOptions,
     TagManager tagManager,
     DbLoggerProvider log,
     DbLogService logFetcher,
     ILogger <LibraryDataController> logger,
     ResourceManager resourceManager,
     FileFolderBrowser fileFolderBrowser,
     IAuthorizationService authorizationService,
     IContextAccessor <AdminContext> adminContextAccessor,
     IContextAccessor <SiteContext> siteContextAccessor) : base(logger)
 {
     _folderManager        = folderManager;
     _documentService      = documentService;
     _driveOptions         = driveOptions.Value;
     _tagManager           = tagManager;
     _log                  = log;
     _logFetcher           = logFetcher;
     _resourceManager      = resourceManager;
     _fileFolderBrowser    = fileFolderBrowser;
     _authorizationService = authorizationService;
     _adminContext         = adminContextAccessor.GetContext();
     _siteContext          = siteContextAccessor.GetContext();
 }
Пример #3
0
        /// <summary>
        /// This method recursively deletes a Google Drive directory and it's contents if they cannot be found in either the file or directory listing
        /// </summary>
        /// <param name="drop">Drive options of previous query</param>
        /// <param name="fileDescList"></param>
        /// <param name="subDirectories"></param>
        private async void RemoveExtras(DriveOptions drop, List <FileDescription> fileDescList, List <DirectoryDescription> subDirectories)
        {
            FilesResource.ListRequest list = driveService.Files.List();
            FileList filesFeed             = null;

            list.MaxResults = 1000;
            list.Q          = GetDriveQuery(drop);

            filesFeed = await list.ExecuteAsync();

            while (filesFeed.Items != null)
            {
                // Deleting files that shouldn't exist anymore
                List <string> names = new List <string>();

                if (fileDescList != null)
                {
                    foreach (var item in fileDescList) // Add names of files
                    {
                        names.Add(item.Name);
                    }
                }

                if (subDirectories != null)
                {
                    foreach (var item in subDirectories) // Add names of subdirectories
                    {
                        names.Add(item.Name);
                    }
                }

                var files = filesFeed.Items;

                foreach (var item in files)
                {
                    if (!names.Contains(item.Title))
                    {
                        // Recursively delete subdirectories
                        if (item.MimeType == "application/vnd.google-apps.folder")
                        {
                            RemoveExtras(new DriveOptions {
                                Parent = item.Id
                            }, null, null);
                        }
                        await driveService.Files.Delete(item.Id).ExecuteAsync();
                    }
                }

                if (filesFeed.NextPageToken == null)
                {
                    break;
                }

                list.PageToken = filesFeed.NextPageToken;
                filesFeed      = await list.ExecuteAsync();
            }
        }
Пример #4
0
        public DocumentDetails(IFolderManager <FileDocument> folderManager,
                               IDocumentService <FileDocument> documentService,
                               IOptions <DriveOptions> driveOptions,
                               UserManager userManager,
                               DbLoggerProvider log)
        {
            Ensure.NotNull(folderManager);
            Ensure.NotNull(documentService);
            Ensure.NotNull(driveOptions);

            this._folderManager   = folderManager;
            this._documentService = documentService;
            this._driveOptions    = driveOptions.Value;
            this._log             = log;
            _userManager          = userManager;
        }
Пример #5
0
 public DocumentPickerController(
     IFolderManager <FileDocument> folderManager,
     IDocumentService <FileDocument> documentService,
     IOptions <DriveOptions> driveOptions,
     TagManager tagManager,
     DbLoggerProvider log,
     DbLogService logFetcher,
     ILogger <DocumentPickerController> logger) : base()
 {
     _folderManager   = folderManager;
     _documentService = documentService;
     _driveOptions    = driveOptions.Value;
     _tagManager      = tagManager;
     _log             = log;
     _logFetcher      = logFetcher;
     _logger          = logger;
 }
Пример #6
0
        private static string GetDriveQuery(DriveOptions drop)
        {
            string rc = ("trashed = false");

            if (!string.IsNullOrEmpty(drop.Title))
            {
                rc += string.Concat(" and title = '", drop.Title, "'");
            }

            if (!string.IsNullOrEmpty(drop.MimeType))
            {
                rc += string.Concat(" and mimeType = '", drop.MimeType, "'");
            }

            if (!string.IsNullOrEmpty(drop.Parent))
            {
                rc += string.Concat(" and '", drop.Parent, "' in parents");
            }

            return(rc);
        }
Пример #7
0
 public AssignmentsWidgetService(AssignmentsDbContext assignmentsDb, ConnectDbContext connectDb, IOptions <DriveOptions> driveOptions)
 {
     _assignmentsDb = assignmentsDb;
     _connectDb     = connectDb;
     _driveOptions  = driveOptions.Value;
 }
Пример #8
0
 public VideoWidgetService(VideoDbContext videoDb, ConnectDbContext connectDb, IOptions <DriveOptions> driveOptions)
 {
     _videoDb      = videoDb;
     _connectDb    = connectDb;
     _driveOptions = driveOptions.Value;
 }
Пример #9
0
        public async Task Fetch(Path source, bool copySubDirs, Dictionary <FileDescription, byte[]> fileDescToBytesMapping, List <DirectoryDescription> subDirectories)
        {
            if (driveService == null)
            {
                throw new Exception("Google drive provider is not authenticated");
            }

            DriveOptions drop = new DriveOptions();

            try
            {
                FilesResource.ListRequest list = driveService.Files.List();
                FileList filesFeed             = null;
                list.MaxResults = 1000;
                var splitArr = source.AbsolutePath.Split('\\');
                drop.Title    = splitArr == null ? source.AbsolutePath : splitArr.Last();
                drop.Parent   = source.Parent;
                drop.MimeType = "application/vnd.google-apps.folder";
                list.Q        = GetDriveQuery(drop);
                filesFeed     = await list.ExecuteAsync();

                string parentId = filesFeed.Items[0].Id;
                list.Q = GetDriveQuery(new DriveOptions {
                    Parent = parentId
                });

                filesFeed = await list.ExecuteAsync();

                while (filesFeed.Items != null)
                {
                    foreach (var file in filesFeed.Items)
                    {
                        if (file.MimeType != "application/vnd.google-apps.folder")
                        {
                            byte[]          bytes = null;
                            FileDescription fd    = new FileDescription();
                            try
                            {
                                fd.Name          = file.Title;
                                fd.LastWriteTime = file.ModifiedDate ?? DateTime.MinValue;
                                fd.MimeType      = file.MimeType;
                                fd.Id            = file.Id;
                                bytes            = driveService.HttpClient.GetByteArrayAsync(file.DownloadUrl).Result;
                                fileDescToBytesMapping.Add(fd, bytes);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        else
                        {
                            if (copySubDirs)
                            {
                                subDirectories.Add(new DirectoryDescription()
                                {
                                    SourceParentId = parentId, Name = file.Title
                                });
                            }
                        }
                    }

                    if (filesFeed.NextPageToken == null)
                    {
                        break;
                    }

                    list.PageToken = filesFeed.NextPageToken;
                    filesFeed      = await list.ExecuteAsync();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Пример #10
0
        public async Task Dispatch(Path destination, bool copySubDirs, Dictionary <FileDescription, byte[]> fileDescToBytesMapping, List <DirectoryDescription> subDirectories)
        {
            if (driveService == null)
            {
                throw new Exception("Google drive provider is not authenticated");
            }

            DriveOptions drop = new DriveOptions();

            try
            {
                FilesResource.ListRequest list      = driveService.Files.List();
                FileList filesFeed                  = null;
                Google.Apis.Drive.v2.Data.File gDir = null;

                if (destination.AbsolutePath != "") // Destination is not root
                {
                    // Get directory
                    list.MaxResults = 1000;
                    drop.Parent     = string.IsNullOrEmpty(destination.Parent) ? "root" : destination.Parent;
                    list.Q          = GetDriveQuery(drop);

                    filesFeed = await list.ExecuteAsync();

                    // Try and locate the folder in google drive if it already exists
                    while (filesFeed.Items != null)
                    {
                        gDir = filesFeed.Items.SingleOrDefault(x => x.MimeType == "application/vnd.google-apps.folder" && x.Title == destination.AbsolutePath.Split('\\').Last());

                        if (filesFeed.NextPageToken == null)
                        {
                            break;                                  // Break if there are no more pages and the directory has not been found
                        }
                        list.PageToken = filesFeed.NextPageToken;
                        filesFeed      = await list.ExecuteAsync();
                    }

                    // If the destination directory doesn't exist, create it.
                    if (gDir == null)
                    {
                        // Create directory
                        Google.Apis.Drive.v2.Data.File file = new Google.Apis.Drive.v2.Data.File();
                        file.Title    = destination.AbsolutePath.Split('\\').Last();
                        file.MimeType = "application/vnd.google-apps.folder";
                        file.Parents  = new List <ParentReference>();
                        ParentReference pr = new ParentReference()
                        {
                            Id = drop.Parent, IsRoot = drop.Parent == "root"
                        };
                        file.Parents.Add(pr);

                        gDir = await driveService.Files.Insert(file).ExecuteAsync();
                    }

                    drop.Parent = gDir.Id;
                }
                else // Destination is root
                {
                    drop.Parent = "root";
                }

                // Remove all files/folders(and their contents) that don't exist in the source
                RemoveExtras(drop, fileDescToBytesMapping.Keys.ToList(), subDirectories);

                // Update/upload files
                foreach (var item in fileDescToBytesMapping)
                {
                    MemoryStream stream = new MemoryStream(item.Value);

                    list.Q = GetDriveQuery(new DriveOptions {
                        Title = item.Key.Name, MimeType = item.Key.MimeType, Parent = gDir.Id
                    });
                    var gFile = list.Execute().Items.Count > 0 ? list.Execute().Items[0] : null;
                    if (gFile == null)
                    {
                        if (item.Key.MimeType != "application/vnd.google-apps.folder")
                        {
                            // Create files
                            Google.Apis.Drive.v2.Data.File f = new Google.Apis.Drive.v2.Data.File();

                            f.MimeType = item.Key.MimeType;
                            f.Title    = item.Key.Name;
                            f.Parents  = new List <ParentReference>();
                            ParentReference pr = new ParentReference()
                            {
                                Id = gDir.Id
                            };

                            if (drop.Parent == "root")
                            {
                                pr.IsRoot = true;
                            }
                            else
                            {
                                pr.IsRoot = false;
                            }

                            f.Parents.Add(pr);
                            var status = await driveService.Files.Insert(f, stream, f.MimeType).UploadAsync();
                        }
                    }
                    else
                    {
                        // Update files
                        if (item.Key.LastWriteTime > gFile.ModifiedDate)
                        {
                            var status = await driveService.Files.Update(gFile, gFile.Id, stream, gFile.MimeType).UploadAsync();
                        }
                    }
                }

                if (copySubDirs)
                {
                    foreach (var item in subDirectories)
                    {
                        item.DestinationParentId = gDir.Id;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }