Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How would I go about unzipping zip files? And zipping them back up?

default discord avatar
zochhlast year
22

I have a collection with an upload field where only zip files are accepted. They get saved to a folder

/files

and inside it theres another dir

/unzipped

. How do I unzip the file after it gets uploaded and saved to the

/unzipped

dir? I've tried using

yauzl

,

decompressed

and

zlib

but I keep getting errors from webpack(?) about adding something like

{ "stream": false }

or it requiring something for zlib, etc. After setting the keys that were shown in the error page to false the frontend UI fails to load and I get a blank white page. Is there a way to unzip and zip?

  • default discord avatar
    deanpetre11 months ago

    I was facing the same issue with you and found the solution through extensive research for couple of days.(In my case, I was using

    extract-zip

    module which rely on

    yauzl

    )


    1. Create a mock module to alias the server-only modules.


    //emptyModuleMock.js
    module.exports = {
      url: () => {},
      raw: () => {},
    };


    2. Configure aliasing in the payload admin webpack configuration.(Change the server-only module path to be aliased as per your recommendation)


    //payload.config.ts
    const mockModulePath = path.resolve(__dirname, './emptyModuleMock');
    ...
    const config = buildConfig({
      admin: {
        webpack: (config) => ({
          resolve: {
            ...config.resolve,
            alias: {
              ...config.resolve.alias,
              //This is for my case, you can change the following line to [path.resolve(__dirname, '../node_modules/yauzl')]: mockModulePath,
              [path.resolve(__dirname, '../node_modules/extract-zip')]: mockModulePath,
            },
          },
        }),
      }
    });

    3. If changes to your Webpack aliases are not surfacing, they might be cached in node_modules/.cache/webpack. Try deleting that folder and restarting your server.

  • default discord avatar
    zochh11 months ago

    Hope you don't mind the ping. Thank you for the answer- I will try your suggestion in a bit. Had to run a python server for managing the zip files haha

  • default discord avatar
    deanpetre11 months ago

    you are welcome

  • default discord avatar
    zochh11 months ago

    Hi again, finally got around to trying it with

    extract-zip

    itself. I keep getting the error that is present in the second screenshot.



    yarn run v1.22.21
    $ cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon
    [nodemon] 2.0.22
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: ts
    [nodemon] starting `ts-node src/server.ts -- -I`
    (node:86591) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    
    /home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/express/admin.ts:8
          ctx.express.use(ctx.config.routes.admin, await ctx.config.admin.bundler.dev(ctx))
                                                                                  ^
    TypeError: Cannot read properties of undefined (reading 'dev')
        at initAdmin (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/express/admin.ts:8:79)
        at initHTTP (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/initHTTP.ts:59:20)
        at processTicksAndRejections (node:internal/process/task_queues:95:5)
        at Payload.init (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/index.ts:17:21)
    [19:17:03] INFO (payload): Connected to MongoDB server successfully!
    [19:17:03] INFO (payload): Starting Payload...
    [nodemon] app crashed - waiting for file changes before starting...


    Text format if you want



    Have deleted the .cache/webpack and reinstall all the node modules



    (Please do ping when replying)

    image.png
    image.png
  • default discord avatar
    notchr11 months ago

    @Zoclhas Whats the act issue here?



    It seems like an issue in admin.ts



    or initHttp

  • default discord avatar
    zochh11 months ago

    I've been trying to unzip a file after its been uploaded to payload but keep coming up with webpack errors or the FE is blank

  • default discord avatar
    notchr11 months ago

    OK, when do you hit webpack errors?



    Just loading the module?

  • default discord avatar
    zochh11 months ago

    A few minutes. I need to re-setup the environment

  • default discord avatar
    notchr11 months ago

    Sure

  • default discord avatar
    zochh11 months ago

    Okay here we are. Without this part of the code the app launches with

    yarn dev

    . But once I add it I get this error:



    (ignore the commented part)

  • default discord avatar
    notchr11 months ago

    can you do



    node -v



    whats your node version?

  • default discord avatar
    zochh11 months ago

    21.5.0

  • default discord avatar
    notchr11 months ago

    I see



    Webpack right?



    You may need to setup resolution fallbacks for 'util' and 'stream'

  • default discord avatar
    zochh11 months ago

    How do I do that?

  • default discord avatar
    notchr11 months ago
    https://payloadcms.com/docs/admin/excluding-server-code#aliasing-server-only-modules


    maybe something like



    module.exports = {
      // ...
      resolve: {
          fallback: {
            util: false,
            stream: false
          }
      }
      // ...
    };


    I could be wrong, but i think like that will exclude it from the client?

  • default discord avatar
    zochh11 months ago

    Woah, new docs



    Same error as this



    Does this have something to do with Payload 2.0?



    I've moved the afterChange hook to a separate file and did the same thing



    getting the same error



    Would it better so that after each file upload I call an endpoint that is only exposed locally with the file path. Then the file unzips it and does the other operations?

  • default discord avatar
    deanpetre11 months ago

    That is exactly same what I was doing with

    extract-zip

    . If the alias was configured correctly, you should not see the webpack errors originated from

    extract-zip

    . Just ignore @notchrisis's fallback configuration because if

    extract-zip

    get aliased correctly, webpack will never require

    util

    or

    stream

    . Please concentrate on confirming the aliasing work correctly.

  • default discord avatar
    zochh11 months ago

    I've followed what you said, but still getting the same error

  • default discord avatar
    deanpetre11 months ago

    Can you share your screen?



    add

    console.log(path.resolve(__dirname, '../node_modules/extract-zip'))

    to your config file to check you are aliasing the correct route

  • default discord avatar
    zochh11 months ago


    and extract zip is present in node_modules



    Oh wait, something new now



    ERROR in ./src/collections/DownloadAfterHook.ts 125:0-34
    Module not found: Error: Can't resolve 'extract-zip' in '/home/zoc/Desktop/code/zaurastudios/payload/src/collections'


    and this too

    image.png
    image.png
    image.png
  • default discord avatar
    deanpetre11 months ago

    It would be easy to assist you if you can share your screen...



    I think your aliasing is not working correctly yet.

  • default discord avatar
    zochh11 months ago

    There needed to be a

    ...config

    above

    resolve:


    works now

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.