/** * @return Creates a temporary handler for temporary files. * @throws IOException */ public static FileSystemAttachmentPersistenceHandler newTemporaryHandler() throws IOException { return new FileSystemAttachmentPersistenceHandler(createTemporaryFolder()); }
@Override public void deleteAttachment(String key, SDocument document) { super.deleteAttachment(key, document); attachments.remove(key); }
@Override public FileSystemAttachmentRef getAttachment(String fileId) { if(fileId != null){ File file = findFileFromId(fileId); if (file.exists()) { return toRef(file); } } return null; }
private FileSystemAttachmentRef addAttachment(InputStream origin, long originLength, String name) { String id = UUID.randomUUID().toString(); File temp = findFileFromId(id); try (OutputStream fos = IOUtil.newBufferedOutputStream(temp); DigestInputStream inHash = HashUtil.toSHA1InputStream(IOUtil.newBuffredInputStream(origin)); OutputStream infoFOS = IOUtil.newBufferedOutputStream(infoFileFromId(id))) { IOUtils.copy(inHash, fos); String sha1 = HashUtil.bytesToBase16(inHash.getMessageDigest().digest()); IOUtil.writeLines(infoFOS, sha1, String.valueOf(originLength), name); return newRef(id, sha1, temp.getAbsolutePath(), originLength, name); } catch (Exception e) { throw SingularException.rethrow(e); } }
@Override public void deleteAttachment(String key, SDocument document) { if (key != null) { TempFileUtils.deleteOrException(findFileFromId(key), getClass()); TempFileUtils.deleteOrException(infoFileFromId(key), getClass()); } } }
@Override public FileSystemAttachmentRef addAttachment(File file, long length, String name, String hashSha1) { try (FileInputStream fis = new FileInputStream(file)){ return addAttachment(fis, length, name, hashSha1); } catch (Exception e) { throw new SingularFormException("Erro lendo origem de dados", e); } }
private FileSystemAttachmentRef toRef(File file) { try { List<String> lines = IOUtil.readLines(new File(file.getAbsolutePath() + INFO_SUFFIX)); return newRef(file.getName(), lines.get(0), file.getAbsolutePath(), Long.parseLong(lines.get(1)), lines.get(2)); } catch (Exception e) { throw SingularUtil.propagate(e); } }
@Override public AttachmentCopyContext<FileSystemAttachmentRef> copy(IAttachmentRef attachmentRef, SDocument document) { AttachmentCopyContext<FileSystemAttachmentRef> acr = super.copy(attachmentRef, document); attachments.put(acr.getNewAttachmentRef().getId(), acr.getNewAttachmentRef()); return acr; }
@Override public Collection<FileSystemAttachmentRef> getAttachments() { LinkedList<FileSystemAttachmentRef> result = new LinkedList<>(); File[] files = folder.listFiles(); if (files == null) { return result; } for (File f : files) { if (f.isFile() && f.exists() && !f.getName().endsWith(INFO_SUFFIX)) { result.add(toRef(f)); } } return result; }
@Override protected File infoFileFromId(String fileId) { File f = super.infoFileFromId(fileId); f.deleteOnExit(); return f; } }
@Override protected File findFileFromId(String fileId) { File f = super.findFileFromId(fileId); f.deleteOnExit(); return f; }
private FileSystemAttachmentRef addAttachment(InputStream origin, long originLength, String name, String hashSha1) { String id = UUID.randomUUID().toString(); File temp = findFileFromId(id); try (FileOutputStream f1 = new FileOutputStream(temp); OutputStream fos = IOUtil.newBufferedOutputStream(f1); DigestInputStream inHash = HashUtil.toSHA1InputStream(IOUtil.newBuffredInputStream(origin)); OutputStream infoFOS = IOUtil.newBufferedOutputStream(infoFileFromId(id))) { IOUtils.copy(inHash, fos); String sha1 = HashUtil.bytesToBase16(inHash.getMessageDigest().digest()); IOUtil.writeLines(infoFOS, sha1, String.valueOf(originLength), name); return newRef(id, sha1, temp.getAbsolutePath(), originLength, name); } catch (Exception e) { throw new SingularFormException("Erro adicionando anexo", e); } }
@Override public void deleteAttachment(String key, SDocument document) { if (key != null) { TempFileUtils.deleteOrException(findFileFromId(key), getClass()); TempFileUtils.deleteOrException(infoFileFromId(key), getClass()); } } }
@Override public FileSystemAttachmentRef addAttachment(File file, long length, String name, String hashSha1) { FileSystemAttachmentRef ref = super.addAttachment(file, length, name, hashSha1); attachments.put(ref.getId(), ref); return ref; }
private FileSystemAttachmentRef toRef(File file) { try { List<String> lines = IOUtil.readLines(new File(file.getAbsolutePath() + INFO_SUFFIX)); return newRef(file.getName(), lines.get(0), file.getAbsolutePath(), Long.parseLong(lines.get(1)), lines.get(2)); } catch (Exception e) { throw SingularUtil.propagate(e); } }
@Override public AttachmentCopyContext<FileSystemAttachmentRef> copy(IAttachmentRef attachmentRef, SDocument document) { AttachmentCopyContext<FileSystemAttachmentRef> acr = super.copy(attachmentRef, document); attachments.put(acr.getNewAttachmentRef().getId(), acr.getNewAttachmentRef()); return acr; }
@Override public Collection<FileSystemAttachmentRef> getAttachments() { LinkedList<FileSystemAttachmentRef> result = new LinkedList<>(); File[] files = folder.listFiles(); if (files == null) { return result; } for (File f : files) { if (f.isFile() && f.exists() && !f.getName().endsWith(INFO_SUFFIX)) { result.add(toRef(f)); } } return result; }
@Override public FileSystemAttachmentRef getAttachment(String fileId) { if(fileId != null){ File file = findFileFromId(fileId); if (file.exists()) { return toRef(file); } } return null; }
/** * @return Creates a temporary handler for temporary files. * @throws IOException */ public static FileSystemAttachmentPersistenceHandler newTemporaryHandler() throws IOException { return new FileSystemAttachmentPersistenceHandler(createTemporaryFolder()); }
@Override public FileSystemAttachmentRef addAttachment(File file, long length, String name) { try (FileInputStream fis = new FileInputStream(file)){ return addAttachment(fis, length, name); } catch (Exception e) { throw new SingularFormException("Erro lendo origem de dados", e); } }