001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input.upload.impl;
029
030import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
031import org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog;
032import org.opencms.gwt.client.ui.input.upload.I_CmsUploader;
033import org.opencms.gwt.client.util.CmsClientStringUtil;
034
035import java.util.List;
036
037import com.google.gwt.core.client.JavaScriptObject;
038import com.google.gwt.core.client.JsArray;
039
040/**
041 * Form data implementation of the file uploader.<p>
042 */
043public class CmsUploaderFormData implements I_CmsUploader {
044
045    /**
046     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploader#uploadFiles(java.lang.String, java.lang.String, boolean, java.lang.String, java.util.List, java.util.List, org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog)
047     */
048    public void uploadFiles(
049        String uploadUri,
050        String targetFolder,
051        boolean isRootPath,
052        String postCreateHandler,
053        List<CmsFileInfo> filesToUpload,
054        List<String> filesToUnzip,
055        I_CmsUploadDialog dialog) {
056
057        JsArray<CmsFileInfo> filesToUploadArray = JavaScriptObject.createArray().cast();
058        for (CmsFileInfo fileInfo : filesToUpload) {
059            filesToUploadArray.push(fileInfo);
060        }
061
062        // create a array that contains the names of the files that should be unziped
063        JavaScriptObject filesToUnzipArray = JavaScriptObject.createArray();
064        for (String filename : filesToUnzip) {
065            CmsClientStringUtil.pushArray(filesToUnzipArray, filename);
066        }
067        upload(uploadUri, targetFolder, isRootPath, postCreateHandler, filesToUploadArray, filesToUnzipArray, dialog);
068    }
069
070    /**
071     * Sends a post request to the upload JSP.<p>
072     *
073     * @param uploadUri the URI of the JSP that performs the upload
074     * @param targetFolder the target folder to upload
075     * @param isRootPath true if the target folder is given as a root path
076     * @param postCreateHandler the post-create handler
077     * @param filesToUpload the files to upload
078     * @param filesToUnzip the file names to unzip
079     * @param dialog this dialog
080     */
081    protected native void upload(
082        String uploadUri,
083        String targetFolder,
084        boolean isRootPath,
085        String postCreateHandler,
086        JsArray<CmsFileInfo> filesToUpload,
087        JavaScriptObject filesToUnzip,
088        I_CmsUploadDialog dialog) /*-{
089
090        var data = new FormData();
091
092        for (i = 0; i < filesToUpload.length; i++) {
093            var file = filesToUpload[i];
094            var fieldName = "file_" + i;
095            data.append(fieldName, file);
096            // get the current file name/override-name
097            var fileName = file.overrideFileName ? file.overrideFileName
098                    : file.name ? file.name : file.fileName;
099            data
100                    .append(
101                            fieldName
102                                    + @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_FILENAME_ENCODED_SUFFIX,
103                            encodeURI(fileName));
104        }
105        data
106                .append(
107                        @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_TARGET_FOLDER_FIELD_NAME,
108                        targetFolder);
109
110        if (postCreateHandler) {
111            data
112                    .append(
113                            @org.opencms.gwt.shared.I_CmsUploadConstants::POST_CREATE_HANDLER,
114                            postCreateHandler);
115        };
116        data
117                .append(
118                        @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_IS_ROOT_PATH_FIELD_NAME,
119                        "" + isRootPath);
120
121        for (var i = 0; i < filesToUnzip.length; ++i) {
122            data
123                    .append(
124                            @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_UNZIP_FILES_FIELD_NAME,
125                            encodeURI(filesToUnzip[i]));
126        }
127
128        var xhr = new XMLHttpRequest();
129        xhr.open("POST", uploadUri, true);
130        xhr.onreadystatechange = function() {
131            if (xhr.readyState == 4) {
132                if (xhr.status == 200) {
133                    dialog.@org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog::parseResponse(Ljava/lang/String;)(xhr.responseText);
134                } else {
135                    dialog.@org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog::showErrorReport(Ljava/lang/String;Ljava/lang/String;)(xhr.statusText, null);
136                }
137            }
138        }
139        xhr.send(data);
140
141    }-*/;
142}