diff --git a/backend/src/services/file.ts b/backend/src/services/file.ts index 7a91e196e..c8d2649fc 100644 --- a/backend/src/services/file.ts +++ b/backend/src/services/file.ts @@ -40,6 +40,10 @@ export function isFileInterfaceDoc(data: unknown): data is FileInterfaceDoc { return true } +export const createFilePath = (modelId: string, fileId: string) => { + return `beta/model/${modelId}/files/${fileId}` +} + export async function uploadFile(user: UserInterface, modelId: string, name: string, mime: string, stream: Readable) { const model = await getModelById(user, modelId) if (model.settings.mirror.sourceModelId) { @@ -49,7 +53,7 @@ export async function uploadFile(user: UserInterface, modelId: string, name: str const fileId = longId() const bucket = config.s3.buckets.uploads - const path = `beta/model/${modelId}/files/${fileId}` + const path = createFilePath(modelId, fileId) const file = new FileModel({ modelId, name, mime, bucket, path, complete: true }) diff --git a/backend/src/services/mirroredModel.ts b/backend/src/services/mirroredModel.ts index b2a1ffbe3..42d98a350 100644 --- a/backend/src/services/mirroredModel.ts +++ b/backend/src/services/mirroredModel.ts @@ -21,6 +21,7 @@ import { UserInterface } from '../models/User.js' import config from '../utils/config.js' import { BadReq, Forbidden, InternalError } from '../utils/error.js' import { + createFilePath, downloadFile, getFilesByIds, getTotalFileSize, @@ -166,13 +167,15 @@ export async function importModel( switch (importKind) { case ImportKind.Documents: { + log.info({ mirroredModelId, payloadUrl }, 'Importing colection of documents.') return await importDocuments(user, res, mirroredModelId, sourceModelId, payloadUrl) } case ImportKind.File: { + log.info({ mirroredModelId, payloadUrl }, 'Importing file data.') if (!filePath) { throw BadReq('Missing File Path.', { mirroredModelId, sourceModelIdMeta: sourceModelId }) } - const result = await importModelFile(res, filePath, mirroredModelId, sourceModelId) + const result = await importModelFile(res, filePath, mirroredModelId) return { mirroredModel, importResult: { @@ -297,14 +300,9 @@ async function importDocuments( } } -async function importModelFile( - content: Response, - importedPath: string, - mirroredModelId: string, - sourceModelId: string, -) { +async function importModelFile(content: Response, importedPath: string, mirroredModelId: string) { const bucket = config.s3.buckets.uploads - const updatedPath = importedPath.replace(sourceModelId, mirroredModelId) + const updatedPath = createFilePath(mirroredModelId, importedPath) await putObjectStream(bucket, updatedPath, content.body as Readable) log.debug({ bucket, path: updatedPath }, 'Imported file successfully uploaded to S3.') await markFileAsCompleteAfterImport(updatedPath) @@ -362,7 +360,7 @@ async function parseFile(fileJson: string, mirroredModelId: string, sourceModelI const modelId = file.modelId file.modelId = mirroredModelId - file.path = file.path.replace(modelId, mirroredModelId) + file.path = createFilePath(mirroredModelId, file.id) if (sourceModelId !== modelId) { throw InternalError('Zip file contains files from an invalid model.', { modelIds: [sourceModelId, modelId] }) } @@ -555,13 +553,13 @@ async function addReleaseToZip( for (const file of files) { zip.append(JSON.stringify(file.toJSON()), { name: `files/${file._id}.json` }) await uploadToS3( - file.path, + file.id, (await downloadFile(user, file._id)).Body as stream.Readable, { exporter: user.dn, sourceModelId: model.id, mirroredModelId, - filePath: file.path, + filePath: file.id, importKind: ImportKind.File, }, { diff --git a/backend/test/services/mirroredModel.spec.ts b/backend/test/services/mirroredModel.spec.ts index 306cc056f..fc892eae1 100644 --- a/backend/test/services/mirroredModel.spec.ts +++ b/backend/test/services/mirroredModel.spec.ts @@ -118,6 +118,7 @@ const fileMocks = vi.hoisted(() => ({ downloadFile: vi.fn(() => ({ Body: 'test' })), markFileAsCompleteAfterImport: vi.fn(), isFileInterfaceDoc: vi.fn(() => true), + createFilePath: vi.fn(() => 'file/path'), })) vi.mock('../../src/services/file.js', () => fileMocks)