-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(server): download assets #3032
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
918c767
to
7065635
Compare
const paths: Record<string, boolean> = {}; | ||
|
||
for (const { originalPath, originalFileName } of assets) { | ||
const ext = extname(originalPath); | ||
let filename = `${originalFileName}${ext}`; | ||
for (let i = 0; i < 10_000; i++) { | ||
if (!paths[filename]) { | ||
break; | ||
} | ||
filename = `${originalFileName}+${i + 1}${ext}`; | ||
} | ||
|
||
paths[filename] = true; | ||
zip.addFile(originalPath, filename); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am late with my review, but it seems if we change the type of the Record
to Record<string, number>
the code looks cleaner:
const paths: Record<string, number> = {};
for (const { originalPath, originalFileName } of assets) {
const ext = extname(originalPath);
const baseName = `${originalFileName}${ext}`;
let filename = baseName;
let index = paths[baseName];
if (index === undefined) {
index = 0;
} else {
index++;
filename = `${originalFileName}+${index}${ext}`;
}
paths[baseName] = index;
zip.addFile(originalPath, filename);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a shame as Array.prototype.group would make this quite nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because it was merged doesn't mean it can't be fixed anymore lol 😛
Do you have an example of how array.group would look? I've never used it before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
group is still experimental, so I can implement the suggestion from @brighteyed instead.
Tested scenarios: