bde148f286
* wip * some extra fixes * stuff kinda works? * need to figure out mocks * need to figure out mocks * fix db listener * gqlgen fix * minor gqlgen watch adjustment * lint fixes * delete old codegen file * converting migrations to ESM * getModuleDIrectory * vitest sort of works * added back ts-vitest * resolve gql double load * fixing test timeout configs * TSC lint fix * fix automate tests * moar debugging * debugging * more debugging * codegen update * server works * yargs migrated * chore(server): getting rid of global mocks for Server ESM (#5046) * got rid of email mock * got rid of comment mocks * got rid of multi region mocks * got rid of stripe mock * admin override mock updated * removed final mock * fixing import.meta.resolve calls * another import.meta.resolve fix * added requested test * nyc ESM fix * removed unneeded deps + linting * yarn lock forgot to commit * tryna fix flakyness * email capture util fix * sendEmail fix * fix TSX check * sender transporter fix + CR comments * merge main fix * test fixx * circleci fix * gqlgen bigint fix * error formatter fix * more error formatting improvements * esmloader added to Dockerfile * more dockerfile fixes * bg jobs fix
90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { RegisterCompletedUpload } from '@/modules/blobstorage/domain/operations'
|
|
import { GetBranchesByIds } from '@/modules/core/domain/branches/operations'
|
|
import {
|
|
GetFileInfoV2,
|
|
InsertNewUploadAndNotify,
|
|
InsertNewUploadAndNotifyV2,
|
|
RegisterUploadCompleteAndStartFileImport
|
|
} from '@/modules/fileuploads/domain/operations'
|
|
import { ModelNotFoundError } from '@/modules/core/errors/model'
|
|
import { ensureError } from '@speckle/shared'
|
|
import { FileImportJobNotFoundError } from '@/modules/fileuploads/helpers/errors'
|
|
import { get } from 'lodash-es'
|
|
|
|
export const registerUploadCompleteAndStartFileImportFactory = (deps: {
|
|
registerCompletedUpload: RegisterCompletedUpload
|
|
insertNewUploadAndNotify: InsertNewUploadAndNotifyV2 | InsertNewUploadAndNotify
|
|
getModelsByIds: GetBranchesByIds
|
|
getFileInfo: GetFileInfoV2
|
|
}): RegisterUploadCompleteAndStartFileImport => {
|
|
const {
|
|
registerCompletedUpload,
|
|
insertNewUploadAndNotify,
|
|
getModelsByIds,
|
|
getFileInfo
|
|
} = deps
|
|
return async (params) => {
|
|
const { projectId, modelId, fileId, userId, expectedETag, maximumFileSize } = params
|
|
const storedBlob = await registerCompletedUpload({
|
|
projectId,
|
|
blobId: fileId,
|
|
expectedETag,
|
|
maximumFileSize
|
|
})
|
|
|
|
const [model] = await getModelsByIds([modelId], { streamId: projectId })
|
|
if (!model) throw new ModelNotFoundError(undefined)
|
|
|
|
try {
|
|
const storedFile = await insertNewUploadAndNotify({
|
|
projectId: storedBlob.streamId,
|
|
streamId: storedBlob.streamId, //backwards compatibility
|
|
userId,
|
|
fileName: storedBlob.fileName,
|
|
fileType: storedBlob.fileType,
|
|
fileSize: storedBlob.fileSize,
|
|
fileId: storedBlob.id,
|
|
modelId,
|
|
modelName: model.name,
|
|
branchName: model.name //backwards compatibility
|
|
})
|
|
|
|
return {
|
|
...storedFile,
|
|
modelName: model.name,
|
|
projectId: storedBlob.streamId //backwards compatibility
|
|
}
|
|
} catch (error) {
|
|
const message = get(error, 'message')
|
|
if (
|
|
message &&
|
|
typeof message === 'string' &&
|
|
message.includes(
|
|
'duplicate key value violates unique constraint "file_uploads_pkey"'
|
|
)
|
|
) {
|
|
// The file import record already exists, so we try to return the existing file info
|
|
const storedFile = await getFileInfo({
|
|
fileId,
|
|
projectId
|
|
})
|
|
if (!storedFile) {
|
|
// Possible that the file id exists, hence the duplicate key error.
|
|
// But we cannot retrieve the file info for the given projectId.
|
|
// This can happen if the file was uploaded to a different project.
|
|
// we do not want to leak the file info, so throw an error
|
|
throw new FileImportJobNotFoundError(
|
|
'File import job not found for the given fileId and projectId'
|
|
)
|
|
}
|
|
return { ...storedFile, modelName: model.name }
|
|
}
|
|
|
|
throw ensureError(
|
|
error,
|
|
'Unexpected error while registering upload of a file as complete and requesting the file import to start.'
|
|
)
|
|
}
|
|
}
|
|
}
|