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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use ::Context;
use ::Job;
use ::JsonResponse;
pub use imageflow_types::Framewise;
use ::internal_prelude::works_everywhere::*;
#[derive(Default)]
pub struct LibClient {
}
#[derive(Clone, PartialEq, Debug)]
pub struct BuildInput<'a> {
pub io_id: i32,
pub bytes: &'a [u8],
}
#[derive(Clone, PartialEq, Debug)]
pub struct BuildOutput {
pub io_id: i32,
pub bytes: Vec<u8>,
pub mime_type: String,
pub file_ext: String,
pub w: Option<u32>,
pub h: Option<u32>,
}
#[derive(Clone, PartialEq, Debug)]
pub struct BuildRequest<'a> {
pub inputs: Vec<BuildInput<'a>>,
pub framewise: s::Framewise,
pub export_graphs_to: Option<std::path::PathBuf>,
}
#[derive(Clone, PartialEq, Debug)]
pub struct BuildSuccess {
pub outputs: Vec<BuildOutput>,
}
#[derive(Debug, PartialEq)]
pub enum BuildFailure {
OutOfMemory,
TransportFailure,
Error { httpish_code: i32, message: String },
}
impl From<::FlowError> for BuildFailure {
fn from(e: ::FlowError) -> BuildFailure {
match e {
FlowError::Oom => BuildFailure::OutOfMemory,
other => {
BuildFailure::Error {
httpish_code: 500,
message: format!("{:?}", other),
}
}
}
}
}
impl BuildFailure {
fn from_parse_error(http_code: i32,
prefix: String,
error: serde_json::error::Error,
json: &[u8])
-> BuildFailure {
let message = format!("{}: {}\n Parsing {}",
prefix,
error,
std::str::from_utf8(json).unwrap_or("[INVALID UTF-8]"));
BuildFailure::Error {
httpish_code: http_code,
message: message,
}
}
}
impl LibClient {
pub fn new() -> LibClient {
LibClient {}
}
pub fn get_image_info(&mut self,
bytes: &[u8])
-> std::result::Result<s::ImageInfo, BuildFailure> {
let context = Context::create()?;
let result = {
let mut job = context.create_job();
job.add_input_bytes(0, bytes)?;
let info_blob: JsonResponse =
job.message("v0.1/get_image_info", b"{\"io_id\": 0}")?;
let info_response: s::Response001 =
match serde_json::from_slice(info_blob.response_json.as_ref()){
Ok(v) => v,
Err(e) =>{
panic!("Failed to parse JSON response {:?} {:?}", e, str::from_utf8(info_blob.response_json.as_ref()));
}
};
if !info_response.success {
panic!("get_image_info failed: {:?}", info_response);
}
match info_response.data {
s::ResponsePayload::ImageInfo(info) => Ok(info),
_ => {
Err(BuildFailure::Error {
httpish_code: 500,
message: "Endpoint failed to return imageinfo".to_owned(),
})
}
}
};
context.destroy_allowing_panics();
result
}
pub fn build(&mut self, task: BuildRequest) -> std::result::Result<BuildSuccess, BuildFailure> {
let context = Context::create()?;
let result = {
let mut job = context.create_job();
for input in task.inputs {
job.add_input_bytes(input.io_id, input.bytes)?;
}
for node in task.framewise.clone_nodes() {
if let s::Node::Encode { ref io_id, .. } = *node {
job.add_output_buffer(*io_id)?;
}
if let s::Node::CommandString { ref encode, ..} = *node{
if let &Some(io_id) = encode{
job.add_output_buffer(io_id)?;
}
}
}
let send_execute = s::Execute001 {
framewise: task.framewise,
graph_recording: match task.export_graphs_to {
Some(_) => Some(s::Build001GraphRecording::debug_defaults()),
None => None,
},
no_gamma_correction: None,
};
let send_execute_str = serde_json::to_string_pretty(&send_execute).unwrap();
let result_blob: JsonResponse =
job.message("v0.1/execute", send_execute_str.as_bytes())?;
let result: s::Response001 =
match serde_json::from_slice(result_blob.response_json.as_ref()) {
Ok(r) => r,
Err(e) => {
return Err(BuildFailure::from_parse_error(500, "Error parsing libimageflow response".to_owned(), e, result_blob.response_json.as_ref()));
}
};
if !result.success {
return Err(BuildFailure::Error {
httpish_code: 500,
message: format!("v0.1/execute failed: {:?}", result),
});
}
let encodes: Vec<s::EncodeResult> = match result.data {
s::ResponsePayload::JobResult(s::JobResult { encodes }) => encodes,
_ => {
return Err(BuildFailure::Error {
httpish_code: 500,
message: "Endpoint failed to return JobResult".to_owned(),
})
}
};
let mut outputs = Vec::new();
for encode in encodes {
outputs.push(BuildOutput {
bytes: job.io_get_output_buffer_copy(encode.io_id)?,
io_id: encode.io_id,
mime_type: encode.preferred_mime_type,
file_ext: encode.preferred_extension,
w: Some(encode.w as u32),
h: Some(encode.h as u32),
});
}
Ok(BuildSuccess { outputs: outputs })
};
context.destroy_allowing_panics();
result
}
}
#[test]
fn test_stateless() {
let png_bytes = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00,
0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00,
0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82];
let req = BuildRequest {
inputs: vec![BuildInput {
io_id: 0,
bytes: &png_bytes,
}],
export_graphs_to: None,
framewise: Framewise::Steps(vec![s::Node::Decode {
io_id: 0,
commands: None,
},
s::Node::Encode {
io_id: 1,
preset: s::EncoderPreset::libpng32(),
}]),
};
let result = LibClient {}.build(req).unwrap();
assert!(result.outputs.len() == 1);
}