예제 #1
0
        public static async Task AddMediaToAlbumAsync(
            ApplicationDbContext dbContext,
            MediaAlbum albumEntity,
            UserMedia mediaEntity,
            AddImageToAlbum createRequest)
        {
            albumEntity.UserMedias.Add(mediaEntity);
            mediaEntity.MediaAlbum = albumEntity;
            if (mediaEntity.State == UserMediaState.UploadedButUnused)
            {
                mediaEntity.State = UserMediaState.InUse;
            }

            mediaEntity.Title = createRequest.Title;
            if (!string.IsNullOrWhiteSpace(createRequest.Description))
            {
                mediaEntity.Description = await TextOperations.CreateTextAsync(
                    dbContext, createRequest.Description);
            }

            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexMediaAsync(new[] { mediaEntity });

            if (mediaEntity.Description != null)
            {
                await UserOperations.NotifyMentionsAsync(
                    dbContext, "Album Entry", mediaEntity.UserId, mediaEntity.Description);
            }
        }
예제 #2
0
        public static async Task <MediaUploadResults> StoreMediaAsync(
            ApplicationDbContext dbContext,
            UserInfo userEntity,
            IEnumerable <UploadedMedia> items)
        {
            var newEntities = new List <UserMedia>();

            foreach (UploadedMedia item in items)
            {
                if (new FileInfo(item.LocalFileName).Length > 0)
                {
                    string ext    = Path.GetExtension(item.OriginalName.Trim('\"')).Substring(1);
                    var    entity = new UserMedia
                    {
                        User       = userEntity,
                        State      = UserMediaState.UploadedButUnused,
                        CreatedUtc = DateTime.UtcNow,
                        Extension  = ext
                    };
                    var dataEntity = new UserMediaData
                    {
                        UserMedia = entity,
                        ImageData = File.ReadAllBytes(item.LocalFileName)
                    };
                    File.Delete(item.LocalFileName);
                    dbContext.UserMedias.Add(entity);
                    dbContext.UserMediaDatas.Add(dataEntity);

                    newEntities.Add(entity);
                }
            }

            await dbContext.SaveChangesAsync();

            await SearchOperations.IndexMediaAsync(newEntities);

            return(new MediaUploadResults
            {
                Files = newEntities
                        .Select(
                    e => new MediaUploadResult
                {
                    Id = e.UserMediaId
                })
                        .ToList()
            });
        }