Example #1
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);
            }
        }
Example #2
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);
     }
 }
Example #3
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();
                    }
                }
            }
        }