Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConverterServlet.java « online « mxgraph « com « java « main « src - github.com/jgraph/drawio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d010acaa6c8a4e3e3eb9921d13f59db347b583f9 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package com.mxgraph.online;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.nio.file.Path;
import java.nio.file.Paths;

//This servlet is an interface between draw.io and CloudConverter.
//For EMF files, it detect its size and resize the huge images such that max dimension is MAX_DIM
public class ConverterServlet  extends HttpServlet 
{
	private static final long serialVersionUID = -5084595244442555865L;

	private static final Logger log = Logger
			.getLogger(HttpServlet.class.getName());
	
	private static final int MAX_DIM = 5000;
	private static final int MAX_FILE_SIZE = 50 * 1024 * 1024; // 50 MB
	private static final double EMF_10thMM2PXL = 26.458;
	private static final String API_KEY_FILE_PATH = "/WEB-INF/cloud_convert_api_key"; // Not migrated to new pattern, since will not be used on diagrams.net
	private static final String CONVERT_SERVICE_URL = "https://api.cloudconvert.com/convert";
	private static final String CRLF = "\r\n";
	private static final String TWO_HYPHENS = "--";
	private static final String BOUNDARY =  "----WebKitFormBoundary6XTanBMjO0kFwa3p"; //FIXME The boundary should not occur inside the file, it is very unlikely but still a possibility
	
	private static String API_KEY = null;
	
	private void readApiKey() 
	{
		if (API_KEY == null)
		{
			try 
			{
				API_KEY = Utils
							.readInputStream(getServletContext()
							.getResourceAsStream(API_KEY_FILE_PATH))
							.replaceAll("\n", "");
			} 
			catch (IOException e) 
			{
				throw new RuntimeException("Invalid API key file/path");
			}
		}
	}
	
	//Little Indian
	private int fromByteArray(byte[] bytes, int start) 
	{
		return ((bytes[start + 3] & 0xFF) << 24) | 
	           ((bytes[start + 2] & 0xFF) << 16) | 
	           ((bytes[start + 1] & 0xFF) << 8 ) | 
	           ((bytes[start] & 0xFF) << 0 );
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		readApiKey();
		
		String inputformat = null, outputformat = null, fileName = null;
		InputStream fileContent = null;
		
		try 
		{
	        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
	        
	        for (FileItem item : items) 
	        {
	            if (item.isFormField()) 
	            {
	                String fieldName = item.getFieldName();
	                
	                if ("inputformat".equals(fieldName))
	                {
	                	inputformat = item.getString();
	                }
	                else if ("outputformat".equals(fieldName))
	                {
	                	outputformat = item.getString();
	                }
	            }
	            else
	            {
	            	//We expect only one file
	                Path file = Paths.get(item.getName());
	                fileName = file.getFileName().toString();
	                fileContent = item.getInputStream();
	            }
	        }
	    } 
		catch (FileUploadException e)
		{
	        throw new ServletException("Cannot parse multipart request.", e);
	    }

		if (inputformat == null || outputformat == null || fileContent == null)
		{
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
		}
		else
		{
		    HttpURLConnection con = null;
			
			try
			{
				URL obj = new URL(CONVERT_SERVICE_URL);
				con = (HttpURLConnection) obj.openConnection();
				con.setUseCaches(false);
				con.setDoOutput(true);
				
				con.setRequestMethod("POST");
				con.setRequestProperty("Connection", "Keep-Alive");
				con.setRequestProperty("Cache-Control", "no-cache");
				con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
				
				DataOutputStream postRequest = new DataOutputStream(con.getOutputStream());

				byte[] data      = new byte[10240]; //10 KB buffer
				int    bytesRead = fileContent.read(data);
				int w = 0, h = 0, dpi = 96;
				
				if (inputformat.equals("emf") && bytesRead >= 40)
				{
					//Read Frame from EMF header (the rectangular inclusive-inclusive dimensions, in .01 millimeter units, 
					//		of a rectangle that surrounds the image stored in the metafile.)
					int x0 = fromByteArray(data, 24);
					int y0 = fromByteArray(data, 28);
					int x1 = fromByteArray(data, 32);
					int y1 = fromByteArray(data, 36);
					
					//Approximate dimensions of the image
					w = (int) ((x1 - x0) / EMF_10thMM2PXL);
					h = (int) ((y1 - y0) / EMF_10thMM2PXL);
				}
				
				if (w > MAX_DIM || h > MAX_DIM)
				{
					dpi = (int) (dpi * Math.min(MAX_DIM / (double) w, MAX_DIM / (double) h));
					
					if (dpi == 0)
					{
						dpi = 1;
					}
				}
				
				addParameter("apikey", API_KEY, postRequest);
				addParameter("inputformat", inputformat, postRequest);
				addParameter("outputformat", outputformat, postRequest);
				addParameter("input", "upload", postRequest);
				addParameter("wait", "true", postRequest);
				addParameter("download", "true", postRequest);
				
				if (dpi != 96)
				{
					addParameter("converteroptions[density]", Integer.toString(dpi), postRequest);
				}

				addParameterHeader("file", fileName, postRequest);
				int total = 0;

				while(bytesRead != -1) 
				{
					postRequest.write(data, 0, bytesRead);
					bytesRead = fileContent.read(data);
					total += bytesRead;

					if (total > Utils.MAX_SIZE)
					{
						postRequest.close();
						throw new Exception("File size exceeds the maximum allowed size of " + MAX_FILE_SIZE + " bytes.");
					}
				}
				
				postRequest.writeBytes(CRLF + TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + CRLF);
				
				postRequest.flush();
				postRequest.close();
	
				InputStream in = con.getInputStream();
				
				response.setStatus(con.getResponseCode());
				
				String contentType = "application/octet-stream";
				
				if ("png".equals(outputformat))
				{
					contentType = "image/png";
				}
				else if ("jpg".equals(outputformat))
				{
					contentType = "image/jpeg";
				}
				
				response.setHeader("Content-Type", contentType);
				
				OutputStream out = response.getOutputStream();

				bytesRead = in.read(data);
				
				try
				{
					URI uri = new URI(request.getHeader("referer"));
				    String domain = uri.getHost();
					log.log(Level.CONFIG, "EMF-CONVERT, domain: " + domain + " ,Filename: " + 
							fileName != null ? fileName : "" + ", size: " + bytesRead);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				
				while(bytesRead != -1) 
				{
					out.write(data, 0, bytesRead);
					bytesRead = in.read(data);
				}
				
				in.close();
				out.flush();
				out.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();

				if (con != null)
				{
					try 
					{
						BufferedReader in = new BufferedReader(
								new InputStreamReader(con.getErrorStream()));
						
						String inputLine;
	
						while ((inputLine = in.readLine()) != null)
						{
							System.err.println(inputLine);
						}
						in.close();
					}
					catch (Exception e2) 
					{
						// Ignore
					}
				}
				
				response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

				throw e;
			}
		}
	}

	private void addParameter(String name, String val, DataOutputStream postRequest) throws IOException {
		addParameterHeader(name, null, postRequest);
		postRequest.writeBytes(val);
		postRequest.writeBytes(CRLF);
	}
	
	private void addParameterHeader(String name, String fileName, DataOutputStream postRequest) throws IOException {
		postRequest.writeBytes(TWO_HYPHENS + BOUNDARY + CRLF);
		postRequest.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + 
					(fileName != null? "; filename=\"" + fileName + "\"" + CRLF + "Content-Type: application/octet-stream" : "") + CRLF);
		postRequest.writeBytes(CRLF);
	}
}