-
Notifications
You must be signed in to change notification settings - Fork 2
/
mimeattachment.class.js
117 lines (98 loc) · 2.8 KB
/
mimeattachment.class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Mock-up of the MimeAttachment class from VMware Aria Automation.
* Describe a mime attachment.
*
* @author Stefan Schnell <mail@stefan-schnell.de>
* @license MIT
* @version 0.2.1
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine.
*
* Checked with Rhino engines version 1.7R4, 1.7.14 and 1.7.15
*/
var MimeAttachment = function(file) {
if (
typeof file !== "undefined" &&
file !== null &&
String(file).trim() !== ""
) {
this.mimeType = "";
try {
this.name = System.extractFileName(file);
var path = java.nio.file.FileSystems.getDefault().getPath("", file);
this.buffer = java.nio.file.Files.readAllBytes(path);
this.content = String(java.lang.String(this.buffer));
} catch (exception) {
System.error(exception);
}
}
};
MimeAttachment.prototype = {
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @function getClassName
* @returns {string}
*
* @example
* var mimeAttachment = new MimeAttachment();
* var result = mimeAttachment.getClassName();
* System.log(result);
*/
getClassName : function() {
return "MimeAttachment";
},
/**
* Write the content of the mime attachment to file.
*
* @function write
* @param {string} directory - Directory where to store the file.
* @param {string} filename - Optional filename<br>
* (if null, use the mime attachment name)
* @returns {File}
*/
write : function(directory, filename) {
var context;
try {
if (
typeof directory === "undefined" ||
directory === null ||
String(directory).trim() === ""
) {
throw new Error("directory argument can not be undefined or null");
}
if (
typeof filename === "undefined" ||
filename === null ||
String(filename).trim() === ""
) {
filename = this.name;
}
var fullPath = System.appendToPath(String(directory), String(filename));
var contextFactory = org.mozilla.javascript.ContextFactory();
context = contextFactory.getGlobal().enterContext();
var path = java.nio.file.Paths.get(fullPath);
java.nio.file.Files.deleteIfExists(path);
java.nio.file.Files.createFile(path);
var outputStream = java.nio.file.Files.newOutputStream(path);
var className = "";
try {
className = this.buffer.getClassName();
} catch (exception) {
className = this.buffer.getClass().getName();
}
if ( className === "ByteBuffer" || className === "[B") {
outputStream.write(this.buffer._byteBuffer);
} else {
outputStream.write(this.buffer);
}
outputStream.close();
} catch (exception) {
System.log(exception);
} finally {
context.exit();
}
}
};