sor4chi / hono-storage

A storage helper for Hono. Support disk, memory, S3, etc.
MIT License
101 stars 3 forks source link

[@hono-storage/node-disk] How do I dynamically create directories #54

Closed ringo360 closed 3 months ago

ringo360 commented 4 months ago

I tried to create a directory based on the information I read from body.

const storage = new HonoDiskStorage({
  dest: async (c) => {
    const body = await c.req.parseBody();
    const username = body.username
    const userDir = path.join('./uploads', username as string);

    if (!fs.existsSync(userDir)) {
        fs.mkdirSync(userDir, { recursive: true });
    }

    return userDir;
  },
  filename: (_, file) => `${file.originalname}.${file.extension}`,
});

But it didn't work.

Log(when I tried to upload the file)

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of Promise
    at mkdir (node:internal/fs/promises:847:10)
    at C:\Users\ringoxd\GitHub\mediaman\node_modules\@hono-storage\node-disk\dist\index.cjs:40:45
    at Array.map (<anonymous>)
    at Object.storage (C:\Users\ringoxd\GitHub\mediaman\node_modules\@hono-storage\node-disk\dist\index.cjs:38:17)
    at HonoDiskStorage.handleSingleStorage (C:\Users\ringoxd\GitHub\mediaman\node_modules\@hono-storage\core\dist\index.cjs:82:26)
    at C:\Users\ringoxd\GitHub\mediaman\node_modules\@hono-storage\core\dist\index.cjs:108:20
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async dispatch (C:\Users\ringoxd\GitHub\mediaman\node_modules\hono\dist\cjs\compose.js:51:17)
    at <anonymous> (c:\Users\ringoxd\GitHub\mediaman\src\index.ts:94:4)
    at async dispatch (C:\Users\ringoxd\GitHub\mediaman\node_modules\hono\dist\cjs\compose.js:51:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}
  --> POST /upload 500 53ms
node:path:433
      validateString(arg, 'path');
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at Object.join (node:path:433:7)
    at HonoDiskStorage.dest (c:\Users\ringoxd\GitHub\mediaman\src\index.ts:37:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v21.2.0

This code also causes type errors.

Cannot assign type '(c: Context<any, any, {}>) => Promise' to type 'string | HDSCustomFunction | undefined'. Cannot assign type '(c: Context<any, any, {}>) => Promise' to type 'HDSCustomFunction'. Cannot assign type 'Promise' to type 'string'.

Is this my code issue?

I am using:

"@hono-storage/node-disk": "0.0.13",
"@hono/node-server": "1.11.1",
"fs": "0.0.1-security",
"hono": "4.3.7",
"path": "0.12.7"
ringo360 commented 4 months ago

✅ Ask googled? - Yes ✅ Tried another method? - Yes(I tried to use @hono-storage/core, but failed because my skill is too bad) ✅ Ask GPT(4)? - Yes(lol) ✅ Read Example? - Yes

sor4chi commented 4 months ago

This is certainly an omission in the Hono Storage implementation, Sorry. I’ll fix this because dest option cannot take asynchronous functions!

ringo360 commented 4 months ago

Thanks <3

sor4chi commented 3 months ago

Hi, @ringo360 This would have been resolved by @hono-storage/node-disk@0.14.0. Thank you!

ringo360 commented 3 months ago

Thank you for the fast support! 🩵