use of com.servoy.j2db.plugins.IMediaUploadCallback in project servoy-client by Servoy.
the class MainPage method showOpenFileDialog.
@SuppressWarnings("nls")
public void showOpenFileDialog(final IMediaUploadCallback callback, boolean multiSelect, String acceptFilter, String title) {
if ((isShowingInDialog() || isClosingAsDivPopup()) && callingContainer != null) {
callingContainer.showOpenFileDialog(callback, multiSelect, acceptFilter, title);
} else {
touch();
this.mediaUploadMultiSelect = multiSelect;
this.mediaUploadFilter = acceptFilter;
this.mediaUploadCallback = new IMediaUploadCallback() {
boolean uploaded = false;
public void uploadComplete(IUploadData[] fu) {
touch();
uploaded = true;
mediaUploadCallback = null;
addJSAction(new DivDialogAction(fileUploadWindow, DivDialogAction.OP_CLOSE));
callback.uploadComplete(fu);
}
public void onSubmit() {
if (!uploaded) {
mediaUploadCallback = null;
divDialogs.remove(FILE_UPLOAD_PAGEMAP);
fileUploadWindow.setPageMapName(null);
fileUploadWindow.remove(fileUploadWindow.getContentId());
addJSAction(new DivDialogAction(fileUploadWindow, DivDialogAction.OP_CLOSE));
}
}
};
fileUploadWindow.setPageMapName(FILE_UPLOAD_PAGEMAP);
if (title == null) {
fileUploadWindow.setTitle(client.getI18NMessage("servoy.filechooser.title"));
} else if (!"".equals(title)) {
fileUploadWindow.setTitle(title);
}
divDialogs.put(FILE_UPLOAD_PAGEMAP, fileUploadWindow);
addJSAction(new DivDialogAction(fileUploadWindow, DivDialogAction.OP_SHOW, new Object[] { FILE_UPLOAD_PAGEMAP }));
}
}
use of com.servoy.j2db.plugins.IMediaUploadCallback in project servoy-client by Servoy.
the class MediaResourcesServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String path = req.getPathInfo();
if (path.startsWith("/"))
path = path.substring(1);
String[] paths = path.split("/");
String reqEncoding = req.getCharacterEncoding() == null ? "UTF-8" : req.getCharacterEncoding();
if ((paths.length == 2 || paths.length >= 5) && paths[0].equals("upload")) {
if (req.getHeader("Content-Type") != null && req.getHeader("Content-Type").startsWith("multipart/form-data")) {
int clientnr = paths[1].length() == 0 ? -1 : Integer.parseInt(paths[1]);
final INGClientWebsocketSession wsSession = getSession(req, clientnr);
try {
if (wsSession != null) {
Settings settings = Settings.getInstance();
File fileUploadDir = null;
String uploadDir = settings.getProperty("servoy.ng_web_client.temp.uploadir");
if (uploadDir != null) {
fileUploadDir = new File(uploadDir);
if (!fileUploadDir.exists() && !fileUploadDir.mkdirs()) {
fileUploadDir = null;
Debug.error("Couldn't use the property 'servoy.ng_web_client.temp.uploadir' value: '" + uploadDir + "', directory could not be created or doesn't exists");
}
}
int tempFileThreshold = Utils.getAsInteger(settings.getProperty("servoy.ng_web_client.tempfile.threshold", "50"), false) * 1000;
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(tempFileThreshold, fileUploadDir);
diskFileItemFactory.setFileCleaningTracker(FILE_CLEANING_TRACKER);
ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
upload.setHeaderEncoding(reqEncoding);
long maxUpload = Utils.getAsLong(settings.getProperty("servoy.webclient.maxuploadsize", "0"), false);
if (maxUpload > 0)
upload.setFileSizeMax(maxUpload * 1000);
Iterator<FileItem> iterator = upload.parseRequest(req).iterator();
final List<FileUploadData> aFileUploadData = new ArrayList<FileUploadData>();
List<FileItem> formFields = new ArrayList<>();
while (iterator.hasNext()) {
FileItem item = iterator.next();
if (item.isFormField()) {
formFields.add(item);
} else {
String encoding = StringUtils.defaultString(req.getCharacterEncoding(), "UTF-8");
JSMap<String, String> fieldsMap = new JSMap<>();
for (FileItem fileItem : formFields) {
try {
fieldsMap.put(fileItem.getFieldName(), fileItem.getString(encoding));
} catch (UnsupportedEncodingException e) {
Debug.error(e);
}
}
if (callClient(req, paths, wsSession, fieldsMap, item)) {
formFields = new ArrayList<>();
} else {
// it is a file from the built-in file selector
aFileUploadData.add(new FileUploadData(item));
}
}
}
if (aFileUploadData.size() > 0) {
final IMediaUploadCallback mediaUploadCallback = ((NGClient) wsSession.getClient()).getMediaUploadCallback();
if (mediaUploadCallback != null) {
// leave time for this request to finish, before executing the callback, so the file
// dialog can do its close
((NGClient) wsSession.getClient()).invokeLater(new Runnable() {
@Override
public void run() {
mediaUploadCallback.uploadComplete(aFileUploadData.toArray(new FileUploadData[aFileUploadData.size()]));
mediaUploadCallback.onSubmit();
}
});
}
}
}
} catch (FileSizeLimitExceededException ex) {
res.setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
if (wsSession != null)
res.getWriter().print(wsSession.getClient().getI18NMessage("servoy.filechooser.sizeExceeded", new Object[] { ex.getPermittedSize() / 1000 + "KB" }));
} catch (FileUploadException ex) {
ex.printStackTrace();
throw new ServletException(ex.toString());
}
}
}
}
Aggregations