use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.
the class TempResourceActionTest method createEmptyFile.
/**
* Creates an empty file at the specified path.
*
* @param path the file path
* @throws IOException if creating the empty file fails
*/
private void createEmptyFile(String path) throws IOException {
File emptyFile = new TemporaryFile(new File(base, path));
emptyFile.getParentFile().mkdirs();
emptyFile.createNewFile();
}
use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.
the class TempResourceActionTest method createFile.
/**
* Creates a file at the specified path, with the specified content.
*
* @param path the file path
* @throws IOException if creating the empty file fails
*/
private void createFile(String path, String content) throws IOException {
File file = new TemporaryFile(new File(base, path));
file.getParentFile().mkdirs();
file.createNewFile();
FileUtils.write(file, content);
}
use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.
the class MailSenderPlugin method createAttachmentBodyPart.
/**
* Add attachments to a multipart message
*
* @param attachment the attachment to create the body part for.
* @param context the XWiki context.
* @return the body part for the given attachment.
*/
public MimeBodyPart createAttachmentBodyPart(Attachment attachment, XWikiContext context) throws XWikiException, IOException, MessagingException {
String name = attachment.getFilename();
byte[] stream = attachment.getContent();
File temp = new TemporaryFile(File.createTempFile("tmpfile", ".tmp"));
FileOutputStream fos = new FileOutputStream(temp);
fos.write(stream);
fos.close();
DataSource source = new FileDataSource(temp);
MimeBodyPart part = new MimeBodyPart();
String mimeType = MimeTypesUtil.getMimeTypeFromFilename(name);
part.setDataHandler(new DataHandler(source));
part.setHeader("Content-Type", mimeType);
part.setFileName(name);
part.setContentID("<" + name + ">");
part.setDisposition("inline");
return part;
}
use of com.xpn.xwiki.internal.file.TemporaryFile in project xwiki-platform by xwiki.
the class AttachmentMimeBodyPartFactory method createTemporaryAttachmentDataSource.
private DataSource createTemporaryAttachmentDataSource(Attachment attachment) throws MessagingException {
File temporaryAttachmentFile;
FileOutputStream fos = null;
try {
temporaryAttachmentFile = new TemporaryFile(File.createTempFile("attachment", ".tmp", this.temporaryDirectory));
fos = new FileOutputStream(temporaryAttachmentFile);
fos.write(attachment.getContent());
} catch (Exception e) {
throw new MessagingException(String.format("Failed to save attachment [%s] to the file system", attachment.getFilename()), e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
// Only an error at closing, we continue
this.logger.warn("Failed to close the temporary file attachment when sending an email. " + "Root reason: [{}]", ExceptionUtils.getRootCauseMessage(e));
}
}
return new FileDataSource(temporaryAttachmentFile);
}
Aggregations