From NextJS to SvelteKit ⚡ #5544
PlopTheReal
started this conversation in
Show and tell
Replies: 1 comment
-
In case that can help some of you, here is an example of the logic I wrote to upload files: import busboy from 'busboy';
import { pipeline } from 'stream/promises';
export const post = async ({ request }) => {
const content = request.headers.get('content-type');
var percent = 0
var bytes = ""
var itemsCounter = 0, finished = false
let contentLenght = request.headers.get('content-length')
let contentLenghtKB = Math.round(contentLenght / 1024)
let uploadedSize = 0
var jobDone, jobRejected;
var task = new Promise(function (resolve, reject) {
jobDone = resolve;
jobRejected = reject;
});
const bb = busboy({
headers: {
'content-type': content
}
});
bb.on('file', async (name, file, info, mimetype) => {
const { filename, encoding, mimeType } = info;
console.log(
`File [${name}]: filename: %j, encoding: %j, mimeType: %j`,
filename,
encoding,
mimeType
);
++itemsCounter
let buffers = []
file.on('data', data => {
buffers.push(data);
uploadedSize += data.length
percent = Math.round(uploadedSize / contentLenght * 100)
bytes = `${Math.round(uploadedSize / 1024)} / ${contentLenghtKB}`
})
file.on('end', async () => {
try {
const file= Buffer.concat(buffers)
// DO WHATEVER YOU NEED WITH THE FILE
// I GIVE IT TO SHARP TO COMPRESS IMAGES OR FFMPEG FOR VIDEOS
if (--itemsCounter === 0 && finished) {
console.log('ALL DONE');
jobDone()
}
}
catch (err) {
console.error(err)
jobRejected(err)
}
})
});
bb.on('field', (name, val, info) => {
console.log(`Field [${name}]: value: %j`, val);
});
bb.on('finish', () => {
finished = true;
console.log('Done parsing form!');
});
await pipeline(request.body, bb);
const error = await task
if (error){
return{
status: 500,
body: error
}
}
return {
status: 200
};
}; Don't hesitate if you spot something that could be improved! svelte version: 1.0.0-next.369 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Just wanted to share my experience of migrating from a complete optimized NextJS+MobX CMS to SvelteKit and so far it's brilliant.
The reasons are that I'm focusing on reducing the data across the wire.
I still got a lot to do but at the moment of writing the size of the app (same features/pages comparison) has been reduce 3-4 times. 💪
Beta Was this translation helpful? Give feedback.
All reactions