adminjs-upload icon indicating copy to clipboard operation
adminjs-upload copied to clipboard

Storing multiple files in one model. What i am doing wrong?

Open etomarat opened this issue 3 years ago • 0 comments

Hello! I'm trying to upload two files to one model, and I ran into a problem: If you upload both files, only the last one is saved. If you upload one file, and then edit the entry and add another one, it will not be saved. This is my code:

import { Schema, model, Document } from 'mongoose';
import { ResourceWithOptions } from 'adminjs';
import uploadFeature from '@adminjs/upload';

interface IMedia extends Document {
  path: string,
  mimeType: string,
  size: number,
  filename: string,
  bucket: string,
}

interface IBrand extends Document {
  title: string,
  media: {
    big: IMedia,
    small: IMedia
  },
}

const mediaSchema = new Schema<IMedia>({
  path: { type: String },
  mimeType: String,
  size: Number,
  filename: String,
  bucket: String,
});

const brandSchema = new Schema<IBrand>({
  title: { type: String, required: true },
  media: {
    big: mediaSchema,
    small: mediaSchema
  },
});


const Brand = model<IBrand>('BrandExample', brandSchema)

export const brandExample: ResourceWithOptions = {
  resource: Brand,
  options: {},
  features: [
    uploadFeature({
      provider: {
        local: {
          bucket: 'uploads'
        }
      },
      properties: {
        key: `media.big.path`,
        mimeType: `media.big.mimeType`,
        size: `media.big.size`,
        bucket: `media.big.bucket`,
        filename: `media.big.filename`,
        file: `media.big.file`,
        filePath: `media.big.filePath`,
        filesToDelete: `media.big.filesToDelete`,
      },
      validation: {
        mimeTypes: ['image/jpeg', 'image/png']
      },
      uploadPath: (record, filename) => (
        `${record.id()}/media.big/${filename}`
      ),
    }),
    uploadFeature({
      provider: {
        local: {
          bucket: 'uploads'
        }
      },
      properties: {
        key: `media.small.path`,
        mimeType: `media.small.mimeType`,
        size: `media.small.size`,
        bucket: `media.small.bucket`,
        filename: `media.small.filename`,
        file: `media.small.file`,
        filePath: `media.small.filePath`,
        filesToDelete: `media.small.filesToDelete`,
      },
      validation: {
        mimeTypes: ['image/jpeg', 'image/png']
      },
      uploadPath: (record, filename) => (
        `${record.id()}/media.small/${filename}`
      ),
    })
  ]
};
image

Look like a bug or did I do something wrong?

etomarat avatar May 02 '22 13:05 etomarat