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

Handle duplicate files #183

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions backend/routes/api/patients.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ router.delete(
}),
);

const modifyFileName = async (filename, data) => {
var updatedFileName = filename;
var file = '';
var suffix = '';
Comment on lines +173 to +175

Choose a reason for hiding this comment

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

Use let here

if (filename.includes('.')) {
file = filename.split('.')[0];
suffix = '.' + filename.split('.')[1];
Comment on lines +177 to +178

Choose a reason for hiding this comment

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

This won't work for files with multiple . like .styles.js maybe use lastIndexOf() and substring to split around the last .

} else file = filename;
mattwalo32 marked this conversation as resolved.
Show resolved Hide resolved
if (data.filter((e) => e.filename === filename).length > 0) {

Choose a reason for hiding this comment

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

I would use if (data.some()) instead of filter since it's much clearer

var numPrev = 1;

Choose a reason for hiding this comment

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

let :)

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',
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The 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(),
});
Expand All @@ -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)*/
}
},
);
Expand Down