-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add Math.random() to storage filename example #841
Conversation
I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files.
README.md
Outdated
@@ -175,7 +175,7 @@ var storage = multer.diskStorage({ | |||
cb(null, '/tmp/my-uploads') | |||
}, | |||
filename: function (req, file, cb) { | |||
cb(null, file.fieldname + '-' + Date.now()) | |||
cb(null, file.fieldname + '-' + Date.now() + '-' + Math.random()) |
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.
Hmm, this will produce filenames like: foo-1581931472193-0.5359727197825674
. I'm not sure how that is being handled since essentially the file extension of the file is now .5359727197825674
🤔
In linux there's no problem, but you are correct that Windows will
probably scream. I'll edit to remove the period.
…On Mon, Feb 17, 2020, 4:26 AM Linus Unnebäck ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In README.md
<#841 (comment)>:
> @@ -175,7 +175,7 @@ var storage = multer.diskStorage({
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
- cb(null, file.fieldname + '-' + Date.now())
+ cb(null, file.fieldname + '-' + Date.now() + '-' + Math.random())
Hmm, this will produce filenames like:
foo-1581931472193-0.5359727197825674. I'm not sure how that is being
handled since essentially the file extension of the file is now
.5359727197825674 🤔
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#841?email_source=notifications&email_token=AABOUCU4DXFBQVOXNCYVSP3RDJJ2RA5CNFSM4KWGLCI2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCVXOMII#pullrequestreview-359589409>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABOUCXQS4WFD3DOB5A7NGTRDJJ2RANCNFSM4KWGLCIQ>
.
|
README.md
Outdated
@@ -175,7 +175,8 @@ var storage = multer.diskStorage({ | |||
cb(null, '/tmp/my-uploads') | |||
}, | |||
filename: function (req, file, cb) { | |||
cb(null, file.fieldname + '-' + Date.now()) | |||
const uniqueHash = Date.now() + '-' + Math.random().split('.')[1] |
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.
Can we call this something other than "hash" since it isn't a hash?
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.
Math.random().split('.')[1]
this will throw an error:
TypeError: Math.random(...).split is not a function
README.md
Outdated
@@ -175,8 +175,8 @@ var storage = multer.diskStorage({ | |||
cb(null, '/tmp/my-uploads') | |||
}, | |||
filename: function (req, file, cb) { | |||
const uniqueHash = Date.now() + '-' + Math.random().split('.')[1] | |||
cb(null, file.fieldname + '-' + uniqueHash) | |||
const uniqueSuffix = Date.now() + '_' + (Math.random().toString().split('.')[1]) |
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.
What do you think about Math.round(Math.random() * 1E9)
instead of (Math.random().toString().split('.')[1])
, I feel that it's a bit easier to understand?
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.
If that works, I'm ok with it.
README.md
Outdated
@@ -175,8 +175,8 @@ var storage = multer.diskStorage({ | |||
cb(null, '/tmp/my-uploads') | |||
}, | |||
filename: function (req, file, cb) { | |||
const uniqueHash = Date.now() + '-' + Math.random().split('.')[1] | |||
cb(null, file.fieldname + '-' + uniqueHash) | |||
const uniqueSuffix = Date.now() + '_' + (Math.random().toString().split('.')[1]) |
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.
Why change from -
to _
?
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've run into issues where Javascript sees a dash and tries to do math. I figured it was safer, but i can change it back if you want.
Great, thanks! |
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
* Add Math.random() to storage filename example I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files. * Remove the period from the hash * Convert to string and split. Also rename * Update README.md * Math.round(Math.random() * 1E9) and revert dashes
I was encountering an issue where two files had the same name due to being handled in the same millisecond. While not a bug, adding extra uniqueness may save someone else time troubleshooting 'missing' files.