示例#1
0
        public override async Task <TagsReply> GetAll(Empty request, ServerCallContext context)
        {
            var reply = new TagsReply();

            reply.Tags.AddRange(await _service.All()
                                .Select(Selector)
                                .ToArrayAsync());
            return(reply);
        }
示例#2
0
        public override async Task <AddOrUpdateTagsReply> AddOrUpdateTags(AddOrUpdateTagsRequest request,
                                                                          ServerCallContext context)
        {
            var reply = new AddOrUpdateTagsReply();
            var user  = await _userService.GetUser(context.GetHttpContext());

            if (user == null)
            {
                reply.Error = Error.NeedLogin;
                return(reply);
            }

            if (!user.HasWritePermission())
            {
                reply.Error = Error.NoPermission;
                return(reply);
            }

            if (!Guid.TryParse(request.Id, out var id))
            {
                reply.Error = Error.InvalidArguments;
                return(reply);
            }

            var item = await _service.All().Where(i => i.Id == id).Include(i => i.FileTags).ThenInclude(t => t.Tag)
                       .FirstOrDefaultAsync();

            if (item == null)
            {
                reply.Error = Error.NoSuchEntity;
                return(reply);
            }

            item.FileTags ??= new List <Domain.Models.FileTag>();

            var tags     = item.FileTags;
            var tagsDict = tags.ToDictionary(t => t.TagId);
            var updates  = request.Tags.Select(t => new
            {
                TagId = Guid.Parse(t.TagId),
                t.Value,
                t.Content,
                ContentType = GetContentType(t.ContentType),
                File        = item
            });

            var resourceContentToDelete = new List <string>();

            foreach (var update in updates)
            {
                Domain.Models.FileTag tag;
                if (tagsDict.ContainsKey(update.TagId))
                {
                    tag = tagsDict[update.TagId];
                }
                else
                {
                    tag = new Domain.Models.FileTag
                    {
                        Tag  = await _tagsService.All().FirstOrDefaultAsync(t => t.Id == update.TagId),
                        File = item
                    };

                    if (tag.Tag == null)
                    {
                        reply.Error = Error.NoSuchEntity;
                        return(reply);
                    }

                    tags.Add(tag);
                }

                if (tag.Tag.Type == (int)Tag.Types.Type.Resource)
                {
                    if (string.IsNullOrWhiteSpace(update.ContentType))
                    {
                        reply.Error = Error.InvalidArguments;
                        return(reply);
                    }

                    if (!string.IsNullOrWhiteSpace(tag.Value))
                    {
                        resourceContentToDelete.Add(tag.Value);
                    }

                    tag.Value = await _contentService.Add(_resourcesGroup, stream =>
                    {
                        update.Content.WriteTo(stream);
                        return(Task.FromResult(true));
                    }, update.ContentType);
                }
                else
                {
                    tag.Value = update.Value;
                }

                reply.TagContents.Add(tag.Value);
            }

            await _service.Update(item);

            foreach (var tobedeleted in resourceContentToDelete)
            {
                try
                {
                    await _contentService.Delete(tobedeleted);
                }
                catch (Exception)
                {
                    //ignore
                }
            }

            if (item.Type == (int)FileType.Folder)
            {
                await ClearFolderVersionsCache();
            }

            return(reply);
        }