Merge pull request #2969 from specklesystems/fabians/blobstorage-ioc-6
chore(server): blobstorage IoC 6 - blobCollectionSummaryFactory
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { db } from '@/db/knex'
|
||||
import {
|
||||
blobCollectionSummaryFactory,
|
||||
getBlobMetadataCollectionFactory,
|
||||
getBlobMetadataFactory
|
||||
} from '@/modules/blobstorage/repositories'
|
||||
import { blobCollectionSummary, getFileSizeLimit } from '@/modules/blobstorage/services'
|
||||
import { getFileSizeLimit } from '@/modules/blobstorage/services'
|
||||
import {
|
||||
ProjectBlobArgs,
|
||||
ProjectBlobsArgs,
|
||||
@@ -20,6 +21,7 @@ import {
|
||||
|
||||
const getBlobMetadata = getBlobMetadataFactory({ db })
|
||||
const getBlobMetadataCollection = getBlobMetadataCollectionFactory({ db })
|
||||
const blobCollectionSummary = blobCollectionSummaryFactory({ db })
|
||||
|
||||
const streamBlobResolvers = {
|
||||
async blobs(parent: StreamGraphQLReturn, args: StreamBlobsArgs | ProjectBlobsArgs) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
NotFoundError,
|
||||
ResourceMismatch
|
||||
} from '@/modules/shared/errors'
|
||||
import { MaybeNullOrUndefined, Nullable } from '@speckle/shared'
|
||||
import { Knex } from 'knex'
|
||||
|
||||
const BlobStorage = buildTableHelper('blob_storage', [
|
||||
@@ -122,3 +123,27 @@ export const getBlobMetadataCollectionFactory =
|
||||
cursor: cursorFromRows(rows, cursorTarget)
|
||||
}
|
||||
}
|
||||
|
||||
export const blobCollectionSummaryFactory =
|
||||
(deps: { db: Knex }) =>
|
||||
async (params: { streamId: string; query?: MaybeNullOrUndefined<string> }) => {
|
||||
const { streamId, query } = params
|
||||
|
||||
const q = tables
|
||||
.blobStorage(deps.db)
|
||||
.where({ [BlobStorage.col.streamId]: streamId })
|
||||
.sum('fileSize')
|
||||
.count('id')
|
||||
|
||||
if (query) q.andWhereLike('fileName', `%${query}%`)
|
||||
|
||||
const [summary] = (await q) as unknown as Array<{
|
||||
sum: Nullable<string>
|
||||
count: string
|
||||
}>
|
||||
|
||||
return {
|
||||
totalSize: summary.sum ? parseInt(summary.sum) : 0,
|
||||
totalCount: parseInt(summary.count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,20 +6,6 @@ const BlobStorage = () => knex('blob_storage')
|
||||
const blobLookup = ({ blobId, streamId }) =>
|
||||
BlobStorage().where({ id: blobId, streamId })
|
||||
|
||||
const blobQuery = ({ streamId, query }) => {
|
||||
let blobs = BlobStorage().where({ streamId })
|
||||
if (query) blobs = blobs.andWhereLike('fileName', `%${query}%`)
|
||||
return blobs
|
||||
}
|
||||
|
||||
const blobCollectionSummary = async ({ streamId, query }) => {
|
||||
const [summary] = await blobQuery({ streamId, query }).sum('fileSize').count('id')
|
||||
return {
|
||||
totalSize: summary.sum ? parseInt(summary.sum) : 0,
|
||||
totalCount: parseInt(summary.count)
|
||||
}
|
||||
}
|
||||
|
||||
const getFileStream = async ({ getObjectStream, streamId, blobId }) => {
|
||||
const { objectKey } = await getBlobMetadataFactory({ db: knex })({ streamId, blobId })
|
||||
return await getObjectStream({ objectKey })
|
||||
@@ -64,6 +50,5 @@ module.exports = {
|
||||
markUploadError,
|
||||
getFileStream,
|
||||
deleteBlob,
|
||||
blobCollectionSummary,
|
||||
getFileSizeLimit
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const expect = require('chai').expect
|
||||
const { beforeEachContext } = require('@/test/hooks')
|
||||
const {
|
||||
blobCollectionSummary,
|
||||
getFileStream,
|
||||
deleteBlob,
|
||||
markUploadOverFileSizeLimit,
|
||||
@@ -15,7 +14,8 @@ const {
|
||||
upsertBlobFactory,
|
||||
updateBlobFactory,
|
||||
getBlobMetadataFactory,
|
||||
getBlobMetadataCollectionFactory
|
||||
getBlobMetadataCollectionFactory,
|
||||
blobCollectionSummaryFactory
|
||||
} = require('@/modules/blobstorage/repositories')
|
||||
const { db } = require('@/db/knex')
|
||||
const { cursorFromRows, decodeCursor } = require('@/modules/blobstorage/helpers/db')
|
||||
@@ -30,6 +30,7 @@ const uploadFileStream = uploadFileStreamFactory({
|
||||
})
|
||||
const getBlobMetadata = getBlobMetadataFactory({ db })
|
||||
const getBlobMetadataCollection = getBlobMetadataCollectionFactory({ db })
|
||||
const blobCollectionSummary = blobCollectionSummaryFactory({ db })
|
||||
|
||||
describe('Blob storage @blobstorage', () => {
|
||||
before(async () => {
|
||||
|
||||
Reference in New Issue
Block a user