Skip to content
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

Merged
merged 5 commits into from
Feb 18, 2020
Merged

Add Math.random() to storage filename example #841

merged 5 commits into from
Feb 18, 2020

Conversation

drazisil
Copy link
Contributor

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.

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())
Copy link
Member

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 🤔

@drazisil
Copy link
Contributor Author

drazisil commented Feb 17, 2020 via email

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]
Copy link
Member

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?

Copy link
Member

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 Show resolved Hide resolved
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])
Copy link
Member

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?

Copy link
Contributor Author

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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change from - to _?

Copy link
Contributor Author

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.

README.md Outdated Show resolved Hide resolved
@LinusU
Copy link
Member

LinusU commented Feb 18, 2020

Great, thanks!

@LinusU LinusU merged commit 92d83c1 into expressjs:master Feb 18, 2020
nevilm-lt pushed a commit to nevilm-lt/multer that referenced this pull request Apr 21, 2022
* 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
nevilm-lt pushed a commit to nevilm-lt/multer that referenced this pull request Apr 21, 2022
* 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
nevilm-lt pushed a commit to nevilm-lt/multer that referenced this pull request Apr 22, 2022
* 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
nevilm-lt pushed a commit to nevilm-lt/multer that referenced this pull request Apr 22, 2022
* 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
nevilm-lt pushed a commit to nevilm-lt/multer that referenced this pull request Apr 22, 2022
* 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
himanshiLt pushed a commit to himanshiLt/multer that referenced this pull request Jun 24, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants