protected override async Task Run()
        {
            await DiscordBot.LoggingChannel.SendMessageAsync($"**[WebPConversionOneTimeTask]** Starting WebP image conversion");

            List <Container> allContainers = new List <Container>();

            // Add all Containers to the List
            allContainers.AddRange(ContainerCache.GetEvents());
            allContainers.AddRange(ContainerCache.GetPopUpNews());
            allContainers.AddRange(ContainerCache.GetPresents());

            // Loop over every Container
            foreach (Container container in allContainers)
            {
                // Get the FileType
                FileType fileType = FileTypeExtensions.GetTypeFromContainer(container);

                // Format the destination S3 path
                string s3Path = $"/smash/{FileTypeExtensions.GetNamePrefixFromType(fileType)}/{container.Id}";

                // Get the raw image
                byte[] jpgImage = (byte[])container.GetType().GetProperty("Image").GetValue(container);

                // Create a new MagickImage
                using (MagickImage image = new MagickImage(jpgImage))
                {
                    // Set the output format to WebP
                    image.Format = MagickFormat.WebP;

                    // Create the raw WebP
                    byte[] webpImage = image.ToByteArray();

                    await DiscordBot.LoggingChannel.SendMessageAsync($"**[WebPConversionOneTimeTask]** Uploading image for {fileType.ToString()} ID {container.Id}");

                    // Upload to S3
                    S3Api.TransferFile(webpImage, s3Path, "image.webp", "image/webp");
                }
            }
        }
Example #2
0
        public static async Task <bool> ScheduleArchival(Container container, DateTime dateTime)
        {
            // Check if this is an event PopUpNews
            PopUpNews popUpNews = container as PopUpNews;

            if (popUpNews != null && popUpNews.IsPopUpForEvent)
            {
                // Skip
                return(false);
            }

            // Create the job name
            string jobName = container.GetType().Name + container.Id;

            // Get the Url
            string url = container.GetFormattedUrl();

            if (url.Length == 0)
            {
                // Skip
                return(false);
            }

            // Create the JobDataMap
            JobDataMap dataMap = new JobDataMap();

            dataMap.Put(ContainerArchivalJob.ARCHIVAL_DATA_PREFIX, FileTypeExtensions.GetNamePrefixFromType(FileTypeExtensions.GetTypeFromContainer(container)));
            dataMap.Put(ContainerArchivalJob.ARCHIVAL_DATA_ID, container.Id);
            dataMap.Put(ContainerArchivalJob.ARCHIVAL_DATA_URL, url);

            // Schedule the job for this Container
            await QuartzScheduler.ScheduleJob <ContainerArchivalJob>(jobName, dateTime, dataMap);

            await DiscordBot.LoggingChannel.SendMessageAsync($"**[ArchivalHandler]** Scheduled archival job for {container.GetType().Name} ({container.Id})");

            return(true);
        }
Example #3
0
        public static void HandleContainer(Container container)
        {
            // Get the FileType
            FileType fileType = FileTypeExtensions.GetTypeFromContainer(container);

            // Format the destination S3 path
            string s3Path = $"/smash/{FileTypeExtensions.GetNamePrefixFromType(fileType)}/{container.Id}";

            // Convert the Container into a JSON string
            byte[] json = Encoding.UTF8.GetBytes(WebFileHandler.ToJson(container));

            // Write the data to S3
            S3Api.TransferFile(json, s3Path, "data.json", "application/json");

            // Check if this has an image
            if (fileType == FileType.Event || fileType == FileType.PopUpNews || fileType == FileType.Present)
            {
                // Get the image
                byte[] image = (byte[])container.GetType().GetProperty("Image").GetValue(container);

                // Write the image to S3
                S3Api.TransferFile(image, s3Path, "image.jpg", "image/jpeg");

                // Create a new MagickImage
                using (MagickImage magickImage = new MagickImage(image))
                {
                    // Set the output format to WebP
                    magickImage.Format = MagickFormat.WebP;

                    // Create the raw WebP
                    byte[] webpImage = magickImage.ToByteArray();

                    // Upload to S3
                    S3Api.TransferFile(webpImage, s3Path, "image.webp", "image/webp");
                }
            }

            lock (WebFileHandler.Lock)
            {
                // Connect to the remote server if needed
                WebFileHandler.Connect(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig);

                // Convert the Container to a StrippedContainer
                StrippedContainer strippedContainer = StrippedContainer.ConvertToStrippedContainer(container);

                // Declare a variable to hold the container list
                List <StrippedContainer> containerList;

                // Format the container list path
                string indexPath = string.Format(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerListPath, FileTypeExtensions.GetNamePrefixFromType(fileType));

                // Check if the file exists
                if (WebFileHandler.Exists(indexPath))
                {
                    // Deserialize the List
                    containerList = WebFileHandler.ReadAllText <List <StrippedContainer> >(indexPath);
                }
                else
                {
                    // Create a new List
                    containerList = new List <StrippedContainer>();
                }

                // Check if the Container already exists in the list
                int index = containerList.FindIndex(x => x.Id == container.Id);

                // Check the index
                if (index == -1)
                {
                    // Add the StrippedContainer to the List
                    containerList.Insert(0, strippedContainer);
                }
                else
                {
                    // Replace the item at the index
                    containerList[index] = strippedContainer;
                }

                // Serialize and write the container list
                WebFileHandler.WriteAllText(indexPath, WebFileHandler.ToJson(containerList));

                // Declare a variable to hold the ContainerIndex
                ContainerIndex containerIndex;

                // Check if the ContainerIndex exists
                if (!WebFileHandler.Exists(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath))
                {
                    // Create a dummy StrippedContainer
                    StrippedContainer dummyStrippedContainer = new StrippedContainer();
                    dummyStrippedContainer.Id   = "-1";
                    dummyStrippedContainer.Text = new Dictionary <Nintendo.Bcat.Language, string>();

                    // Create a dummy ContainerIndex
                    containerIndex           = new ContainerIndex();
                    containerIndex.Event     = dummyStrippedContainer;
                    containerIndex.LineNews  = dummyStrippedContainer;
                    containerIndex.PopUpNews = dummyStrippedContainer;
                    containerIndex.Present   = dummyStrippedContainer;
                }
                else
                {
                    // Read the file
                    containerIndex = WebFileHandler.ReadAllText <ContainerIndex>(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath);
                }

                // Get the correct property
                PropertyInfo propertyInfo = containerIndex.GetType().GetProperty(container.GetType().Name);

                // Set the value
                propertyInfo.SetValue(containerIndex, strippedContainer);

                // Write out the ContainerIndex
                WebFileHandler.WriteAllText(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath, WebFileHandler.ToJson(containerIndex));

                // Disconnect from the remote server
                WebFileHandler.Disconnect();
            }
        }