Example usage for org.apache.commons.transaction.util FileHelper copy

List of usage examples for org.apache.commons.transaction.util FileHelper copy

Introduction

In this page you can find the example usage for org.apache.commons.transaction.util FileHelper copy.

Prototype

public static long copy(InputStream in, OutputStream out) throws IOException 

Source Link

Document

Copies an InputStream to an OutputStream using a local internal buffer for performance.

Usage

From source file:org.apache.openmeetings.servlet.outputhandler.UploadController.java

private void uploadFile(HttpServletRequest request, boolean userProfile, Long userId, String roomName,
        InputStream is, String fileSystemName, UploadCompleteMessage uploadCompleteMessage) throws Exception {
    ConverterProcessResultList returnError = new ConverterProcessResultList();

    // Check variable to see if this file is a presentation
    int dotidx = fileSystemName.lastIndexOf('.');
    String newFileName = StringComparer.getInstance().compareForRealPaths(fileSystemName.substring(0, dotidx));
    String newFileExtDot = fileSystemName.substring(dotidx, fileSystemName.length()).toLowerCase();
    String newFileExt = newFileExtDot.substring(1);

    // trim long names cause cannot output that
    final int MAX_FILE_NAME_LENGTH = 30;
    if (newFileName.length() >= MAX_FILE_NAME_LENGTH) {
        newFileName = newFileName.substring(0, MAX_FILE_NAME_LENGTH);
    }/*from   ww w.  j a  v  a 2  s.c o  m*/
    StoredFile storedFile = new StoredFile(newFileName, newFileExt);

    // check if this is a a file that can be converted by
    // openoffice-service
    boolean canBeConverted = storedFile.isConvertable();
    boolean isPdf = storedFile.isPdf();
    boolean isImage = storedFile.isImage();
    boolean isAsIs = storedFile.isAsIs();

    File workingDir = null;
    // add outputfolders for profiles
    if (userProfile) {
        // User Profile Update
        this.deleteUserProfileFilesStoreTemp(userId);
        newFileName = "profile"; //set unified file name to avoid any problems with national characters
        workingDir = OmFileHelper.getUploadProfilesUserDir(userId);
    }
    // if it is a presenation it will be copied to another
    // place
    if (isAsIs) {
        // check if this is a room file or UserProfile
        if (!userProfile) {
            workingDir = OmFileHelper.getUploadRoomDir(roomName);
        }
    } else if (canBeConverted || isPdf || isImage) {
        workingDir = OmFileHelper.getUploadTempProfilesUserDir(userId);
    } else {
        return;
    }

    File completeName = OmFileHelper.getNewFile(workingDir, newFileName, newFileExtDot);

    log.debug("write file to : " + completeName);

    FileHelper.copy(is, completeName);
    is.close();

    log.debug("canBeConverted: " + canBeConverted);
    if (canBeConverted) {
        // convert to pdf, thumbs, swf and xml-description
        returnError = generatePDF.convertPDF(newFileName, roomName, true, completeName);
    } else if (isPdf) {

        boolean isEncrypted = true;

        log.debug("isEncrypted :: " + isEncrypted);

        if (isEncrypted) {
            // Do convert pdf to other pdf first
            File f_old = completeName;

            completeName = OmFileHelper.appendSuffix(completeName, "_N_E");
            newFileName += "_N_E";

            generateThumbs.decodePDF(f_old.getCanonicalPath(), completeName.getCanonicalPath());

            if (f_old.exists()) {
                f_old.delete();
            }

        }

        // convert to thumbs, swf and xml-description
        returnError = generatePDF.convertPDF(newFileName, roomName, false, completeName);

        // returnError.put("decodePDF", returnError2);

    } else if (isImage && !isAsIs) {

        log.debug("##### isImage! userProfilePic: " + userProfile);

        if (userProfile) {
            // User Profile Update
            this.deleteUserProfileFiles(userId);
            // convert it to JPG
            returnError = generateImage.convertImageUserProfile(newFileName, newFileExtDot, userId, newFileName,
                    false);
        } else {
            // convert it to JPG
            log.debug("##### convert it to JPG: " + userProfile);
            returnError = generateImage.convertImage(newFileName, newFileExtDot, roomName, newFileName, false);
        }
    } else if (isAsIs) {
        if (userProfile) {
            // User Profile Update
            this.deleteUserProfileFiles(userId);
            // is UserProfile Picture
            returnError.addItem("processThumb1", generateThumbs.generateThumb("_chat_", completeName, 40));
            returnError.addItem("processThumb2", generateThumbs.generateThumb("_profile_", completeName, 126));
            returnError.addItem("processThumb3", generateThumbs.generateThumb("_big_", completeName, 240));

            String pictureuri = completeName.getName();
            User us = usersDao.get(userId);
            us.setUpdatetime(new java.util.Date());
            us.setPictureuri(pictureuri);
            usersDao.update(us, userId);

            //FIXME: After updating the picture url all other users should refresh
        } else {
            returnError.addItem("processThumb", generateThumbs.generateThumb("_thumb_", completeName, 50));
        }
    }

    uploadCompleteMessage.setMessage("library");
    uploadCompleteMessage.setAction("newFile");

    uploadCompleteMessage.setHasError(returnError.hasError());

    //we only send the complete log to the client if there is really something 
    //to show because of an error
    if (returnError.hasError()) {
        uploadCompleteMessage.setError(returnError.getLogMessage());
    }
    uploadCompleteMessage.setFileName(completeName.getName());

}

From source file:org.apache.openmeetings.util.OmFileHelper.java

public static void copyFile(File f1, OutputStream out) throws IOException {
    try (InputStream in = new FileInputStream(f1)) {
        FileHelper.copy(in, out);
        log.debug("File copied.");
    } catch (Exception e) {
        log.error("[copyfile(File, File)]", e);
    }//from  ww w  .j  av  a2  s.  com
}