Ejemplo n.º 1
0
        public void SyncFolder(SourceFolder sourceFolder, GoogleFolder remoteFolder)
        {
            if (sourceFolder.Files != null)
            {
                foreach (var file in sourceFolder.Files)
                {
                    if (!_googleDriveService.DoesFileExistInFolder(remoteFolder, file.FileName))
                    {
                        try
                        {
                            var remoteFile = _googleDriveService.UploadFile(remoteFolder, file.FileName, file.FullLocation, file.MimeType);
                            _log.Info("File {0} uploaded, google id: {1}", file.FullLocation, remoteFile.Id);
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex);
                        }
                    }
                    else
                    {
                        _log.Info("File {0} already exists", file.FullLocation);
                    }
                }
            }
            if (sourceFolder.Folders != null)
            {
                foreach (var childFolder in sourceFolder.Folders)
                {
                    if (_googleDriveService.DoesFolderExistInFolder(remoteFolder, childFolder.FolderName))
                    {
                        try
                        {
                            var childRemoteFolder = _googleDriveService.CreateFolder(remoteFolder, childFolder.FolderName);
                            _log.Info("Folder {0} created, google id: {1}", childFolder.FullLocation, childRemoteFolder.Id);

                            SyncFolder(childFolder, childRemoteFolder);
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex);
                        }
                    }
                    else
                    {
                        _log.Info("Folder {0} already exists", childFolder.FullLocation);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public FolderDto CreateFolder(string folderName, string description)
        {
            FolderDto response = null;

            try
            {
                var service = _googleDriveOperation.GetDriveService();
                response = _googleDriveOperation.CreateFolder(service, folderName, description);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.StackTrace);
            }
            return(response);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> ListImages()
        {
            Response.Headers.Add("Cache-Control", "no-cache");
            string newFolderId;
            string doneFolderId;
            string errorFolderId;
            string importFolderId;
            string accountFolderId;
            string imagesFolderId;
            string accountName = this._httpContextAccessor.HttpContext.Request.Headers[DriveImportConstants.VTEX_ACCOUNT_HEADER_NAME];

            FolderIds folderIds = await _driveImportRepository.LoadFolderIds(accountName);

            if (folderIds != null)
            {
                accountFolderId = folderIds.AccountFolderId;
                imagesFolderId  = folderIds.ImagesFolderId;
                newFolderId     = folderIds.NewFolderId;
                doneFolderId    = folderIds.DoneFolderId;
                errorFolderId   = folderIds.ErrorFolderId;
            }
            else
            {
                Dictionary <string, string> folders = await _googleDriveService.ListFolders();   // Id, Name

                if (!folders.ContainsValue(DriveImportConstants.FolderNames.IMPORT))
                {
                    importFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.IMPORT);
                }
                else
                {
                    importFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.IMPORT).Key;
                }

                if (string.IsNullOrEmpty(importFolderId))
                {
                    _context.Vtex.Logger.Info("DriveImport", null, $"Could not find '{DriveImportConstants.FolderNames.IMPORT}' folder");
                    return(Json($"Could not find {DriveImportConstants.FolderNames.IMPORT} folder"));
                }

                folders = await _googleDriveService.ListFolders(importFolderId);

                if (!folders.ContainsValue(accountName))
                {
                    accountFolderId = await _googleDriveService.CreateFolder(accountName, importFolderId);
                }
                else
                {
                    accountFolderId = folders.FirstOrDefault(x => x.Value == accountName).Key;
                }

                if (string.IsNullOrEmpty(accountFolderId))
                {
                    _context.Vtex.Logger.Info("DriveImport", null, $"Could not find {accountFolderId} folder");
                    return(Json($"Could not find {accountFolderId} folder"));
                }

                folders = await _googleDriveService.ListFolders(accountFolderId);

                if (!folders.ContainsValue(DriveImportConstants.FolderNames.IMAGES))
                {
                    imagesFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.IMAGES, accountFolderId);
                }
                else
                {
                    imagesFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.IMAGES).Key;
                }

                if (string.IsNullOrEmpty(imagesFolderId))
                {
                    _context.Vtex.Logger.Info("DriveImport", null, $"Could not find {imagesFolderId} folder");
                    return(Json($"Could not find {imagesFolderId} folder"));
                }

                folders = await _googleDriveService.ListFolders(imagesFolderId);

                if (!folders.ContainsValue(DriveImportConstants.FolderNames.NEW))
                {
                    newFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.NEW, imagesFolderId);

                    bool setPermission = await _googleDriveService.SetPermission(newFolderId);

                    if (!setPermission)
                    {
                        _context.Vtex.Logger.Error("DriveImport", "SetPermission", $"Could not set permissions on '{DriveImportConstants.FolderNames.NEW}' folder {newFolderId}");
                    }
                }
                else
                {
                    newFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.NEW).Key;
                }

                if (!folders.ContainsValue(DriveImportConstants.FolderNames.DONE))
                {
                    doneFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.DONE, imagesFolderId);
                }
                else
                {
                    doneFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.DONE).Key;
                }

                if (!folders.ContainsValue(DriveImportConstants.FolderNames.ERROR))
                {
                    errorFolderId = await _googleDriveService.CreateFolder(DriveImportConstants.FolderNames.ERROR, imagesFolderId);
                }
                else
                {
                    errorFolderId = folders.FirstOrDefault(x => x.Value == DriveImportConstants.FolderNames.ERROR).Key;
                }
            }

            if (folderIds == null)
            {
                folderIds = new FolderIds
                {
                    AccountFolderId = accountFolderId,
                    DoneFolderId    = doneFolderId,
                    ErrorFolderId   = errorFolderId,
                    ImagesFolderId  = imagesFolderId,
                    ImportFolderId  = imagesFolderId,
                    NewFolderId     = newFolderId
                };

                _driveImportRepository.SaveFolderIds(folderIds, accountName);
            }

            Dictionary <string, string> images     = new Dictionary <string, string>();
            ListFilesResponse           imageFiles = new ListFilesResponse();

            imageFiles.Files = new List <GoogleFile>();
            string nextPageToken = string.Empty;

            do
            {
                ListFilesResponse listFilesResponse = await _googleDriveService.ListImagesInFolder(newFolderId, nextPageToken);

                imageFiles.Files.AddRange(listFilesResponse.Files);
                nextPageToken = listFilesResponse.NextPageToken;
            } while (!string.IsNullOrEmpty(nextPageToken));

            return(Json(imageFiles));
        }