Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a size limit when saving binary data from Buffer with mongoose-field-encryption? #74

Open
bluepuma77 opened this issue Jun 9, 2022 · 1 comment
Assignees

Comments

@bluepuma77
Copy link

We would like to save images to MongoDB and want to encrypt the binary data field. And we are aware that the MongoDB object size limit is around 16MB (Stackoverflow info).

But even when the image only has 3.6MB I receive an error with file.save():
ERROR The value of "offset" is out of range. It must be >= 0 && <= 17825792. Received 17825794

// mongoose model
import mongoose from 'mongoose';
const { Schema, model } = mongoose;
import { v4 as uuidv4 } from 'uuid';
import { fieldEncryption } from 'mongoose-field-encryption';

export interface File extends Document {
    id: string,
    blob: Buffer,
    mime: string,
    size: number,
}

const FileSchema = new Schema<File>(
    {
        id: { type: String, required: true, unique: true, index: true, default: () => uuidv4()},
        blob: { type: Buffer },
        mime: { type: String },
        size: { type: Number },
    }
);

FileSchema.plugin(fieldEncryption, { fields: ['blob'], secret: process.env.DB_SECRET });

export default model<File>('Blob', FileSchema);

Is there a specific size limit when saving binary data from Buffer with mongoose-field-encryption?

@wheresvic
Copy link
Owner

Hi and sorry for the delay in responding to this. The issue at hand is that mfe does a JSON.stringify on a non-string field which then explodes in size for a Buffer.

I would highly recommend that you encrypt the files but store them somewhere else on a server and just save the encryption keys in Mongo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants