public async Task <MediaViewModel> UploadMedia(Guid userId, CreateMediaDTO createMediaDTO) { await DeleteUnusedMedias(userId); using var ms = new MemoryStream(); await createMediaDTO.Media.CopyToAsync(ms, new CancellationTokenSource(TimeSpan.FromSeconds(MEDIA_PROCESS_TIMEOUT)).Token); var blobName = $"media-{Guid.NewGuid()}"; ms.Position = 0; var result = await BlobStorageRepo.UploadMedia(ms, blobName, createMediaDTO.Container); var media = new Media { MediaType = createMediaDTO.Media.ContentType.ToMediaType(), MimeType = createMediaDTO.Media.ContentType, BlobName = blobName, Container = createMediaDTO.Container, FileSize = createMediaDTO.Media.Length, OwnerId = userId }; await DbContext.Medias.AddAsync(media); await DbContext.SaveChangesAsync(); var mediaViewModel = MediaViewModel.FromMedia(media); mediaViewModel.BlobFullUrl = result.AbsoluteUri; return(mediaViewModel); }
public static PrivateMessageViewModel FromPrivateMessage(PrivateMessage msg) => new PrivateMessageViewModel { Id = msg.Id, MessageContent = msg.MessageContent, CreationDateUtc = msg.CreationDate, Media = msg.Media == null ? null : MediaViewModel.FromMedia(msg.Media), Sender = UserSummaryViewModel.FromUser(msg.Sender), Recipient = UserSummaryViewModel.FromUser(msg.Recipient) };
public async Task <MediaViewModel> GetMedia(Guid?userId, Guid id) { var media = DbContext.Medias .Include(x => x.Owner) .SingleOrDefault(x => x.Id == id); if (media == null) { throw new DataNotFoundException($"Could not find media with id {id}"); } var mediaViewModel = MediaViewModel.FromMedia(media); mediaViewModel.BlobFullUrl = (await BlobStorageRepo.GetMediaUri(media.BlobName, media.Container)).AbsoluteUri; return(mediaViewModel); }