Exemplo n.º 1
0
        public static int Handle(UploadParameters options)
        {
            try
            {
                Console.OutputEncoding = Encoding.UTF8;
            }
            catch (Exception)
            {
                // swallow. The above throws a handle error when run in Visual Studio
            }

            IsUploading = true;
            // -u user, and <path> are all required, so they must contain strings.
            // -d destination has a default value, so it also must contain a string.
            options.Path = options.Path.TrimEnd(new[] { '/', '\\', System.IO.Path.PathSeparator });             // remove any trailing slashes
            // validate the value for the upload destination.
            options.Dest = options.Dest.ToLowerInvariant();
            switch (options.Dest)
            {
            case UploadDestination.DryRun:
            case UploadDestination.Development:
            case UploadDestination.Production:
                break;

            default:
                Console.WriteLine($"Error: if present, upload destination (-d) must be one of {UploadDestination.DryRun}, {UploadDestination.Development}, or {UploadDestination.Production}");
                return(1);
            }
            BookUpload.Destination = options.Dest;                // must be set before calling SetupErrorHandling() (or BloomParseClient constructor)

            // This task will be all the program does. We need to do enough setup so that
            // the upload code can work, then tear it down.
            Program.SetUpErrorHandling();
            try
            {
                using (var applicationContainer = new ApplicationContainer())
                {
                    Program.SetUpLocalization(applicationContainer);
                    LocalizationManager.SetUILanguage(Settings.Default.UserInterfaceLanguage, false);
                    var singleBookUploader = new BookUpload(new BloomParseClient(), ProjectContext.CreateBloomS3Client(),
                                                            applicationContainer.BookThumbNailer);
                    var uploader = new BulkUploader(singleBookUploader);


                    // Since Bloom is not a normal console app, when run from a command line, the new command prompt
                    // appears at once. The extra newlines here are attempting to separate this from our output.
                    switch (options.Dest)
                    {
                    case UploadDestination.DryRun:
                        Console.WriteLine($"\nThe following actions would happen if you set destination to '{(BookUpload.UseSandboxByDefault ? UploadDestination.Development : UploadDestination.Production)}'.");
                        break;

                    case UploadDestination.Development:
                        Console.WriteLine("\nThe upload will go to dev.bloomlibrary.org.");
                        break;

                    case UploadDestination.Production:
                        Console.WriteLine("\nThe upload will go to bloomlibrary.org.");
                        break;
                    }
                    Console.WriteLine("\nStarting upload...");
                    uploader.BulkUpload(applicationContainer, options);
                    Console.WriteLine(("\nBulk upload complete.\n"));
                }
                return(0);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(1);
            }
        }