Esempio n. 1
0
        public async Task GetImage(int id, Func <Stream, ImageInfo, Task> callback)
        {
            var imageInfo = await _connection.Images.SingleOrDefaultAsync(info => info.Id == id);

            using (var trans = _connection.BeginTransaction())
            {
                using (var stream = await _largeObjectManager.OpenReadAsync(imageInfo.ImageId, CancellationToken.None))
                {
                    await callback(stream, imageInfo);
                }
            }
        }
Esempio n. 2
0
        public async Task <Stream> GetLOBBuffered(uint oid, DBTransaction transaction)
        {
            var outStream  = new MemoryStream();
            var manager    = new NpgsqlLargeObjectManager((NpgsqlConnection)transaction.Connection);
            var bufferSize = 81920;
            var buffer     = new byte[bufferSize];

            using (var lobStream = await manager.OpenReadAsync(oid, CancellationToken.None))
            {
                int count;
                while ((count = await lobStream.ReadAsync(buffer, 0, bufferSize)) != 0)
                {
                    outStream.Write(buffer, 0, count);
                }
            }
            outStream.Position = 0;
            return(outStream);
        }
Esempio n. 3
0
        public override async Task <Stream> GetLOB(uint oid, DBTransaction transaction, int bufferSize = 81920)
        {
            var manager = new NpgsqlLargeObjectManager((NpgsqlConnection)transaction.Connection);

            return(await manager.OpenReadAsync(oid, CancellationToken.None));
        }
        public async Task <Result <FileResult, Unit> > Get(
            string?userName,
            DateTimeOffset?ifModifiedSince,
            Guid id)
        {
            var contextData = await this.dbContext.Contexts
                              .Where(context =>
                                     context.Project.PublicallyReadable ||
                                     (context.Project.Owner.UserName == userName ||
                                      context.Project.Contributors.Any(contributor => contributor.User.UserName == userName)))
                              .Select(context => new
            {
                Id = context.Id,
                ContentObjectId = context.ContentObjectId,
                MediaType       = context.MediaType != null ? context.MediaType.MediaType : null,
                Extension       = context.MediaType != null ? context.MediaType.Extension : null,
                LastModified    = context.CreationTime
            })
                              .FirstOrDefaultAsync(contextData => contextData.Id == id);

            if (contextData == null)
            {
                return(Result <FileResult, Unit> .Failure(
                           HttpStatusCode.NotFound,
                           "context not found"));
            }

            if (contextData.ContentObjectId == null || contextData.MediaType == null)
            {
                return(Result <FileResult, Unit> .Failure(
                           HttpStatusCode.NotFound,
                           "context has no associated binary data"));
            }

            if (ifModifiedSince != null && contextData.LastModified < ifModifiedSince)
            {
                return(Result <FileResult, Unit> .Failure(
                           HttpStatusCode.NotModified,
                           "not modified"));
            }

            await this.dbContext.Database.OpenConnectionAsync();

            var connection = (NpgsqlConnection)this.dbContext.Database.GetDbConnection();
            // we have to begin a transaction, in order for LOB access to work
            // (otherwise we get a "invalid large-object descriptor: 0" error)
            // but we can't dispose it in this method, so we wrap the created stream
            // in a wrapper that disposes another object after disposing the stream
            var transaction = await this.dbContext.Database.BeginTransactionAsync();

            var lobManager = new NpgsqlLargeObjectManager(connection);

            return(Result <FileResult, Unit> .Ok(new FileResult()
            {
                Content = new DisposingStream(
                    await lobManager.OpenReadAsync(contextData.ContentObjectId.Value),
                    transaction),
                FileName = $"{contextData.Id}.{contextData.Extension}",
                MediaType = contextData.MediaType,
                LastModified = contextData.LastModified
            }));
        }