Skip to content

Commit

Permalink
replace Types.Buffer with mongo.Binary
Browse files Browse the repository at this point in the history
  • Loading branch information
david-loe committed Oct 29, 2024
1 parent 5f8dd9f commit cf3e602
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
16 changes: 8 additions & 8 deletions backend/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DeleteResult } from 'mongodb'
import { FilterQuery, HydratedDocument, Model, ProjectionType, Types } from 'mongoose'
import { FilterQuery, HydratedDocument, Model, mongo, ProjectionType, Types } from 'mongoose'
import { Controller as TsoaController } from 'tsoa'
import { Base64 } from '../../common/scripts.js'
import { GETResponse, Meta, User, _id } from '../../common/types.js'
import { _id, GETResponse, Meta, User } from '../../common/types.js'
import { ConflictError, NotAllowedError, NotFoundError } from './error.js'
import { IdDocument } from './types.js'

Expand Down Expand Up @@ -55,8 +55,8 @@ type SetterPartial<T, U extends string> = T extends object
| boolean[]
| Types.ObjectId[]
? T[P] | undefined | null
: T[P] extends Types.Buffer | null | undefined
? Data | Types.Buffer | undefined
: T[P] extends mongo.Binary | null | undefined
? Data | mongo.Binary | undefined
: T[P] extends (infer ElementType)[] | null | undefined
? ElementType extends { _id: infer idType extends _id | string }
? _SetterPartial2<ElementType, U>[] | IdDocument<idType>[] | null | undefined
Expand All @@ -83,8 +83,8 @@ type _SetterPartial2<T, U extends string> = T extends object
| boolean[]
| Types.ObjectId[]
? T[P] | undefined | null
: T[P] extends Types.Buffer | null | undefined
? Data | Types.Buffer | undefined
: T[P] extends mongo.Binary | null | undefined
? Data | mongo.Binary | undefined
: T[P] extends (infer ElementType)[] | null | undefined
? ElementType extends { _id: infer idType extends _id | string }
? _SetterPartial2<ElementType, U>[] | IdDocument<idType>[] | null | undefined
Expand All @@ -111,8 +111,8 @@ type _SetterPartial1<T, U extends string> = T extends object
| boolean[]
| Types.ObjectId[]
? T[P] | undefined | null
: T[P] extends Types.Buffer | null | undefined
? Data | Types.Buffer | undefined
: T[P] extends mongo.Binary | null | undefined
? Data | mongo.Binary | undefined
: T[P] extends (infer ElementType)[] | null | undefined
? ElementType extends { _id: infer idType extends _id | string }
? _SetterPartial2<ElementType, U>[] | IdDocument<idType>[] | null | undefined
Expand Down
9 changes: 4 additions & 5 deletions backend/controller/documentFileController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Request as ExRequest } from 'express'
import { mongo } from 'mongoose'
import { Delete, Get, Produces, Query, Request, Route, Security, SuccessResponse } from 'tsoa'
import { _id, documentFileTypes } from '../../common/types.js'
import DocumentFile from '../models/documentFile.js'
Expand All @@ -18,8 +17,8 @@ export class DocumentFileController extends Controller {
const file = await DocumentFile.findOne({ _id: _id }).lean()
if (file && request.user!._id.equals(file.owner._id)) {
request.res?.setHeader('Content-Type', file.type)
request.res?.setHeader('Content-Length', (file.data as any as mongo.Binary).length().toString())
request.res?.send((file.data as any as mongo.Binary).buffer)
request.res?.setHeader('Content-Length', file.data.length().toString())
request.res?.send(file.data.buffer)
} else {
throw new NotAllowedError()
}
Expand All @@ -45,8 +44,8 @@ export class DocumentFileAdminController extends Controller {
const file = await DocumentFile.findOne({ _id: _id }).lean()
if (file) {
request.res?.setHeader('Content-Type', file.type)
request.res?.setHeader('Content-Length', (file.data as any as mongo.Binary).length().toString())
request.res?.send((file.data as any as mongo.Binary).buffer)
request.res?.setHeader('Content-Length', file.data.length().toString())
request.res?.send(file.data.buffer)
} else {
throw new NotFoundError('No file found')
}
Expand Down
4 changes: 2 additions & 2 deletions common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Types } from 'mongoose'
import { mongo, Types } from 'mongoose'

/**
* @pattern ^[0-9a-fA-F]{24}$
Expand Down Expand Up @@ -157,7 +157,7 @@ export interface Place {
}

export interface DocumentFile<T extends DocumentFileType = DocumentFileType> {
data: Types.Buffer
data: mongo.Binary
owner: _id
type: T
name: string
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/costumModules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ declare module 'mongoose' {
type ObjectId = string
type Buffer = Blob
}
namespace mongo {
type Binary = Blob
}
}

0 comments on commit cf3e602

Please sign in to comment.