Esempio n. 1
0
        public static void AutoImport(string[] args, DriveHelper driveHelper)
        {
            ContentElement contentElement = null;
            ContentType    contentType    = null;

            importOptions = new ImportOptions(args);
            bool hasError = false;

            try
            {
                List <Google.Apis.Drive.v3.Data.File> filestoimport;
                if (importOptions.directory != null)
                {
                    filestoimport = driveHelper.GetFilesInDirectory(importOptions.directory);
                }
                else
                {
                    var file = driveHelper.GetSingleFile(importOptions.sourceFileName);
                    filestoimport = new List <Google.Apis.Drive.v3.Data.File>();
                    filestoimport.Add(file);
                }

                // Get content type
                var task = clientDelivery.GetTypeAsync(importOptions.contentTypeName);
                contentType = task.GetAwaiter().GetResult();

                // Obtain target element from content type. Skip for spreadsheets
                bool allSpreadsheets = true;
                foreach (var f in filestoimport)
                {
                    if (!DriveHelper.IsSpreadsheet(f))
                    {
                        allSpreadsheets = false;
                        break;
                    }
                }
                if (!allSpreadsheets)
                {
                    contentElement = GetElementFromType(contentType, importOptions.targetElementName);
                }

                //Import file(s)
                foreach (var file in filestoimport)
                {
                    var stream = driveHelper.DownloadFile(file);
                    if (stream != null)
                    {
                        BeginImport(stream, file, contentElement, contentType, importOptions.update);
                    }
                }
            }
            catch (Exception e)
            {
                hasError = true;
                Program.ShowError(e.Message);
            }

            Console.WriteLine(hasError ? "Import not complete, please check error messages" : "Import complete");
            Console.ReadKey();
        }
Esempio n. 2
0
        /// <summary>
        /// Processes MemoryStream and upserts data. If the file is a spreadsheet, processes the sheet into CSV and upserts individual rows. If the file is an asset, creates new asset.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="file"></param>
        /// <param name="element"></param>
        /// <param name="type"></param>
        /// <param name="update"></param>
        public static void BeginImport(MemoryStream stream, Google.Apis.Drive.v3.Data.File file, ContentElement element, ContentType type, bool update)
        {
            string result = "";

            if (!DriveHelper.IsAsset(file))
            {
                // Read stream to string for content imports only
                stream.Seek(0, SeekOrigin.Begin);
                var sr = new StreamReader(stream);
                result = sr.ReadToEnd();
            }

            string importtype = DriveHelper.IsAsset(file) ? "Asset" : "Content";

            Program.WriteColoredText($"Download success, importing {importtype.ToLower()}..", ConsoleColor.Green);

            if (DriveHelper.IsSpreadsheet(file))
            {
                // Import multiple items
                string[] rows           = result.Split("\r\n");
                string[] headers        = rows[0].Split(',');
                int      codenamecolumn = Array.IndexOf(headers, SPREADSHEET_CODENAME_HEADER);
                for (var i = 1; i < rows.Length; i++)
                {
                    string[] rowdata = rows[i].Split(',');
                    if (rowdata.Length > headers.Length)
                    {
                        Program.ShowError($"Inconsistent data for row {i}. This could be because the row contains a comma, which is not currently supported. Skipping row.");
                        continue;
                    }
                    UpsertRowData(rowdata, headers, codenamecolumn, type, update);
                }
            }
            else if (DriveHelper.IsAsset(file))
            {
                stream.Position = 0;
                UpsertAsset(file, update, stream);
            }
            else
            {
                // Import single item
                Dictionary <string, object> elements = null;
                if (element != null)
                {
                    elements = new Dictionary <string, object>()
                    {
                        { element.Codename, result }
                    };
                }

                UpsertSingleItem(elements, file.Name, type, update);
            }
        }
Esempio n. 3
0
 static bool CanImport(Google.Apis.Drive.v3.Data.File file, ContentElement ele, ContentType type)
 {
     if (DriveHelper.IsSpreadsheet(file))
     {
         // Target element isn't needed for importing spreadsheets
         return(file != null && type != null);
     }
     else if (DriveHelper.IsAsset(file))
     {
         // Nothing needed for importing assets
         return(true);
     }
     else
     {
         return(ele != null && file != null && type != null);
     }
 }
Esempio n. 4
0
        static bool Init()
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            try
            {
                KontentConfig kontentConfig = LoadKontentConfiguration(ConfigFile);
                InitKontent(kontentConfig);

                UserCredential credential = LoadGoogleDriveCredentials();
                driveHelper = new DriveHelper(credential, AppName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
                return(false);
            }

            return(true);
        }
Esempio n. 5
0
        public static void Run()
        {
            ContentType    targetType    = null;
            ContentElement targetElement = null;

            Google.Apis.Drive.v3.Data.File sourceFile = null;
            bool update   = false;
            bool hasError = false;

            var files = driveHelper.ListFiles();

            if (files != null && files.Count > 0)
            {
                sourceFile = VerifySourceFile(files);
            }
            else
            {
                Program.ShowError("No files found.");
                Console.ReadKey();
                return;
            }

            if (!DriveHelper.IsAsset(sourceFile))
            {
                var types = KontentHelper.ListContentTypes();
                targetType = VerifyContentType(types);

                // If imported file is not a spreadsheet, get target element
                if (!DriveHelper.IsSpreadsheet(sourceFile))
                {
                    targetElement = VerifyContentElement(targetType);
                }

                update = AskUpdate();
            }

            // Verify data and begin import
            Console.WriteLine();
            if (!CanImport(sourceFile, targetElement, targetType))
            {
                ShowError("Something went wrong.. Please press any key to close.");
                Console.ReadKey();
                return;
            }
            else
            {
                try
                {
                    var stream = driveHelper.DownloadFile(sourceFile);
                    if (stream != null)
                    {
                        KontentHelper.BeginImport(stream, sourceFile, targetElement, targetType, update);
                    }
                }
                catch (Exception e)
                {
                    hasError = true;
                    ShowError(e.Message);
                }
                finally
                {
                    Console.WriteLine(hasError ? "Import completed with error(s), please check error messages" : "Import complete");
                    // Ask if user wants to run import again
                    Console.WriteLine("Do you want to perform another import? [Enter = Yes, Any other key = Quit]");
                    if (Console.ReadKey().Key == ConsoleKey.Enter)
                    {
                        Run();
                    }
                }
            }
        }