<commit_before>/*
 * Copyright 1999-2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the License. You may obtain
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 * for the specific language governing permissions and limitations under the License.
 */

/* $Id: UploadHelper.java,v 1.3 2004/04/14 14:14:14 andreas Exp $ */

package org.apache.lenya.cms.authoring;

import org.apache.cocoon.servlet.multipart.Part;
import org.apache.log4j.Category;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;

/**
 *
 */
public class UploadHelper {
    Category log = Category.getInstance(UploadHelper.class);

    private String baseDir;

    /**
     *
     */
    public UploadHelper(String baseDir) {
        this.baseDir = baseDir;
    }

    /**
     * Save uploaded file
     * @param part
     */
    public boolean save(Part part) {

        File file = new File(baseDir + File.separator + part.getFileName());
        log.debug(file.getAbsolutePath());
        File parent = new File(file.getParent());
        if (!parent.isDirectory()) {
            parent.mkdirs();
            log.info("Directory has been created: " + parent);
        }

        try {
            FileOutputStream out = new FileOutputStream(file.getAbsolutePath());
            InputStream in = part.getInputStream();
            byte[] buf = new byte[4096];
            int read = in.read(buf);

            while (read > 0) {
                out.write(buf, 0, read);
                read = in.read(buf);
            }
        } catch (IOException e) {
            log.error(e);
            return false;
        } catch (Exception e) {
            log.error(e);
            return false;
        }
        return true;
    }
}<commit_msg>- code style fixes - added method<commit_after>/*
 * Copyright 1999-2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the License. You may obtain
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 * for the specific language governing permissions and limitations under the License.
 */

/* $Id: UploadHelper.java,v 1.4 2004/04/14 17:41:32 andreas Exp $ */

package org.apache.lenya.cms.authoring;

import org.apache.cocoon.environment.Request;
import org.apache.cocoon.servlet.multipart.Part;
import org.apache.log4j.Category;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;

/**
 * Helper class for uploading files.
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @author <a href="mailto:<EMAIL>"><NAME> </a>
 * @version $Id: UploadHelper.java,v 1.4 2004/04/14 17:41:32 andreas Exp $
 */
public class UploadHelper {

    private static final Category log = Category.getInstance(UploadHelper.class);

    private File directory;

    /**
     * Ctor.
     * @param directory The directory to save the files to.
     */
    public UploadHelper(File directory) {
        this.directory = directory;
    }

    /**
     * Ctor.
     * @param directoryPath The path of the directory to save the files to.
     */
    public UploadHelper(String directoryPath) {
        this.directory = new File(directoryPath);
    }

    /**
     * Save uploaded file
     * @param part The part of the multipart request.
     * @return <code>true</code> if the upload succeeded, <code>false</code> otherwise.
     */
    public boolean save(Part part) {

        File file = new File(directory, part.getFileName());
        if (log.isDebugEnabled()) {
            log.debug("Uploading file: [" + file.getAbsolutePath() + "]");
        }

        if (!directory.isDirectory()) {
            directory.mkdirs();
            if (log.isInfoEnabled()) {
                log.info("Directory has been created: [" + directory + "]");
            }
        }

        try {
            FileOutputStream out = new FileOutputStream(file.getAbsolutePath());
            InputStream in = part.getInputStream();
            byte[] buf = new byte[4096];
            int read = in.read(buf);

            while (read > 0) {
                out.write(buf, 0, read);
                read = in.read(buf);
            }
        } catch (IOException e) {
            log.error(e);
            return false;
        } catch (Exception e) {
            log.error(e);
            return false;
        }
        return true;
    }

    /**
     * Saves the a file the request for a certain request parameter name.
     * @param requestParameter The name of the &lt;input type="file"/&gt; request parameter value.
     * @return The saved file or <code>null</code> if the upload was not successful.
     * @throws Exception when something went wrong.
     */
    public File save(Request request, String requestParameter)
            throws Exception {

        Part part = (Part) request.get(requestParameter);

        File file = null;
        
        boolean success = save(part);
        if (success) {
            file = new File(directory, part.getFileName());
        }

        return file;
    }

}
{'fraction_non_alphanumeric': 0.10728036401820092, 'fraction_numerical': 0.015050752537626881, 'mean_word_length': 2.3499413833528724, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4150892', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1531, 'n_words': 512}