/// <summary>
        /// Save the image as a png in a public bucket.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="id"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        private async Task <MediaItemStorageLocation> SaveImage(Image image, Guid id, Guid accountId)
        {
            ImageFormat format = ImageFormat.Bmp;
            DateTime    date   = SystemDate.UtcNow;

            string imageName = string.Format("{0}/{1}/{2}/{3}/{4}.bmp",
                                             accountId,
                                             date.Year,
                                             date.Month,
                                             date.Day,
                                             id);

            using (MemoryStream stream = new MemoryStream())
            {
                // Save to png...
                // unless the original was gif to preserve animated gifs?
                image.Save(stream, format);
                stream.Position = 0;

                var mediaItemStorageLocation = new MediaItemStorageLocation
                {
                    Bucket        = _rawImageBucket,
                    ContentType   = "image/bmp",
                    ItemType      = MediaItemType.UploadedItem.ToString(),
                    ContentLength = (int)stream.Length,
                    Filename      = imageName,
                };

                await _fileStore.SaveAsync(mediaItemStorageLocation, stream);

                return(mediaItemStorageLocation);
            }
        }
        private async Task DeleteUploadedImageAsync(MediaItem mediaItem)
        {
            MediaItemStorageLocation storageLocation = mediaItem.StorageLocations.First();

            Logger.LogMessage("Deleting original uploaded image: {0}", storageLocation.Filename);
            await _fileStore.DeleteAsync(storageLocation);

            mediaItem.StorageLocations.Clear();
        }
Beispiel #3
0
 public async Task DeleteAsync(MediaItemStorageLocation storageLocation)
 {
     try
     {
         await _client.DeleteObjectAsync(storageLocation.Bucket, storageLocation.Filename);
     }
     catch (Exception ex)
     {
         Logger.LogException(ex, "Failed to deleted file from S3. Bucket: " + storageLocation + ", file: " + storageLocation.Filename);
         // Just sink the exception.
     }
 }
Beispiel #4
0
        public async Task SaveAsync(MediaItemStorageLocation storageLocation, Stream stream)
        {
            Logger.LogMessage("Saving media item to bucket: '{0}', path: {1}", storageLocation.Bucket, storageLocation.Filename);
            var request = new PutObjectRequest
            {
                BucketName  = storageLocation.Bucket,
                ContentType = storageLocation.ContentType,
                InputStream = stream,
                Key         = storageLocation.Filename
            };

            await _client.PutObjectAsync(request);
        }
        /// <summary>
        /// Save the image as a png in a public bucket.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="mediaItemType"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        private async Task <MediaItemStorageLocationDto> SaveImage(ProcessMediaItemRequestEvent request,
                                                                   MediaItemType mediaItemType,
                                                                   Image image)
        {
            Guid id = request.Id;

            // Default encoding as png for everything
            // except the original image.
            ImageFormat format = ImageFormat.Jpeg; // ImageFormat.Png; // TODO: Use gif if original was gif.


            // Try to preserve the original content type.
            if (mediaItemType == MediaItemType.OriginalItem || request.PreserveImageFormat)
            {
                format = GetFormat(request.StorageLocation.ContentType);
            }

            DateTime date        = DateTime.UtcNow;
            string   contentType = GetContentType(format);

            string imageName = string.Format("{0}/{1}/{2}/{3}/{4}.{5}",
                                             date.Year,
                                             date.Month,
                                             date.Day,
                                             mediaItemType,
                                             id,
                                             format);

            using (MemoryStream stream = new MemoryStream())
            {
                // Save to png...
                // unless the original was gif to preserve animated gifs?
                image.Save(stream, format);
                stream.Position = 0;

                var mediaItemStorageLocation = new MediaItemStorageLocation
                {
                    Bucket        = _imageBucket,
                    ContentType   = contentType,
                    ItemType      = mediaItemType.ToString(),
                    ContentLength = (int)stream.Length,
                    Filename      = imageName,
                };

                await _fileStore.SaveAsync(mediaItemStorageLocation, stream);

                return(Mapper.Map <MediaItemStorageLocationDto>(mediaItemStorageLocation));
            }
        }
 /// <summary>
 /// Publish a request to convert the media at the storage location
 /// into the appropriate media items (scaled down etc)
 /// </summary>
 /// <param name="mediaItem"></param>
 /// <param name="storageLocation"></param>
 /// <returns></returns>
 private async Task PublishProcessMediaItemAsync(MediaItem mediaItem, MediaItemStorageLocation storageLocation, bool preserveImageFormat)
 {
     ProcessMediaItemRequestEvent requestEvent = new ProcessMediaItemRequestEvent
     {
         Id = mediaItem.Id,
         StorageLocation = new MediaItemStorageLocationDto
         {
             Bucket        = storageLocation.Bucket,
             ContentLength = storageLocation.ContentLength,
             ContentType   = storageLocation.ContentType,
             Filename      = storageLocation.Filename,
             ItemType      = (MediaItemType)Enum.Parse(typeof(MediaItemType), storageLocation.ItemType),
         },
         PreserveImageFormat = preserveImageFormat
     };
     await _bus.PublishAsync(requestEvent);
 }
Beispiel #7
0
        public async Task <Stream> LoadStreamAsync(MediaItemStorageLocation storageLocation)
        {
            try
            {
                var request = new GetObjectRequest
                {
                    BucketName = storageLocation.Bucket,
                    Key        = storageLocation.Filename,
                };

                GetObjectResponse response = await _client.GetObjectAsync(request);

                return(response.ResponseStream);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, "Failed to load attachment from S3. Url: " + storageLocation);
                throw;
            }
        }
        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.");
            }
        }