try {
iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
final FileItemStream item = iterator.next();
final InputStream stream = item.openStream();
final JSONObject preference = preferenceUtils.getPreference();
if (null == preference) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String localeString = preference.getString(
Preference.LOCALE_STRING);
final Locale locale = new Locale(
Locales.getLanguage(localeString),
Locales.getCountry(localeString));
if (!item.isFormField()) {
final ResourceBundle lang =
ResourceBundle.getBundle(Keys.LANGUAGE, locale);
// XXX: check size before streaming
final byte[] contentBytes = IOUtils.toByteArray(stream);
if (contentBytes.length > MAX_SIZE) {
final String fail = lang.getString("uploadFailLabel");
final String cause =
lang.getString("exceedMaxUploadSizeLabel");
sendError(request, response,
HttpServletResponse.SC_BAD_REQUEST,
fail, cause);
return;
}
if (0 == contentBytes.length) {
final String fail = lang.getString("uploadFailLabel");
final String cause = lang.getString("fileEmptyLabel");
sendError(request, response,
HttpServletResponse.SC_BAD_REQUEST,
fail, cause);
return;
}
final Blob blob = new Blob(contentBytes);
final JSONObject file = new JSONObject();
final String id = Ids.genTimeMillisId();
file.put(Keys.OBJECT_ID, id);
file.put(File.FILE_CONTENT_TYPE, item.getContentType());
file.put(File.FILE_CONTENT, blob);
file.put(File.FILE_DOWNLOAD_COUNT, 0);
final String timeZoneId =
preference.getString(Preference.TIME_ZONE_ID);
final Date createDate = timeZoneUtils.getTime(timeZoneId);
file.put(File.FILE_UPLOAD_DATE, createDate);
final String fileName = item.getName();
file.put(File.FILE_NAME, fileName);
final long fileSize = contentBytes.length;
file.put(File.FILE_SIZE, fileSize);
final String downloadURL = "/datastore-file-access.do?oId="
+ id;