private async Task <MediaItemStorageLocation> CreateS3FileAsync(CreateImageRequestEvent request)
        {
            if (request.Height == 0 || request.Width == 0 || request.Data == null || request.Data.Length == 0)
            {
                throw new Exception("Invalid image size");
            }

            if (request.Height * request.Width != request.Data.Length)
            {
                throw new Exception("Image data does not match specified size.");
            }

            int factor = 1;

            if (request.Width < 100)
            {
                factor = 10;
            }

            Bitmap bitmap = new Bitmap(request.Width * factor, request.Height * factor, System.Drawing.Imaging.PixelFormat.Format32bppArgb);


            for (int x = 0; x < request.Width; x++)
            {
                for (int y = 0; y < request.Height; y++)
                {
                    int   pixel      = request.Data[(y * request.Width) + x];
                    Color pixelColor = Color.FromArgb(pixel);

                    if (factor > 1)
                    {
                        FillRectangle(factor, pixel, bitmap, x * factor, y * factor);
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, pixelColor);
                    }
                }
            }


            return(await SaveImage(bitmap, request.Id, request.User.AccountId));
        }
        public async Task OnRequest(CreateImageRequestEvent request)
        {
            Logger.LogMessage("Creating image from data. Width: {0}, Height: {1}", request.Width, request.Height);

            try
            {
                request.UniqueMediaName = request.UniqueMediaName ?? "";

                MediaItemStorageLocation storageLocation = await CreateS3FileAsync(request);

                var mediaItem = new MediaItem
                {
                    Id        = request.Id,
                    AccountId = request.User.AccountId,
                    UserId    = request.User.UserId,
                    Caption   = request.Caption,
                    //ContentType = request. .ContentType,
                    Location    = Mapper.Map <LocationDetails>(request.Location),
                    Tags        = request.Tags,
                    Description = request.Description,
                    //OriginalStorageLocation = request.StorageLocation
                    UniqueMediaName  = request.UniqueMediaName,
                    UniqueMediaKey   = string.Format("{0}-{1}", request.User.AccountId, request.UniqueMediaName.ToLower()),
                    StorageLocations = new List <MediaItemStorageLocation> {
                        storageLocation
                    },
                    HistoryType = MediaHistoryType.Pending,
                };

                // After status post so the status post Id can be included for reference
                await _mediaService.SaveAsync(mediaItem);

                await PublishProcessMediaItemAsync(mediaItem, storageLocation, true); //request.PreserveImageFormat);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, "Failed to add new image from raw data.");
            }
        }