Search in sources :

Example 1 with IMediaUploadCallback

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 }));
    }
}
Also used : IUploadData(com.servoy.j2db.plugins.IUploadData) DivDialogAction(com.servoy.j2db.server.headlessclient.PageJSActionBuffer.DivDialogAction) IMediaUploadCallback(com.servoy.j2db.plugins.IMediaUploadCallback)

Example 2 with IMediaUploadCallback

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());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) ServletException(javax.servlet.ServletException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IMediaUploadCallback(com.servoy.j2db.plugins.IMediaUploadCallback) Settings(com.servoy.j2db.util.Settings) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSMap(com.servoy.j2db.scripting.JSMap) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

IMediaUploadCallback (com.servoy.j2db.plugins.IMediaUploadCallback)2 IUploadData (com.servoy.j2db.plugins.IUploadData)1 JSMap (com.servoy.j2db.scripting.JSMap)1 DivDialogAction (com.servoy.j2db.server.headlessclient.PageJSActionBuffer.DivDialogAction)1 Settings (com.servoy.j2db.util.Settings)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1