Search in sources :

Example 1 with Attachment

use of net.jforum.entities.Attachment in project jforum2 by rafaelsteil.

the class PostAction method downloadAttach.

public void downloadAttach() {
    int id = this.request.getIntParameter("attach_id");
    if (!SessionFacade.isLogged() && !SystemGlobals.getBoolValue(ConfigKeys.ATTACHMENTS_ANONYMOUS)) {
        String referer = this.request.getHeader("Referer");
        if (referer != null) {
            this.setTemplateName(ViewCommon.contextToLogin(referer));
        } else {
            this.setTemplateName(ViewCommon.contextToLogin());
        }
        return;
    }
    AttachmentDAO am = DataAccessDriver.getInstance().newAttachmentDAO();
    Attachment a = am.selectAttachmentById(id);
    PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
    Post post = postDao.selectById(a.getPostId());
    String forumId = Integer.toString(post.getForumId());
    boolean attachmentsEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, forumId);
    boolean attachmentsDownload = SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD, forumId);
    if (!attachmentsEnabled && !attachmentsDownload) {
        this.setTemplateName(TemplateKeys.POSTS_CANNOT_DOWNLOAD);
        this.context.put("message", I18n.getMessage("Attachments.featureDisabled"));
        return;
    }
    String filename = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename();
    if (!new File(filename).exists()) {
        this.setTemplateName(TemplateKeys.POSTS_ATTACH_NOTFOUND);
        this.context.put("message", I18n.getMessage("Attachments.notFound"));
        return;
    }
    FileInputStream fis = null;
    OutputStream os = null;
    try {
        a.getInfo().setDownloadCount(a.getInfo().getDownloadCount() + 1);
        am.updateAttachment(a);
        fis = new FileInputStream(filename);
        os = response.getOutputStream();
        if (am.isPhysicalDownloadMode(a.getInfo().getExtension().getExtensionGroupId())) {
            this.response.setContentType("application/octet-stream");
        } else {
            this.response.setContentType(a.getInfo().getMimetype());
        }
        if (this.request.getHeader("User-Agent").indexOf("Firefox") != -1) {
            this.response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(a.getInfo().getRealFilename().getBytes(SystemGlobals.getValue(ConfigKeys.ENCODING)), SystemGlobals.getValue(ConfigKeys.DEFAULT_CONTAINER_ENCODING)) + "\";");
        } else {
            this.response.setHeader("Content-Disposition", "attachment; filename=\"" + ViewCommon.toUtf8String(a.getInfo().getRealFilename()) + "\";");
        }
        this.response.setContentLength((int) a.getInfo().getFilesize());
        int c;
        byte[] b = new byte[4096];
        while ((c = fis.read(b)) != -1) {
            os.write(b, 0, c);
        }
        JForumExecutionContext.enableCustomContent(true);
    } catch (IOException e) {
        throw new ForumException(e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : AttachmentDAO(net.jforum.dao.AttachmentDAO) Post(net.jforum.entities.Post) OutputStream(java.io.OutputStream) Attachment(net.jforum.entities.Attachment) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AttachmentException(net.jforum.exceptions.AttachmentException) ForumException(net.jforum.exceptions.ForumException) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException) PostDAO(net.jforum.dao.PostDAO) File(java.io.File)

Example 2 with Attachment

use of net.jforum.entities.Attachment in project jforum2 by rafaelsteil.

the class AttachmentCommon method insertAttachments.

public void insertAttachments(Post post) {
    if (!this.canProceed) {
        return;
    }
    post.hasAttachments(this.filesToSave.size() > 0);
    for (Iterator iter = this.filesToSave.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        Attachment a = (Attachment) entry.getValue();
        a.setPostId(post.getId());
        String path = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename();
        this.am.addAttachment(a);
        ((UploadUtils) entry.getKey()).saveUploadedFile(path);
        if (this.shouldCreateThumb(a)) {
            this.createSaveThumb(path);
        }
    }
}
Also used : Iterator(java.util.Iterator) Attachment(net.jforum.entities.Attachment) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Attachment

use of net.jforum.entities.Attachment in project jforum2 by rafaelsteil.

the class GenericAttachmentDAO method selectAttachmentById.

/**
	 * @see net.jforum.dao.AttachmentDAO#selectAttachmentById(int)
	 */
public Attachment selectAttachmentById(int attachId) {
    ResultSet rs = null;
    PreparedStatement p = null;
    try {
        Attachment e = null;
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("AttachmentModel.selectAttachmentById"));
        p.setInt(1, attachId);
        rs = p.executeQuery();
        if (rs.next()) {
            e = this.getAttachment(rs);
        }
        return e;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Attachment(net.jforum.entities.Attachment) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 4 with Attachment

use of net.jforum.entities.Attachment in project jforum2 by rafaelsteil.

the class GenericAttachmentDAO method getAttachment.

protected Attachment getAttachment(ResultSet rs) throws SQLException {
    Attachment a = new Attachment();
    a.setId(rs.getInt("attach_id"));
    a.setPostId(rs.getInt("post_id"));
    a.setPrivmsgsId(rs.getInt("privmsgs_id"));
    AttachmentInfo ai = new AttachmentInfo();
    ai.setComment(rs.getString("description"));
    ai.setDownloadCount(rs.getInt("download_count"));
    ai.setFilesize(rs.getLong("filesize"));
    ai.setMimetype(rs.getString("mimetype"));
    ai.setPhysicalFilename(rs.getString("physical_filename"));
    ai.setRealFilename(rs.getString("real_filename"));
    ai.setUploadTime(new Date(rs.getTimestamp("upload_time").getTime()));
    ai.setExtension(this.selectExtension(rs.getInt("extension_id")));
    a.setInfo(ai);
    return a;
}
Also used : AttachmentInfo(net.jforum.entities.AttachmentInfo) Attachment(net.jforum.entities.Attachment) Date(java.util.Date)

Example 5 with Attachment

use of net.jforum.entities.Attachment in project jforum2 by rafaelsteil.

the class AttachmentCommon method editAttachments.

public void editAttachments(int postId, int forumId) {
    // Allow removing the attachments at least
    AttachmentDAO am = DataAccessDriver.getInstance().newAttachmentDAO();
    // Check for attachments to remove
    List deleteList = new ArrayList();
    String[] delete = null;
    String s = this.request.getParameter("delete_attach");
    if (s != null) {
        delete = s.split(",");
    }
    if (delete != null) {
        for (int i = 0; i < delete.length; i++) {
            if (delete[i] != null && !delete[i].equals("")) {
                int id = Integer.parseInt(delete[i]);
                Attachment a = am.selectAttachmentById(id);
                am.removeAttachment(id, postId);
                String filename = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + a.getInfo().getPhysicalFilename();
                File f = new File(filename);
                if (f.exists()) {
                    f.delete();
                }
                // Check if we have a thumb to delete
                f = new File(filename + "_thumb");
                if (f.exists()) {
                    f.delete();
                }
            }
        }
        deleteList = Arrays.asList(delete);
    }
    if (!SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(forumId)) && !SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD)) {
        return;
    }
    // Update
    String[] attachIds = null;
    s = this.request.getParameter("edit_attach_ids");
    if (s != null) {
        attachIds = s.split(",");
    }
    if (attachIds != null) {
        for (int i = 0; i < attachIds.length; i++) {
            if (deleteList.contains(attachIds[i]) || attachIds[i] == null || attachIds[i].equals("")) {
                continue;
            }
            int id = Integer.parseInt(attachIds[i]);
            Attachment a = am.selectAttachmentById(id);
            a.getInfo().setComment(this.request.getParameter("edit_comment_" + id));
            am.updateAttachment(a);
        }
    }
}
Also used : AttachmentDAO(net.jforum.dao.AttachmentDAO) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Attachment(net.jforum.entities.Attachment) File(java.io.File)

Aggregations

Attachment (net.jforum.entities.Attachment)7 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 AttachmentDAO (net.jforum.dao.AttachmentDAO)2 AttachmentInfo (net.jforum.entities.AttachmentInfo)2 AttachmentException (net.jforum.exceptions.AttachmentException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 PostDAO (net.jforum.dao.PostDAO)1 AttachmentExtension (net.jforum.entities.AttachmentExtension)1 Post (net.jforum.entities.Post)1