-
Notifications
You must be signed in to change notification settings - Fork 0
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
Handle duplicate files #183
base: master
Are you sure you want to change the base?
Changes from 5 commits
f20d8c2
ef14806
57d6f52
cb46806
a8346b1
a7a4f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,26 @@ router.delete( | |
}), | ||
); | ||
|
||
const modifyFileName = async (filename, data) => { | ||
var updatedFileName = filename; | ||
var file = ''; | ||
var suffix = ''; | ||
if (filename.includes('.')) { | ||
file = filename.split('.')[0]; | ||
suffix = '.' + filename.split('.')[1]; | ||
Comment on lines
+177
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work for files with multiple |
||
} else file = filename; | ||
mattwalo32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (data.filter((e) => e.filename === filename).length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use |
||
var numPrev = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
while ( | ||
data.filter((e) => e.filename === file + '_' + numPrev + suffix) | ||
.length > 0 | ||
) | ||
numPrev += 1; | ||
updatedFileName = file + '_' + numPrev + suffix; | ||
} | ||
return updatedFileName; | ||
}; | ||
|
||
// POST: upload individual files | ||
router.post( | ||
'/:id/files/:stepKey/:fieldKey/:fileName', | ||
|
@@ -211,8 +231,12 @@ router.post( | |
if (err) { | ||
res.json(err); | ||
} else { | ||
const modifiedFileName = await modifyFileName( | ||
fileName, | ||
stepData[fieldKey], | ||
); | ||
Comment on lines
+236
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I were looking at this without context, i wouldnt understand what this function does. Think a one line comment explaining what this function does would be super helpful! |
||
stepData[fieldKey].push({ | ||
filename: fileName, | ||
filename: modifiedFileName, | ||
uploadedBy: req.user.Username, | ||
uploadDate: Date.now(), | ||
}); | ||
|
@@ -232,13 +256,14 @@ router.post( | |
success: true, | ||
message: 'File successfully uploaded', | ||
data: { | ||
name: fileName, | ||
name: modifiedFileName, | ||
uploadedBy: req.user.Username, | ||
uploadDate: Date.now(), | ||
mimetype: file.mimetype, | ||
size: file.size, | ||
}, | ||
}); | ||
/*console.log(res.data)*/ | ||
} | ||
}, | ||
); | ||
|
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.
Use
let
here