/** * Stores uploaded file "as it is" and adds database entry. * * @return ID of attachment in the database. */ @Override public Attachment uploadFile( String fileName, String contentType, User user, byte[] contents, String tags) { if (log.isTraceEnabled()) { log.trace(">> uploadFile()"); } try { if (contents.length > MAX_ZIP_SIZE) { log.trace("File too large!"); throw new IOException("File too large."); } if (!checkUploadRights(user)) { return null; } Attachment a = prepareAttachment(fileName, contentType, user, contents, tags); em.persist(a); Set<User> uset = new HashSet(); uset.add(user); a.setUser(uset); em.merge(a); if (log.isTraceEnabled()) { log.trace("<< uploadFile(): " + a); } return a; } catch (Exception ex) { log.error("uploadFile(): Failed to upload file.", ex); return null; } }
/** * Stores uploaded file in database. If there are several files, they are compressed into zip * archive (with filename = user_login.zip). * * @param user Owner of the attachment * @param files List of uploaded files to be saved into database * @return ID of attachment in the database or null if operation failed. */ @Override public Attachment uploadFiles(User user, List<ReshakaUploadedFile> files, String tags) { if (log.isTraceEnabled()) { log.trace(">> uploadFiles(): " + files); } if (!checkUploadRights(user)) { return null; } if (files.isEmpty()) { if (log.isDebugEnabled()) { log.debug("List of files is empty! Nothing to compress."); } return null; } try { Attachment att = prepareAttachment(user, files, tags); if (att.getSize() > MAX_ZIP_SIZE) { if (log.isTraceEnabled()) { log.trace("File too large!"); } throw new IOException("File too large."); } em.persist(att); Set<User> uset = new HashSet(); uset.add(user); att.setUser(uset); em.merge(att); if (log.isTraceEnabled()) { log.trace("<< uploadFiles(): " + att); } return att; } catch (Exception ex) { log.error("uploadFiles(): Failed to upload files. ", ex); return null; } }
@Override public Attachment reuploadFiles( User user, Long attachmentId, List<ReshakaUploadedFile> files, String tags) { if (attachmentId == null) { return uploadFiles(user, files, tags); } if (!checkUploadRights(user)) { return null; } Attachment original = em.find(Attachment.class, attachmentId); if (original == null) { return null; } Set<User> u = original.getUser(); removeAttachmentFromDisk(original); original = prepareAttachment(user, files, tags); original.setId(attachmentId); original.setUser(u); return em.merge(original); }