public async Task <IActionResult> ShortUrlAsync(string base62, CancellationToken token) { if (string.IsNullOrEmpty(base62)) { return(NotFound()); } //if (!long.TryParse(base62, out var id)) //{ if (!Base62.TryParse(base62, out var id)) { return(NotFound()); } _userManager.TryGetLongUserId(User, out var userId); var query = new DocumentById(id, userId); var model = await _queryBus.QueryAsync(query, token); if (model == null) { return(NotFound()); } var t = RedirectToRoutePermanent(SeoTypeString.Document, new { courseName = FriendlyUrlHelper.GetFriendlyTitle(model.Document.Course), id = id.Value, name = FriendlyUrlHelper.GetFriendlyTitle(model.Document.Title) }); return(t); }
public async Task <ActionResult> DownloadAsync(long id, [FromServices] ICommandBus commandBus, [FromServices] IBlobProvider blobProvider2, CancellationToken token) { var user = _userManager.GetLongUserId(User); var query = new DocumentById(id, user); var tItem = _queryBus.QueryAsync(query, token); var tFiles = _blobProvider.FilesInDirectoryAsync("file-", id.ToString(), token); await Task.WhenAll(tItem, tFiles); var item = tItem.Result; if (item == null) { return(NotFound()); } if (item.Document.DocumentType == DocumentType.Video) { return(Unauthorized()); } if (!item.IsPurchased) { return(Unauthorized()); } var files = tFiles.Result; var uri = files.First(); var file = uri.Segments.Last(); Task followTask = Task.CompletedTask; //blob.core.windows.net/spitball-files/files/6160/file-82925b5c-e3ba-4f88-962c-db3244eaf2b2-advanced-linux-programming.pdf if (item.Document.User.Id != user) { var command = new DownloadDocumentCommand(item.Document.Id, user); //var command = new FollowUserCommand(item.Document.User.Id, user); followTask = commandBus.DispatchAsync(command, token); } var messageTask = _queueProvider.InsertMessageAsync(new UpdateDocumentNumberOfDownloads(id), token); await Task.WhenAll(followTask, messageTask); var nameToDownload = item.Document.Title; var extension = Path.GetExtension(file); var url = blobProvider2.GenerateDownloadLink(uri, TimeSpan.FromMinutes(30), nameToDownload + extension); return(Redirect(url.AbsoluteUri)); }
public async Task <IActionResult> OldDocumentLinkRedirect2Async(long id, CancellationToken token) { _userManager.TryGetLongUserId(User, out var userId); var query = new DocumentById(id, userId); var model = await _queryBus.QueryAsync(query, token); if (model == null) { return(NotFound()); } return(RedirectToRoutePermanent(SeoTypeString.Document, new { courseName = FriendlyUrlHelper.GetFriendlyTitle(model.Document.Course), id, name = FriendlyUrlHelper.GetFriendlyTitle(model.Document.Title) })); }
public async Task <ActionResult <DocumentPreviewResponse> > GetAsync(long id, [FromServices] IQueueProvider queueProvider, [FromServices] ICrawlerResolver crawlerResolver, [FromServices] IIndex <DocumentType, IDocumentGenerator> generatorIndex, [FromServices] IUrlBuilder urlBuilder, CancellationToken token) { long?userId = null; if (User.Identity.IsAuthenticated) { userId = _userManager.GetLongUserId(User); } var query = new DocumentById(id, userId); var model = await _queryBus.QueryAsync(query, token); if (model == null) { return(NotFound()); } model.Document.User.Image = urlBuilder.BuildUserImageEndpoint(model.Document.User.Id, model.Document.User.Image); if (model.Tutor != null) { model.Tutor.Image = urlBuilder.BuildUserImageEndpoint(model.Tutor.UserId, model.Tutor.Image); } var tQueue = queueProvider.InsertMessageAsync(new UpdateDocumentNumberOfViews(id), token); var textTask = Task; if (crawlerResolver.Crawler != null) { textTask = _blobProvider.DownloadTextAsync("text.txt", query.Id.ToString(), token); } var files = await generatorIndex[model.Document.DocumentType].GeneratePreviewAsync(model, userId.GetValueOrDefault(-1), token); await System.Threading.Tasks.Task.WhenAll(tQueue, textTask); model.Document.Url = Url.DocumentUrl(model.Document.Course, model.Document.Id, model.Document.Title); return(new DocumentPreviewResponse(model, files, textTask.Result)); }
public async Task <IActionResult> IndexAsync([FromQuery] string theme, long id, CancellationToken token) { _userManager.TryGetLongUserId(User, out var userId); var query = new DocumentById(id, userId); var model = await _queryBus.QueryAsync(query, token); if (model == null) { return(NotFound()); } if (model.DuplicateId.HasValue && id != model.DuplicateId) { var url = Url.RouteUrl("ShortDocumentLink2", new { id = model.DuplicateId.Value }, "https"); Response.Headers.Add("Link", $"<{url}>; rel=\"canonical\""); } ViewBag.title = _localizer["Title", model.Document.Course, model.Document.Title]; ViewBag.metaDescription = _localizer["Description", model.Document.Course]; Country country = model.Document.User.Country; if (model.Document.DocumentType == DocumentType.Video && !string.IsNullOrEmpty(model.Document.Snippet)) { var jsonLd = new VideoObject() { Description = model.Document.Snippet, Name = model.Document.Title, ThumbnailUrl = new Uri(_urlBuilder.BuildDocumentImageShareEndpoint(model.Document.Id, new { width = 703, height = 395, mode = "crop", theme, rtl = country.MainLanguage.Info.TextInfo.IsRightToLeft.ToString() })), UploadDate = model.Document.DateTime, Duration = model.Document.Duration, }; ViewBag.jsonLd = jsonLd; } ViewBag.ogImage = new Uri(_urlBuilder.BuildDocumentImageShareEndpoint(model.Document.Id, new { width = 1200, height = 630, mode = "crop", theme, rtl = country.MainLanguage.Info.TextInfo.IsRightToLeft.ToString() })); ViewBag.ogTitle = model.Document.Title; ViewBag.ogDescription = _localizer.WithCulture(country.MainLanguage.Info) ["OgDescription", model.Document.Course]; ViewBag.ogImageWidth = 1200; ViewBag.ogImageHeight = 630; return(View()); }
public async Task DocumentById_Ok(long documentId, long?userId) { var query = new DocumentById(documentId, userId); var _ = await fixture.QueryBus.QueryAsync(query, default); }