Search in sources :

Example 1 with FileItem

use of net.jforum.util.legacy.commons.fileupload.FileItem in project jforum2 by rafaelsteil.

the class SmiliesAction method processUpload.

private String processUpload() {
    String imgName = "";
    if (this.request.getObjectParameter("smilie_img") != null) {
        FileItem item = (FileItem) this.request.getObjectParameter("smilie_img");
        UploadUtils uploadUtils = new UploadUtils(item);
        imgName = MD5.crypt(item.getName());
        uploadUtils.saveUploadedFile(SystemGlobals.getApplicationPath() + "/" + SystemGlobals.getValue(ConfigKeys.SMILIE_IMAGE_DIR) + "/" + imgName + "." + uploadUtils.getExtension());
        imgName += "." + uploadUtils.getExtension();
    }
    return imgName;
}
Also used : FileItem(net.jforum.util.legacy.commons.fileupload.FileItem) UploadUtils(net.jforum.view.forum.common.UploadUtils)

Example 2 with FileItem

use of net.jforum.util.legacy.commons.fileupload.FileItem in project jforum2 by rafaelsteil.

the class WebRequestContext method handleMultipart.

/**
	 * @param superRequest HttpServletRequest
	 * @param encoding String
	 * @throws UnsupportedEncodingException
	 */
private void handleMultipart(HttpServletRequest superRequest, String encoding) throws UnsupportedEncodingException {
    String tmpPath = new StringBuffer(256).append(SystemGlobals.getApplicationPath()).append('/').append(SystemGlobals.getValue(ConfigKeys.TMP_DIR)).toString();
    File tmpDir = new File(tmpPath);
    boolean success = false;
    try {
        if (!tmpDir.exists()) {
            tmpDir.mkdirs();
            success = true;
        }
    } catch (Exception e) {
    // We won't log it because the directory
    // creation failed for some reason - a SecurityException
    // or something else. We don't care about it, as the
    // code below tries to use java.io.tmpdir
    }
    if (!success) {
        tmpPath = System.getProperty("java.io.tmpdir");
        tmpDir = new File(tmpPath);
    }
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(100 * 1024, tmpDir));
    upload.setHeaderEncoding(encoding);
    try {
        List items = upload.parseRequest(superRequest);
        for (Iterator iter = items.iterator(); iter.hasNext(); ) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                this.addParameter(item.getFieldName(), item.getString(encoding));
            } else {
                if (item.getSize() > 0) {
                    // We really don't want to call addParameter(), as 
                    // there should not be possible to have multiple
                    // values for a InputStream data
                    this.query.put(item.getFieldName(), item);
                }
            }
        }
    } catch (FileUploadException e) {
        throw new MultipartHandlingException("Error while processing multipart content: " + e);
    }
}
Also used : FileItem(net.jforum.util.legacy.commons.fileupload.FileItem) ServletFileUpload(net.jforum.util.legacy.commons.fileupload.servlet.ServletFileUpload) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DiskFileItemFactory(net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(net.jforum.util.legacy.commons.fileupload.FileUploadException) IOException(java.io.IOException) MultipartHandlingException(net.jforum.exceptions.MultipartHandlingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileUploadException(net.jforum.util.legacy.commons.fileupload.FileUploadException) MultipartHandlingException(net.jforum.exceptions.MultipartHandlingException)

Example 3 with FileItem

use of net.jforum.util.legacy.commons.fileupload.FileItem in project jforum2 by rafaelsteil.

the class UserCommon method handleAvatar.

/**
	 * @param u User
	 */
private static void handleAvatar(User u) {
    String fileName = MD5.crypt(Integer.toString(u.getId()));
    FileItem item = (FileItem) JForumExecutionContext.getRequest().getObjectParameter("avatar");
    UploadUtils uploadUtils = new UploadUtils(item);
    // Gets file extension
    String extension = uploadUtils.getExtension().toLowerCase();
    int type = ImageUtils.IMAGE_UNKNOWN;
    if (extension.equals("jpg") || extension.equals("jpeg")) {
        type = ImageUtils.IMAGE_JPEG;
    } else if (extension.equals("gif") || extension.equals("png")) {
        type = ImageUtils.IMAGE_PNG;
    }
    if (type != ImageUtils.IMAGE_UNKNOWN) {
        String avatarTmpFileName = SystemGlobals.getApplicationPath() + "/images/avatar/" + fileName + "_tmp." + extension;
        // We cannot handle gifs
        if (extension.toLowerCase().equals("gif")) {
            extension = "png";
        }
        String avatarFinalFileName = SystemGlobals.getApplicationPath() + "/images/avatar/" + fileName + "." + extension;
        uploadUtils.saveUploadedFile(avatarTmpFileName);
        // OK, time to check and process the avatar size
        int maxWidth = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_WIDTH);
        int maxHeight = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT);
        BufferedImage image = ImageUtils.resizeImage(avatarTmpFileName, type, maxWidth, maxHeight);
        ImageUtils.saveImage(image, avatarFinalFileName, type);
        u.setAvatar(fileName + "." + extension);
        // Delete the temporary file
        new File(avatarTmpFileName).delete();
    }
}
Also used : FileItem(net.jforum.util.legacy.commons.fileupload.FileItem) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 4 with FileItem

