in genie-web/src/main/java/com/netflix/genie/web/services/impl/LocalFileSystemAttachmentServiceImpl.java [73:142]
public Set<URI> saveAttachments(
@Nullable final String jobId,
final Set<Resource> attachments
) throws SaveAttachmentException {
if (attachments.isEmpty()) {
return Sets.newHashSet();
}
final Path attachmentsBasePath = this.attachmentsDirectoryPath.resolve(UUID.randomUUID().toString());
try {
Files.createDirectories(attachmentsBasePath);
} catch (IOException e) {
throw new SaveAttachmentException("Failed to create directory for attachments: " + e.getMessage(), e);
}
long totalSize = 0;
final ImmutableSet.Builder<URI> setBuilder = ImmutableSet.builder();
for (final Resource attachment : attachments) {
try (InputStream inputStream = attachment.getInputStream()) {
final long attachmentSize = attachment.contentLength();
final String filename = attachment.getFilename();
if (filename != null) {
if ((filename.contains("/") || filename.contains("\\")
|| filename.equals(".") || filename.contains(".."))) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "Filenames should not be ., or contain .., /, \\.");
}
final String attachmentCanonicalPath =
createTempFile(String.valueOf(attachmentsBasePath), filename).getCanonicalPath();
final String baseCanonicalPath =
new File(String.valueOf(attachmentsBasePath)).getCanonicalPath();
if (!attachmentCanonicalPath.startsWith(baseCanonicalPath)
|| attachmentCanonicalPath.equals(baseCanonicalPath)) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "Filenames should not be a relative path.");
}
}
if (attachmentSize > this.attachmentServiceProperties.getMaxSize().toBytes()) {
throw new AttachmentTooLargeException("Attachment is too large: " + filename);
}
totalSize += attachmentSize;
if (totalSize > this.attachmentServiceProperties.getMaxTotalSize().toBytes()) {
throw new AttachmentTooLargeException("Attachments total size is too large");
}
final Path attachmentPath = attachmentsBasePath.resolve(
filename != null ? filename : UUID.randomUUID().toString()
);
Files.copy(inputStream, attachmentPath);
setBuilder.add(attachmentPath.toUri());
} catch (IOException e) {
throw new SaveAttachmentException("Failed to save attachment: " + e.getMessage(), e);
}
}
return setBuilder.build();
}