GithubHelp home page GithubHelp logo

Comments (2)

TimMikeladze avatar TimMikeladze commented on July 20, 2024

This is indeed an oversight. PRs welcome.

from next-upload.

vegandiet705 avatar vegandiet705 commented on July 20, 2024

@TimMikeladze Actually for my use case, I think it's better to use a server action like the following:

'use server'

import { nup } from '@/app/upload/nup';

async function idExistInBucket(id: string): Promise<boolean> {
    try {
        await nup.init();
        await nup.getAsset({ id });
    }
    catch (error) {
        return false;
    }

    return true;
}

export async function idsExistInBucket(ids: string[]): Promise<Map<string, boolean>> {
    const resultMap = new Map<string, boolean>();
    const promises = ids.map(async id => {
        const exists = await idExistInBucket(id);
        resultMap.set(id, exists);
    });

    await Promise.all(promises);

    return resultMap;
}

With this server action, I can do the existence verification before uploading files in the client, avoiding upload requests for already existent files:

    const uploadFiles = async () => {
        const orderAndFilesArray = await cacheOperator.getOrderAndFilesArray(orderIds);

        await Promise.all(
            orderAndFilesArray.map(async (orderAndFiles) => {
                const files = orderAndFiles.files.map((file) => {
                    return { id: `${file.imageId}`, file: blobToFile(arrayBufferToBlob(file.file, file.type), `${file.imageId}`) };
                });
                const filesIds = files.map(file => file.id);
                const resultMap = await idsExistInBucket(filesIds);
                const uploadPromises = [];

                for (const file of files) {
                    if (!resultMap.get(file.id)) {
                        uploadPromises.push(nup.upload(file));
                    }
                }

                await Promise.all(uploadPromises);
            })
        );
    };

This not only ensures error prevention but also idempotence. So, I'm retreating on my PR suggestion for now. But how do you think it should be done ? My idea was just adding a throw("Generic error") inside the upload's catch block, in the file src/react/useNextUpload.tsx.

from next-upload.

Related Issues (9)

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.