use of net.jforum.util.legacy.commons.fileupload.FileItem in project jforum2 by rafaelsteil.

the class AttachmentCommon method preProcess.

public void preProcess() {
    if (!this.canProceed) {
        return;
    }
    String t = this.request.getParameter("total_files");
    if (t == null || "".equals(t)) {
        return;
    }
    int total = Integer.parseInt(t);
    if (total < 1) {
        return;
    }
    if (total > SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST)) {
        total = SystemGlobals.getIntValue(ConfigKeys.ATTACHMENTS_MAX_POST);
    }
    long totalSize = 0;
    int userId = SessionFacade.getUserSession().getUserId();
    Map extensions = this.am.extensionsForSecurity();
    for (int i = 0; i < total; i++) {
        FileItem item = (FileItem) this.request.getObjectParameter("file_" + i);
        if (item == null) {
            continue;
        }
        if (item.getName().indexOf('\000') > -1) {
            logger.warn("Possible bad attachment (null char): " + item.getName() + " - user_id: " + SessionFacade.getUserSession().getUserId());
            continue;
        }
        UploadUtils uploadUtils = new UploadUtils(item);
        // Check if the extension is allowed
        boolean containsExtension = extensions.containsKey(uploadUtils.getExtension());
        boolean denyAll = extensions.containsKey(DENY_ALL);
        boolean isAllowed = (!denyAll && !containsExtension) || (containsExtension && extensions.get(uploadUtils.getExtension()).equals(Boolean.TRUE));
        if (!isAllowed) {
            throw new BadExtensionException(I18n.getMessage("Attachments.badExtension", new String[] { uploadUtils.getExtension() }));
        }
        // Check comment length:
        String comment = this.request.getParameter("comment_" + i);
        if (comment.length() > 254) {
            throw new AttachmentException("Comment too long.");
        }
        Attachment a = new Attachment();
        a.setUserId(userId);
        AttachmentInfo info = new AttachmentInfo();
        info.setFilesize(item.getSize());
        info.setComment(comment);
        info.setMimetype(item.getContentType());
        // Get only the filename, without the path (IE does that)
        String realName = this.stripPath(item.getName());
        info.setRealFilename(realName);
        info.setUploadTimeInMillis(System.currentTimeMillis());
        AttachmentExtension ext = this.am.selectExtension(uploadUtils.getExtension().toLowerCase());
        if (ext.isUnknown()) {
            ext.setExtension(uploadUtils.getExtension());
        }
        info.setExtension(ext);
        String savePath = this.makeStoreFilename(info);
        info.setPhysicalFilename(savePath);
        a.setInfo(info);
        filesToSave.put(uploadUtils, a);
        totalSize += item.getSize();
    }
    // Check upload limits
    QuotaLimit ql = this.getQuotaLimit(userId);
    if (ql != null) {
        if (ql.exceedsQuota(totalSize)) {
            throw new AttachmentSizeTooBigException(I18n.getMessage("Attachments.tooBig", new Integer[] { new Integer(ql.getSizeInBytes() / 1024), new Integer((int) totalSize / 1024) }));
        }
    }
}
Also used : AttachmentInfo(net.jforum.entities.AttachmentInfo) Attachment(net.jforum.entities.Attachment) AttachmentException(net.jforum.exceptions.AttachmentException) FileItem(net.jforum.util.legacy.commons.fileupload.FileItem) AttachmentExtension(net.jforum.entities.AttachmentExtension) AttachmentSizeTooBigException(net.jforum.exceptions.AttachmentSizeTooBigException) QuotaLimit(net.jforum.entities.QuotaLimit) HashMap(java.util.HashMap) Map(java.util.Map) BadExtensionException(net.jforum.exceptions.BadExtensionException)

Aggregations

FileItem (net.jforum.util.legacy.commons.fileupload.FileItem)4 File (java.io.File)2 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Attachment (net.jforum.entities.Attachment)1 AttachmentExtension (net.jforum.entities.AttachmentExtension)1 AttachmentInfo (net.jforum.entities.AttachmentInfo)1 QuotaLimit (net.jforum.entities.QuotaLimit)1 AttachmentException (net.jforum.exceptions.AttachmentException)1 AttachmentSizeTooBigException (net.jforum.exceptions.AttachmentSizeTooBigException)1 BadExtensionException (net.jforum.exceptions.BadExtensionException)1 MultipartHandlingException (net.jforum.exceptions.MultipartHandlingException)1 FileUploadException (net.jforum.util.legacy.commons.fileupload.FileUploadException)1 DiskFileItemFactory (net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory)1