Code Monkey home page Code Monkey logo

Comments (12)

Murderlon avatar Murderlon commented on June 3, 2024 1

Thanks for sharing. We can't allow to override the entire upload, only metadata should be allowed, and the metadata should be validated as well. It's also important to make it backwards compatible to prevent a breaking change.

I'll take next week probably. Created an issue: #594

from tus-node-server.

Murderlon avatar Murderlon commented on June 3, 2024

Hi, this is an XY problem. What is the problem you are trying to solve? Are you only communicating between hooks or does the client also need to know this value?

Maybe you can just do it in server memory with a Map. Or if it needs to survive server restarts, put it in something like Redis?

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

I'm sorry, here is the problem:
I need to pass a variable from the onUploadCreate function to the onUploadFinish function. It does not need to be sent back to the client.

Is there a way to do this?

If not, then I'll look into your other suggestions, thank you!

from tus-node-server.

Murderlon avatar Murderlon commented on June 3, 2024

I don't know the requirements and why you are trying to pass that variable. There are many ways to write/read to different things depending on what you are doing, but those all fall into your hands and setup, there is no built-in way of doing this.

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

I'm sorry for being unclear.

I'm using the onUploadCreate function to validate the upload request, a process that generates a unique ID that I need to get in the onUploadFinish function.

Redis would be a way to do this, but I was hoping that I could simply update the metadata of the upload, but it sounds like that is not an option, correct?

from tus-node-server.

Murderlon avatar Murderlon commented on June 3, 2024

If you tell me why you need the ID to be accessible by the onUploadFinish hook, we can see if there are alternative approaches.

You can't change the metadata in onUploadCreate in a way that's persisted in the store too. We may want to support that at some point.

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

Once the upload is completed I create an entry for it, that includes the unique ID, in a separate database.

I'd be happy to share the code that I wrote to make the changes to the upload object persistent or even make a pull request if you think it'd help.
I've got it down to just adding two lines and changing two lines in the tus-node-server/packages/server/src/handlers/PostHandler.ts file.

The main issue with my approach is that the changes do not persist all the way back to the client (which isn't necessary in my case, but still).

from tus-node-server.

Murderlon avatar Murderlon commented on June 3, 2024

Once the upload is completed I create an entry for it, that includes the unique ID, in a separate database.

Can't you just validate the upload in onUploadCreate, reject if needed, and only create and store the ID in onUploadFinish without the need to communicate between hooks?

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

I would, but the ID is created as part of the validation process.
I'll just create a fork for myself with those small changes I mentioned, it should be simple to keep up to date with the main repo.

That said, would you like me to create a pull request with those changes?

The way it works is that the onUploadCreate function returns both the res object and the upload object to the PostHandler which then overwrites the upload object in that file same as the res object.

I'm not sufficiently familiar with the whole codebase to be sure that that's the best way to do it, what do you think?

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

For reference, here is the updated code:

        let upload = new models_1.Upload({
            id,
            size: upload_length ? Number.parseInt(upload_length, 10) : undefined,
            offset: 0,
            metadata,
        });
        if (this.options.onUploadCreate) {
            try {
                const afterUploadCreate = await this.options.onUploadCreate(req, res, upload);
                res = afterUploadCreate[0];
                upload = afterUploadCreate[1];
            }
            catch (error) {
                log(`onUploadCreate error: ${error.body}`);
                throw error;
            }
        }

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

Here is the fork: https://github.com/sammibajrami/tus-node-server

from tus-node-server.

sammibajrami avatar sammibajrami commented on June 3, 2024

No problem, thank you for taking the time to help me!

While I'm sure that there is a smoother way to do this, here is updated code that only overwrites upload.metadata, while also being backwards compatible:

        let upload = new models_1.Upload({
            id,
            size: upload_length ? Number.parseInt(upload_length, 10) : undefined,
            offset: 0,
            metadata,
        });
        if (this.options.onUploadCreate) {
            try {
                const afterUploadCreate = await this.options.onUploadCreate(req, res, upload);
                res = afterUploadCreate[0];
                if (afterUploadCreate[1]) {
	                upload.metadata = afterUploadCreate[1].metadata;
                }
            }
            catch (error) {
                log(`onUploadCreate error: ${error.body}`);
                throw error;
            }
        }

from tus-node-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.