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

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

Closed
etomarat opened this issue May 2, 2022 · 4 comments
Closed

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

etomarat opened this issue May 2, 2022 · 4 comments

Comments

@etomarat
Copy link

etomarat commented May 2, 2022

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
Copy link
Author

etomarat commented May 2, 2022

i submitted this issue here SoftwareBrothers/adminjs-upload#45

@etomarat etomarat closed this as completed May 2, 2022
@aiperi
Copy link

aiperi commented May 6, 2024

How did you solve this?

@etomarat
Copy link
Author

etomarat commented May 6, 2024

Насколько помню, я убрал вложенность. Сделал

{
media_big,
media_small
}

Вместо

{
media: {
big,
small
}
}

Давно это было, не помню уже в точности. Сори за ответ на русском и плохое форматирование. Пишу с телефона 🫠

@aiperi
Copy link

aiperi commented May 6, 2024 via email

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

No branches or pull requests

2 participants