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

upload multiple file #41

Open
aliazimoshan opened this issue Mar 6, 2022 · 1 comment
Open

upload multiple file #41

aliazimoshan opened this issue Mar 6, 2022 · 1 comment

Comments

@aliazimoshan
Copy link

Hi,
When I set multiple to true, in the admin panel I can get multiple files,
I even can log the path of the files in the custom provider, it prints all the selected file paths,
But the data is not stored in the database,

    "@adminjs/express": "^4.0.3",
    "@adminjs/mongoose": "^2.0.2",
    "@adminjs/upload": "^2.0.2",
    "adminjs": "^5.7.3",
      features: [
        uploadFeature({
          multiple: true,
          provider,
          properties: {
            key: "image.path",
            bucket: "image.folder",
            mimeType: "image.type",
            size: "image.size",
            filename: "image.filename",
            //file: "uploadFile",
          },
          uploadPath: (record, filename) =>
            `assets/products/${record.id()}/${filename}`,
          validation: { mimeTypes: ["image/png", "image/jpeg", "image/jpg"] },
        }),
      ],

and my custom provider

import { BaseProvider } from "@adminjs/upload";
//import { ActionContext, UploadedFile } from "adminjs";
import { promises, existsSync } from "fs";
import { resolve, dirname } from "path";

export class UploadProvider extends BaseProvider {
  assetPath;
  constructor(bucket, assetPath) {
    super(bucket);

    this.assetPath = assetPath;
  }

  async upload(file, key, context) {
    const fullPath = resolve(this.assetPath, key);
    const dirPath = dirname(fullPath);

    if (!existsSync(dirPath)) {
      await promises.mkdir(dirPath, { recursive: true });
    }

    await promises.copyFile(file.path, fullPath);
    await promises.unlink(file.path);
    return key;
  }

  async delete(key, bucket, context) {
    const filePath = resolve(this.assetPath, key);

    if (existsSync(filePath)) {
      await promises.unlink(filePath);
      const dirPath = dirname(filePath);
      const otherFiles = await promises.readdir(dirPath);
      if (otherFiles && otherFiles.length == 0) {
        await promises.rmdir(dirPath);
      }
    }
  }

  path(key, bucket, context) {
    return "/" + bucket + "/" + key;
  }
}

Thanks for your attention.

@KevinKoetz
Copy link

How does your schema look like?
The way your uploadFeature properties are configures image needs to be an object containing the path, folder, type and size paths, which need to be arrays:

const modelWithImages = model(
  "WithImages",
  new Schema({
    image: {
      key: [String],
      mimeType: [String],
      bucket: [String],
      size: [Number],
    },
  })
);

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