19 package org.turro.attach.search;
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import java.util.stream.Stream;
29 import org.turro.attach.db.AttachPU;
30 import org.turro.attach.entity.Attachment;
31 import org.turro.attach.zul.AttachmentUtil;
32 import org.turro.file.Folder;
33 import org.turro.path.Path;
34 import org.turro.path.PathObject;
35 import org.turro.path.PathTree;
36 import org.turro.sql.SqlClause;
46 get().forEach(a -> saveToFile(folder, a));
49 public void remove() {
50 SqlClause.delete(
"Attachment")
53 .or().startsWith(
"path", path +
"/")
56 .and().isTrue(
"publishable")
58 .startIf(banRestricted)
59 .and().not().contains(
"path",
"#")
65 public List<Attachment>
get() {
66 return SqlClause.select(
"a").from(
"Attachment a")
68 .equal(
"a.path", path)
69 .or().startsWith(
"a.path", path +
"/")
72 .and().isTrue(
"a.publishable")
74 .startIf(banRestricted)
75 .and().not().contains(
"a.path",
"#")
77 .orderBy(
"a.modification")
83 return PathTree.fromObjects(SqlClause.select(
"a").from(
"Attachment a")
85 .equal(
"a.path", path)
86 .or().startsWith(
"a.path", path +
"/")
89 .and().isTrue(
"a.publishable")
91 .startIf(banRestricted)
92 .and().not().contains(
"a.path",
"#")
93 .and().isFalse(
"a.requiresAcceptance")
94 .and().isFalse(
"a.watermark")
96 .orderBy(
"a.modification")
100 return PathObject.from(
101 a.getRelativePath(path) +
"/" + a.getFileName(),
102 new AttachmentNode(a.getId(), a.getFileSize(), a.getModification(), a.getFileExtension()));
109 private void saveToFile(Folder folder,
Attachment attachment) {
112 Files.write(folder.path().resolve(attachment.
getFileName()),
114 }
catch (IOException ex) {
115 Logger.getLogger(Attachments.class.getName()).log(Level.SEVERE,
null, ex);
122 try(Stream<String> stream = SqlClause.select(
"distinct a.path").from(
"Attachment a")
123 .where().startsWith(
"a.path",
"/" + root +
"/")
125 .stream(String.class)) {
126 return new HashSet<>(
127 stream.map(path -> Path.pathFrom(path).getTill(2))
134 private boolean onlyPublic, banRestricted;
142 banRestricted =
true;
152 private final String path;
AttachContent getAttachContent()
Attachments banRestricted()
void toFolder(Folder folder)
static Attachments from(String path)
static Set< String > getAllPaths(String root)
static Attachment withContent(Attachment attachment)