Esempio n. 1
0
        public async Task <PendingDocumentResponse> Get(long?fromId,
                                                        [FromServices] IBlobProvider blobProvider,
                                                        CancellationToken token)
        {
            var query  = new PendingDocumentQuery(fromId, User.GetCountryClaim());
            var retVal = await _queryBus.QueryAsync(query, token);

            var  tasks   = new Lazy <List <Task> >();
            var  counter = 0;
            long?id      = null;

            foreach (var document in retVal)
            {
                id = document.Id;
                var files = await _blobProvider.FilesInDirectoryAsync("preview-0", document.Id.ToString(), token);

                var file = files.FirstOrDefault();
                if (file != null)
                {
                    document.Preview =
                        blobProvider.GeneratePreviewLink(file,
                                                         TimeSpan.FromMinutes(20));

                    counter++;
                }
                else
                {
                    var t = _queueProvider.InsertBlobReprocessAsync(document.Id);
                    tasks.Value.Add(t);
                }
                document.SiteLink = Url.RouteUrl("DocumentDownload", new { id = document.Id });

                if (counter >= 21)
                {
                    break;
                }
            }

            if (tasks.IsValueCreated)
            {
                await Task.WhenAll(tasks.Value);
            }

            string nextLink = null;

            if (id.HasValue)
            {
                nextLink = Url.RouteUrl("Pending", new
                {
                    fromId = id.Value
                });
            }

            return(new PendingDocumentResponse()
            {
                Documents = retVal,
                NextLink = nextLink
            });
        }
Esempio n. 2
0
        public async Task <IEnumerable <UserDocumentsDto> > GetUserInfo(long id, int page, [FromServices] IBlobProvider blobProvider,
                                                                        CancellationToken token)
        {
            var country = User.GetCountryClaim();
            var query   = new UserDocumentsQuery(id, page, country);


            var retVal = (await _queryBus.QueryAsync(query, token)).ToList();
            var tasks  = new Lazy <List <Task> >();

            foreach (var document in retVal)
            {
                var files = await _blobProvider.FilesInDirectoryAsync("preview-0", document.Id.ToString(), token);

                var file = files.FirstOrDefault();
                if (file != null)
                {
                    document.Preview =
                        blobProvider.GeneratePreviewLink(file,
                                                         TimeSpan.FromMinutes(20));

                    document.SiteLink = Url.RouteUrl("DocumentDownload", new { id = document.Id });
                }
                else
                {
                    var t = _queueProvider.InsertBlobReprocessAsync(document.Id);
                    tasks.Value.Add(t);
                }
            }

            return(retVal);
        }
Esempio n. 3
0
        public async Task <IActionResult> DownloadDocument(long id,
                                                           [FromServices] IDocumentDirectoryBlobProvider blobProvider,
                                                           [FromServices] IBlobProvider blobProvider2,
                                                           CancellationToken token)
        {
            var files = await blobProvider.FilesInDirectoryAsync("file-", id.ToString(), token);

            var file = files.First();

            //blob.core.windows.net/spitball-files/files/6160/file-82925b5c-e3ba-4f88-962c-db3244eaf2b2-advanced-linux-programming.pdf
            var extension = Path.GetExtension(file.Segments.Last());
            var url       = blobProvider2.GenerateDownloadLink(file, TimeSpan.FromMinutes(30), "Temp" + extension);

            return(Redirect(url.AbsoluteUri));
        }
Esempio n. 4
0
        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));
        }