public async Task <object> Get(Expression <Func <Domain.Models.File, bool> > predicate) { var file = await _filesService.All().Where(predicate).Where(f => f.Private == 0ul && f.Type == (int)FileType.Resource) .FirstOrDefaultAsync(); if (file == null) { return(NotFound()); } return(Redirect("/" + file.Content)); }
public override async Task <AddResponce> Add(AddRequest request, ServerCallContext context) { var reply = new AddResponce(); 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); } var publishService = _publishServices[request.Type]; if (publishService == null) { reply.Error = Error.NoPermission; return(reply); } if (!Guid.TryParse(request.ArticleId, out var articleId)) { reply.Error = Error.NoSuchEntity; return(reply); } var file = await _filesService.All() .Where(f => f.Id == articleId && f.Type == (int)File.Types.FileType.Normal) .FirstOrDefaultAsync(); if (file == null) { reply.Error = Error.NoSuchEntity; return(reply); } var baseUrl = request.BaseUrl; try { var article = new PublishArticle { Id = file.Id, Title = file.Name, BaseUrl = baseUrl, Url = baseUrl + request.Url, Group = request.Group }; var regexp = new Regex(@"\$\{FILENAME=(.*?)\}"); var matches = regexp.Matches(request.Content); var slices = new List <PublishArticleSlice>(); var cur = 0; foreach (Match match in matches) { if (match.Index > cur) { slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.String, Value = request.Content.Substring(cur, match.Index - cur) }); } slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.Image, Value = match.Groups[1].Value }); cur = match.Index + match.Value.Length; } if (cur < request.Content.Length) { slices.Add(new PublishArticleSlice { Type = PublishArticleSliceType.String, Value = request.Content.Substring(cur, request.Content.Length - cur) }); } article.Slices = slices.ToArray(); var id = await publishService.Publish(article); reply.Id = id; } catch { reply.Error = Error.InvalidArguments; } return(reply); }
public override async Task <FilesReply> Query(QueryRequest request, ServerCallContext context) { var reply = new FilesReply(); var query = request.Query; var parentPaths = default(FileParentId[]); if (!string.IsNullOrWhiteSpace(query.Parent)) { var indexedParentPath = query.Parent !; parentPaths = await _service.All() .Where(n => n.Type == (int)FileType.Folder && n.Path == indexedParentPath).Select( n => new FileParentId { Path = n.Path + PathSep } ).ToArrayAsync(); if (!parentPaths.Any()) { return(null); } } Expression <Func <File, bool> > queryFunc; try { queryFunc = _queryCompiler.Compile(parentPaths, query); } catch { reply.Error = Error.InvalidArguments; return(reply); } var items = _service.All().Where(queryFunc); items = await FilterPrivate(items, context); var filter = request.Filter; if (!string.IsNullOrWhiteSpace(filter)) { filter = filter !.Trim(); items = items.Where(n => n.Path !.Contains(filter, StringComparison.CurrentCultureIgnoreCase)); } var randomOrder = query.OrderBy == "Random"; int?total = null; if (randomOrder) { total = await items.CountAsync(); var skip = total.Value - request.Take + 1; if (skip > 0) { skip = new Random().Next() % skip; items = items.Skip(skip); } if (request.Take > 0) { items = items.Take(request.Take); } } else { if (!string.IsNullOrWhiteSpace(query.OrderBy)) { Expression <Func <File, object> > orderBy = default; switch (query.OrderBy) { case nameof(File.Name): orderBy = node => node.Name !; break; case nameof(File.Created): orderBy = node => node.Created; break; case nameof(File.Modified): orderBy = node => node.Modified; break; case nameof(File.Published): orderBy = node => node.Published; break; } if (orderBy == null) { reply.Error = Error.InvalidArguments; return(reply); } items = query.OrderByDesc ? items.OrderByDescending(orderBy) : items.OrderBy(orderBy); } if (request.Skip > 0 || request.Take > 0) { total = await items.CountAsync(); } if (request.Skip > 0) { items = items.Skip(request.Skip); } if (request.Take > 0) { items = items.Take(request.Take); } } var files = await items .Select(Selector) .ToArrayAsync(); reply.Files.AddRange(files); if (total.HasValue) { reply.Total = total.Value; } return(reply); }