private BlobDto GetDtoFromBlob(Blob blob)
        {
            if (blob == null)
            {
                return(null);
            }
            List <string> pathSegments = GetPathSegmentsFromPath(blob.Path);
            BlobDto       dto          = new BlobDto
            {
                TenantId    = blob.TenantId,
                BlobId      = blob.BlobId,
                Size        = blob.Size,
                ContentType = blob.ContentType,
                Folder1     = GetFolderFromPathSegments(pathSegments, 0),
                Folder2     = GetFolderFromPathSegments(pathSegments, 1),
                Folder3     = GetFolderFromPathSegments(pathSegments, 2),
                Name        = blob.Name,
                Created     = blob.Created,
                Updated     = blob.Updated
            };

            if (blob is BlobImage)
            {
                dto.Width  = ((BlobImage)blob).Width;
                dto.Height = ((BlobImage)blob).Height;
            }
            return(dto);
        }
        private Blob GetBlobFromDto(BlobDto dto)
        {
            Blob blob = null;

            if (dto == null)
            {
                return(blob);
            }
            if (dto.BlobType == BlobType.Image)
            {
                blob = new BlobImage {
                    Width = dto.Width.Value, Height = dto.Height.Value, BlobType = BlobType.Image
                }
            }
            ;
            else
            {
                blob = new Blob {
                    BlobType = BlobType.Document
                }
            };
            List <string> pathSegments = GetPathSegmentsFromFolders(dto.Folder1, dto.Folder2, dto.Folder3);

            blob.TenantId    = dto.TenantId;
            blob.BlobId      = dto.BlobId;
            blob.Size        = dto.Size;
            blob.ContentType = dto.ContentType;
            blob.Path        = GetPathFromPathSegments(pathSegments);
            blob.Name        = dto.Name;
            blob.Created     = dto.Created;
            blob.Updated     = dto.Updated;
            return(blob);
        }
Ejemplo n.º 3
0
        public async Task <Blob> ReadBlobAsync(long tenantId, long blobId)
        {
            using (SqlConnection connection = new SqlConnection(_options.Value.SqlConnectionString))
            {
                connection.Open();

                BlobDto dto = await connection.QueryFirstOrDefaultAsync <BlobDto>(
                    @"SELECT TenantId, BlobId, Size, ContentType, Name, Folder1, Folder2, Folder3, Width, Height, Created, Updated
                        FROM Blob WHERE TenantId = @TenantId AND BlobId = @BlobId",
                    new { TenantId = tenantId, BlobId = blobId }
                    );

                return(GetBlobFromDto(dto));
            }
        }
        public async Task <Blob> ReadBlobAsync(long tenantId, long blobId)
        {
            using (SqlConnection connection = new SqlConnection(_options.Value.SqlConnectionString))
            {
                connection.Open();

                BlobDto dto = await connection.QueryFirstOrDefaultAsync <BlobDto>($@"
                    {GetSelectBlobSql()}
                    WHERE
                        cms.Upload.TenantId = @TenantId AND
                        cms.Upload.UploadId = @BlobId",
                                                                                  new { TenantId = tenantId, BlobId = blobId }
                                                                                  );

                return(GetBlobFromDto(dto));
            }
        }
        public async Task <long> CreateBlobAsync(long tenantId, Blob blob)
        {
            BlobDto dto = GetDtoFromBlob(blob);

            using (SqlConnection connection = new SqlConnection(_options.Value.SqlConnectionString))
            {
                connection.Open();
                long blobId = await connection.QuerySingleAsync <long>(
                    @"INSERT INTO Blob (TenantId, Size, ContentType, Name, Folder1, Folder2, Folder3, Width, Height, Created, Updated)
                        VALUES(@TenantId, @Size, @ContentType, @Name, @Folder1, @Folder2, @Folder3, @Width, @Height, @Created, @Updated)
                        SELECT CAST(SCOPE_IDENTITY() as bigint)",
                    dto
                    );

                return(blobId);
            }
        }