@Transactional public void createDocument(Document info, String folder, InputStream docData, User user) throws IOException { boolean created = false; File file = null; try { info.setId(null); documentDao.makePersistent(info); String uuid = UUID.randomUUID().toString(); file = new File(docsFolderPath + uuid); while (file.exists()) { uuid = UUID.randomUUID().toString(); file = new File(docsFolderPath + uuid); } created = file.createNewFile(); info.setUuid(uuid); Folder f = folderDao.getFolderByPath(folder, user); if (f == null) { if (folder != null && !folder.equals("")) { f = new Folder(); f.setPath(folder); folderDao.makePersistent(f); } } if (f == null) { throw new IOException("Internal Error"); } if (!f.canWrite(user)) throw new InsufficientPrivilagesException(); f.getDocs().add(info); Integer id = info.getId(); OutputStream out = new FileOutputStream(file); ByteUtils.write(docData, out); out.flush(); out.close(); File fi = new File(docsFolderPath + id); info.setSize((int) fi.length()); documentDao.makePersistent(info); } catch (Throwable t) { if (created && file != null) { file.delete(); } throw new RuntimeException(t); } }
@Transactional public Set<Document> retrieveAllDocumentsInFolder(String folderName, User user) throws InsufficientPrivilagesException { Folder f = folderDao.getFolderByPath(folderName, user); if (!f.canRead(user)) throw new InsufficientPrivilagesException(); Set<Document> docs = f.getDocs(); return docs; }
@Transactional public Document getDocumentByPath(String path, User user) throws InsufficientPrivilagesException { int lastSlash = path.lastIndexOf('/'); String folderPath = path.substring(0, lastSlash); Folder f = folderDao.getFolderByPath(folderPath, user); if (!f.canRead(user)) throw new InsufficientPrivilagesException(); Document doc = f.getDocumentByName(path.substring(lastSlash + 1)); return doc; }
@Transactional public Folder getFolderByPath(String path, User user) throws InsufficientPrivilagesException { if (!path.startsWith("/")) path = "/" + path; return folderDao.getFolderByPath(path, user); }