Recently, we had someone upload a file that contained illegal characters in the name (double hyphen) which resulted in the inability to redownload the file. In this instance the file name was
Some name -- some other information
For the upload, the file name gets set by getting the original file name which is a business rule.
file.setFileName(file.getFile().getOriginalFilename());
This resulted in the double hyphen becoming two upside down question marks, and for whatever reason resulted in the inability to retrieve the file back from the server.
I'm wondering if there is a programmatic solution to check the original file name for situation like this.
For transparency, here is the code for uploading the file:
public void saveOpcertCeuFile(OpcertCeuFileUpload file) {
UmdContact user = secUtilService.getActiveUser();
String username = user.getEmail();
Date now = new Date();
file.setCreatedTs(now);
file.setLastUpdatedTs(now);
file.setCreatedBy(username);
file.setLastUpdatedBy(username);
file.setFileName(file.getFile().getOriginalFilename());
file.setIsApproved(Boolean.FALSE);
file.setIsDeleted(Boolean.FALSE);
try {
file.setByteContents(file.getFile().getBytes());
} catch (Exception ex) {
log.info(ex);
throw new RuntimeException(ex);
}
dao.insertOpcertCeuFileUpload(file);
Path path = this.getOptcertCeuFilePath(file);
String configF = envService.getServerUrl();
file.setFilePath(String.valueOf(path));
dao.updateOpcertCeuFilePath(file);
try {
File file1 = path.toFile();
file1.getParentFile().mkdirs();
Files.write(path, file.getByteContents(), StandardOpenOption.CREATE_NEW);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
sanitizeFilename(String)and use it like this:file.setFileName(sanitizeFilename(file.getFile().getOriginalFilename()));. Better: use your own naming system for your filesystem. If you want to retrieve the original filename later, store it as metadata instead of the filename itself.