示例#1
0
        private async Task <string> ProcessImages(
            S3Event s3Event,
            IImageScaler imageScaler,
            ILogWriter <ImageResizeHandler> logger,
            IPhotosService photoService)
        {
            logger.LogInformation($"{nameof(ProcessImages)}");

            var s3Entity = s3Event.Records?[0].S3;

            if (s3Entity == null)
            {
                logger.LogCritical($"{nameof(s3Entity)} is null");
                return("NULL");
            }

            var     urlDecodedKey = System.Web.HttpUtility.UrlDecode(s3Entity.Object.Key);
            UserId  userId        = GetUserIdFromKey(urlDecodedKey);
            PhotoId photoId       = GetPhotoIdFromKey(urlDecodedKey);

            try
            {
                logger.LogInformation($"{nameof(urlDecodedKey)} is '{urlDecodedKey}'");



                await photoService.SetPhotoState(userId, photoId, PhotoState.ProcessingStarted);

                // generate filenames to use for the scaled images
                ImageKeys imageKeys = GetImageKeysWithoutExtension(urlDecodedKey);

                IEnumerable <Size> imageSizes;

                using (var s3InputObject = await S3Client.GetObjectAsync(s3Entity.Bucket.Name, urlDecodedKey))
                    using (var originalImageStream = new MemoryStream())
                    {
                        await ReadImageIntoStream(logger, originalImageStream, s3InputObject.ResponseStream);

                        imageSizes = await CreateScaledImages(imageScaler, logger, imageKeys, originalImageStream, GetTargetBucket());
                    }

                logger.LogInformation($"Updating photo data, making it available.");
                await UpdatePhotoData(photoService, photoId, imageSizes);

                // finish with deleting the upload file
                await DeleteOriginalSourceFile(logger, s3Entity, urlDecodedKey);

                logger.LogInformation($"Done with {urlDecodedKey}");
                return("OK");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Error when resizing {s3Entity.Object.Key} from bucket {s3Entity.Bucket.Name}:\n{ex.ToString()}");
                // set photo state to ProcessingFailed
                await photoService.SetPhotoState(userId, photoId, PhotoState.ProcessingFailed);

                throw;
            }
        }