Skip to content

Commit

Permalink
BAI-1627 fix backend build with FileWithScanResultsInterface typing
Browse files Browse the repository at this point in the history
  • Loading branch information
PE39806 committed Mar 6, 2025
1 parent a72a2c1 commit 9c2c0d1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions backend/src/routes/v2/model/file/getDownloadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { z } from 'zod'

import { AuditInfo } from '../../../../connectors/audit/Base.js'
import audit from '../../../../connectors/audit/index.js'
import { FileInterface, FileInterfaceDoc } from '../../../../models/File.js'
import { FileInterface, FileWithScanResultsInterface } from '../../../../models/File.js'
import { downloadFile, getFileById } from '../../../../services/file.js'
import { getFileByReleaseFileName } from '../../../../services/release.js'
import { registerPath } from '../../../../services/specification.js'
Expand Down Expand Up @@ -92,7 +92,7 @@ export const getDownloadFile = [
async (req: Request, res: Response<GetDownloadFileResponse>) => {
req.audit = AuditInfo.ViewFile
const { params } = parse(req, getDownloadFileSchema)
let file: FileInterfaceDoc
let file: FileWithScanResultsInterface
if ('semver' in params) {
file = await getFileByReleaseFileName(req.user, params.modelId, params.semver, params.fileName)
} else {
Expand All @@ -107,7 +107,7 @@ export const getDownloadFile = [
res.set('Content-Length', String(file.size))
// TODO: support ranges
// res.set('Accept-Ranges', 'bytes')
const stream = await downloadFile(req.user, file._id)
const stream = await downloadFile(req.user, file.id)

if (!stream.Body) {
throw InternalError('We were not able to retrieve the body of this file', { fileId: file._id })
Expand Down
12 changes: 6 additions & 6 deletions backend/src/services/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ export async function getFileById(user: UserInterface, fileId: string): Promise<
const files = await FileModel.aggregate([
{ $match: { _id: new Types.ObjectId(fileId) } },
{ $limit: 1 },
{ $addFields: { fileStrId: { $toString: '$_id' } } },
{ $addFields: { id: { $toString: '$_id' } } },
{
$lookup: {
from: 'v2_scans',
localField: 'fileStrId',
localField: 'id',
foreignField: 'fileId',
as: 'avScan',
},
Expand All @@ -149,11 +149,11 @@ export async function getFilesByModel(user: UserInterface, modelId: string) {
const model = await getModelById(user, modelId)
const files = await FileModel.aggregate([
{ $match: { modelId } },
{ $addFields: { fileStrId: { $toString: '$_id' } } },
{ $addFields: { id: { $toString: '$_id' } } },
{
$lookup: {
from: 'v2_scans',
localField: 'fileStrId',
localField: 'id',
foreignField: 'fileId',
as: 'avScan',
},
Expand All @@ -175,11 +175,11 @@ export async function getFilesByIds(
}
const files = await FileModel.aggregate([
{ $match: { _id: { $in: fileIds } } },
{ $addFields: { fileStrId: { $toString: '$_id' } } },
{ $addFields: { id: { $toString: '$_id' } } },
{
$lookup: {
from: 'v2_scans',
localField: 'fileStrId',
localField: 'id',
foreignField: 'fileId',
as: 'avScan',
},
Expand Down
8 changes: 4 additions & 4 deletions backend/src/services/mirroredModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ModelAction, ReleaseAction } from '../connectors/authorisation/actions.
import authorisation from '../connectors/authorisation/index.js'
import { ScanState } from '../connectors/fileScanning/Base.js'
import scanners from '../connectors/fileScanning/index.js'
import { FileInterfaceDoc } from '../models/File.js'
import { FileInterfaceDoc, FileWithScanResultsInterface } from '../models/File.js'
import { ModelDoc, ModelInterface } from '../models/Model.js'
import { ModelCardRevisionDoc } from '../models/ModelCardRevision.js'
import { ReleaseDoc } from '../models/Release.js'
Expand Down Expand Up @@ -548,15 +548,15 @@ async function addReleaseToZip(
mirroredModelId: string,
) {
log.debug('Adding release to zip file of releases.', { user, modelId: model.id, semver: release.semver })
const files: FileInterfaceDoc[] = await getFilesByIds(user, release.modelId, release.fileIds)
const files: FileWithScanResultsInterface[] = await getFilesByIds(user, release.modelId, release.fileIds)

try {
zip.append(JSON.stringify(release.toJSON()), { name: `releases/${release.semver}.json` })
for (const file of files) {
zip.append(JSON.stringify(file.toJSON()), { name: `files/${file._id}.json` })
zip.append(JSON.stringify(file), { name: `files/${file._id}.json` })
await uploadToS3(
file.path,
(await downloadFile(user, file._id)).Body as stream.Readable,
(await downloadFile(user, file.id)).Body as stream.Readable,
{
exporter: user.dn,
sourceModelId: model.id,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ export async function getModelReleases(
foreignField: '_id',
as: 'files',
pipeline: [
{ $addFields: { fileStrId: { $toString: '$_id' } } },
{ $addFields: { id: { $toString: '$_id' } } },
{
$lookup: {
from: 'v2_scans',
localField: 'fileStrId',
localField: 'id',
foreignField: 'fileId',
as: 'avScan',
},
Expand Down

0 comments on commit 9c2c0d1

Please sign in to comment.