public Media Add(Media media, int userId)
{
try
{
var guid = Guid.NewGuid().ToString();
var extension = !string.IsNullOrEmpty(media.MediaType) ? media.MediaType.Split('/')[1] ?? "jpg" : "jpg";
var filename = guid + "." + extension;
var album = _albumRepository.Find(a => a.AlbumId == media.AlbumId, false).FirstOrDefault();
if (album == null)
{
album = GetAlbumByName(DateTime.Now.ToShortDateString(), userId);
if (album == null) throw new Exception("Error creating or finding album");
}
var mediaPath = _imageHelper.GenerateImagePath(album.UserId, guid, Constants.FileMediaLocation);
if (string.IsNullOrEmpty(mediaPath)) throw new Exception("Error generating media directory path");
var hasCreatedDir = _fileHelper.CreateDirectory(mediaPath);
if (!hasCreatedDir) throw new Exception("Error creating media directory");
var hasCreatedMedia = _imageHelper.SaveImage(media.MediaContent, mediaPath, filename);
if (!hasCreatedMedia) throw new Exception("Error saving media");
var tMedia = PrepareMediaForAdding(filename, album.AlbumId, mediaPath, album.UserId, media.MediaType, guid);
CreateThumbnail(tMedia, mediaPath, filename);
return MediaMapper.ToDto(_mediaRepository.Add(MediaMapper.ToEntity(media)));
}
catch (Exception ex)
{
throw new BlogException(ex.Message, ex.InnerException);
}
}