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, boolean, 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        boolean keepFileNames,
056        I_CmsUploadDialog dialog) {
057
058        JsArray<CmsFileInfo> filesToUploadArray = JavaScriptObject.createArray().cast();
059        for (CmsFileInfo fileInfo : filesToUpload) {
060            filesToUploadArray.push(fileInfo);
061        }
062
063        // create a array that contains the names of the files that should be unziped
064        JavaScriptObject filesToUnzipArray = JavaScriptObject.createArray();
065        for (String filename : filesToUnzip) {
066            CmsClientStringUtil.pushArray(filesToUnzipArray, filename);
067        }
068        upload(
069            uploadUri,
070            targetFolder,
071            isRootPath,
072            postCreateHandler,
073            filesToUploadArray,
074            filesToUnzipArray,
075            keepFileNames,
076            dialog);
077    }
078
079    /**
080     * Sends a post request to the upload JSP.<p>
081     *
082     * @param uploadUri the URI of the JSP that performs the upload
083     * @param targetFolder the target folder to upload
084     * @param isRootPath true if the target folder is given as a root path
085     * @param postCreateHandler the post-create handler
086     * @param filesToUpload the files to upload
087     * @param filesToUnzip the file names to unzip
088     * @param dialog this dialog
089     */
090    protected native void upload(
091        String uploadUri,
092        String targetFolder,
093        boolean isRootPath,
094        String postCreateHandler,
095        JsArray<CmsFileInfo> filesToUpload,
096        JavaScriptObject filesToUnzip,
097        boolean keepFileNames,
098        I_CmsUploadDialog dialog) /*-{
099
100        var data = new FormData();
101        if (keepFileNames) {
102            data.append(@org.opencms.gwt.shared.I_CmsUploadConstants::KEEP_FILE_NAMES, "true");
103        }
104        for (i = 0; i < filesToUpload.length; i++) {
105            var file = filesToUpload[i];
106            var fieldName = "file_" + i;
107            data.append(fieldName, file);
108            // get the current file name/override-name
109            var fileName = file.overrideFileName ? file.overrideFileName
110                    : file.name ? file.name : file.fileName;
111            data
112                    .append(
113                            fieldName
114                                    + @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_FILENAME_ENCODED_SUFFIX,
115                            encodeURI(fileName));
116
117
118           data.append(fieldName + @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_ORIGINAL_FILENAME_ENCODED_SUFFIX, file.name || file.fileName);
119
120        }
121        data
122                .append(
123                        @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_TARGET_FOLDER_FIELD_NAME,
124                        targetFolder);
125
126        if (postCreateHandler) {
127            data
128                    .append(
129                            @org.opencms.gwt.shared.I_CmsUploadConstants::POST_CREATE_HANDLER,
130                            postCreateHandler);
131        };
132        data
133                .append(
134                        @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_IS_ROOT_PATH_FIELD_NAME,
135                        "" + isRootPath);
136
137        for (var i = 0; i < filesToUnzip.length; ++i) {
138            data
139                    .append(
140                            @org.opencms.gwt.shared.I_CmsUploadConstants::UPLOAD_UNZIP_FILES_FIELD_NAME,
141                            encodeURI(filesToUnzip[i]));
142        }
143
144        var xhr = new XMLHttpRequest();
145        xhr.open("POST", uploadUri, true);
146        xhr.onreadystatechange = function() {
147            if (xhr.readyState == 4) {
148                if (xhr.status == 200) {
149                    dialog.@org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog::parseResponse(Ljava/lang/String;)(xhr.responseText);
150                } else {
151                    dialog.@org.opencms.gwt.client.ui.input.upload.I_CmsUploadDialog::showErrorReport(Ljava/lang/String;Ljava/lang/String;)(xhr.statusText, null);
152                }
153            }
154        }
155        xhr.send(data);
156
157    }-*/;
158}