-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2959 from specklesystems/fabians/blobstorage-ioc-3
chore(server): blobstorage IoC 3 - uploadFileStreamFactory
- Loading branch information
Showing
9 changed files
with
127 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
BlobStorageItem, | ||
BlobStorageItemInput | ||
} from '@/modules/blobstorage/domain/types' | ||
import { MaybeNullOrUndefined } from '@speckle/shared' | ||
|
||
export type GetBlobs = (params: { | ||
streamId?: MaybeNullOrUndefined<string> | ||
blobIds: string[] | ||
}) => Promise<BlobStorageItem[]> | ||
|
||
export type UpsertBlob = (item: BlobStorageItemInput) => Promise<BlobStorageItem> | ||
|
||
export type UpdateBlob = (params: { | ||
id: string | ||
item: Partial<BlobStorageItem> | ||
}) => Promise<BlobStorageItem> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { UpdateBlob, UpsertBlob } from '@/modules/blobstorage/domain/operations' | ||
import { BadRequestError } from '@/modules/shared/errors' | ||
|
||
export const uploadFileStreamFactory = | ||
(deps: { upsertBlob: UpsertBlob; updateBlob: UpdateBlob }) => | ||
async ( | ||
storeFileStream: (params: { | ||
objectKey: string | ||
fileStream: Buffer | ||
}) => Promise<{ fileHash: string }>, | ||
params1: { streamId: string; userId: string }, | ||
params2: { blobId: string; fileName: string; fileType: string; fileStream: Buffer } | ||
) => { | ||
const { streamId, userId } = params1 | ||
const { blobId, fileName, fileType, fileStream } = params2 | ||
|
||
if (streamId.length !== 10) | ||
throw new BadRequestError('The stream id has to be of length 10') | ||
if (userId.length !== 10) | ||
throw new BadRequestError('The user id has to be of length 10') | ||
|
||
const objectKey = `assets/${streamId}/${blobId}` | ||
const dbFile = { | ||
id: blobId, | ||
streamId, | ||
userId, | ||
objectKey, | ||
fileName, | ||
fileType | ||
} | ||
// need to insert the upload data before starting otherwise the upload finished | ||
// even might fire faster, than the db insert, causing missing asset data in the db | ||
await deps.upsertBlob(dbFile) | ||
|
||
const { fileHash } = await storeFileStream({ objectKey, fileStream }) | ||
|
||
// here we should also update the blob db record with the fileHash | ||
await deps.updateBlob({ id: blobId, item: { fileHash } }) | ||
|
||
return { blobId, fileName, fileHash } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters