Comments (2)
This is indeed an oversight. PRs welcome.
from next-upload.
@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)
- Dependency Dashboard
- Drizzle MySQL Asset Store HOT 2
- Figure out how to store metadata purely in s3
- Upload files to a post policy url directly from the server HOT 3
- Replace minio-js client with an s3 client that supports edge runtimes
- 400 Bad Request when using with R2 HOT 6
- Prisma store HOT 1
- Integration tests which communicate with s3 and r2 as part of github actions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-upload.