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) {
}
}
}
}
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);
}
}
}
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);
}
}
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;
}
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);
}
}
}
Aggregations