Example #1
0
        public async Task <ImageInfo> InsertImage(ImageInfo imageInfo, Stream image,
                                                  CancellationToken token)
        {
            using (var transaction = _connection.BeginTransaction())
            {
                imageInfo.ImageId = await _largeObjectManager.CreateAsync(imageInfo.ImageId, token);

                using (var stream = await _largeObjectManager.OpenReadWriteAsync(imageInfo.ImageId, token))
                {
                    await image.CopyToAsync(stream);
                }
                imageInfo.Id = _connection.InsertId(imageInfo);
                transaction.Commit();
                return(imageInfo);
            }
        }
Example #2
0
        public static async Task <uint> SetLOBDirect(Stream value, DBTransaction transaction)
        {
            if (value.CanSeek)
            {
                value.Position = 0;
            }
            var manager    = new NpgsqlLargeObjectManager((NpgsqlConnection)transaction.Connection);
            var bufferSize = 81920;
            var buffer     = new byte[bufferSize];

            var oid = await manager.CreateAsync(0, CancellationToken.None);

            using (var lobStream = await manager.OpenReadWriteAsync(oid, CancellationToken.None))
            {
                //await value.CopyToAsync(lobStream);
                int count;
                while ((count = await value.ReadAsync(buffer, 0, bufferSize)) != 0)
                {
                    lobStream.Write(buffer, 0, count);
                }
            }
            return(oid);
        }
Example #3
0
        public async Task <Result <Unit, Unit> > Add(
            string?userName,
            Guid id,
            string correlationId,
            string projectName,
            Stream?content,
            string?mediaType,
            string?text)
        {
            if (text == null && mediaType == null && content == null)
            {
                return(Result <Unit, Unit> .Failure(
                           HttpStatusCode.BadRequest,
                           "either text or content (with its media type) must be specified"));
            }

            var context = await this.dbContext.Contexts.FindAsync(id);

            if (context != null)
            {
                return(Result <Unit, Unit> .Failure(
                           HttpStatusCode.BadRequest,
                           "request with such a guid exists"));
            }

            var user = await this.userManager.FindByNameAsync(userName);

            var project = await this.dbContext.Projects
                          .FirstOrDefaultAsync(project =>
                                               (project.Owner.UserName == userName ||
                                                project.Contributors.Any(contributor => contributor.User.UserName == userName)) &&
                                               project.Name == projectName);

            if (project == null)
            {
                return(Result <Unit, Unit> .Failure(
                           HttpStatusCode.BadRequest,
                           "project not found"));
            }

            if (content != null && mediaType == null)
            {
                return(Result <Unit, Unit> .Failure(
                           HttpStatusCode.BadRequest,
                           "if a request provides content, its media type must be specified"));
            }

            if (content == null && mediaType != null)
            {
                return(Result <Unit, Unit> .Failure(
                           HttpStatusCode.BadRequest,
                           "no content provided"));
            }

            AllowedMediaType?mediaTypeModel = null;

            if (mediaType != null)
            {
                mediaTypeModel = await this.dbContext.MediaTypes.FirstOrDefaultAsync(m => m.MediaType == mediaType);

                if (mediaTypeModel == null)
                {
                    return(Result <Unit, Unit> .Failure(
                               HttpStatusCode.BadRequest,
                               "media type not acceptable"));
                }
            }

            uint?contentObjectId = null;

            await this.dbContext.Database.OpenConnectionAsync();

            var connection = (NpgsqlConnection)this.dbContext.Database.GetDbConnection();

            using (var transaction = await connection.BeginTransactionAsync())
            {
                if (content != null)
                {
                    var lobManager = new NpgsqlLargeObjectManager(connection);
                    var contentOid = await lobManager.CreateAsync(0);

                    using (var stream = await lobManager.OpenReadWriteAsync(contentOid))
                    {
                        await content.CopyToAsync(stream);
                    }

                    contentObjectId = contentOid;
                }

                await this.dbContext.Database.ExecuteSqlInterpolatedAsync(
                    $@"INSERT INTO ""Contexts"" (
                        ""Id"",
                        ""ProjectId"",
                        ""CorrelationId"",
                        ""Text"",
                        ""ContentObjectId"",
                        ""MediaTypeId"",
                        ""CreatedById"",
                        ""CreationTime"")
                    VALUES (
                        {id},
                        {project.Id},
                        {correlationId},
                        {text},
                        {contentObjectId},
                        {mediaTypeModel?.Id},
                        {user.Id},
                        {currentTimeProvider.GetCurrentTime()});");

                await transaction.CommitAsync();
            }
            return(Result <Unit, Unit> .Ok(Unit.Value));
        }