BrightSide Workbench Full Report + Source Code
Attachments.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.attach.search;
20 
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
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;
37 
42 public class Attachments {
43 
44  public void toFolder(Folder folder) {
45  folder.ensure();
46  get().forEach(a -> saveToFile(folder, a));
47  }
48 
49  public void remove() {
50  SqlClause.delete("Attachment")
51  .where().group()
52  .equal("path", path)
53  .or().startsWith("path", path + "/")
54  .endGroup()
55  .startIf(onlyPublic)
56  .and().isTrue("publishable")
57  .endIf()
58  .startIf(banRestricted)
59  .and().not().contains("path", "#")
60  .endIf()
61  .dao(new AttachPU())
62  .execute();
63  }
64 
65  public List<Attachment> get() {
66  return SqlClause.select("a").from("Attachment a")
67  .where().group()
68  .equal("a.path", path)
69  .or().startsWith("a.path", path + "/")
70  .endGroup()
71  .startIf(onlyPublic)
72  .and().isTrue("a.publishable")
73  .endIf()
74  .startIf(banRestricted)
75  .and().not().contains("a.path", "#")
76  .endIf()
77  .orderBy("a.modification")
78  .dao(new AttachPU())
79  .resultList(Attachment.class);
80  }
81 
82  public PathTree getTree() {
83  return PathTree.fromObjects(SqlClause.select("a").from("Attachment a")
84  .where().group()
85  .equal("a.path", path)
86  .or().startsWith("a.path", path + "/")
87  .endGroup()
88  .startIf(onlyPublic)
89  .and().isTrue("a.publishable")
90  .endIf()
91  .startIf(banRestricted)
92  .and().not().contains("a.path", "#")
93  .and().isFalse("a.requiresAcceptance")
94  .and().isFalse("a.watermark")
95  .endIf()
96  .orderBy("a.modification")
97  .dao(new AttachPU())
98  .stream(Attachment.class)
99  .map(a -> {
100  return PathObject.from(
101  a.getRelativePath(path) + "/" + a.getFileName(),
102  new AttachmentNode(a.getId(), a.getFileSize(), a.getModification(), a.getFileExtension()));
103  })
104  .toList(), new AttachmentNode(null, 0, null, null));
105  }
106 
107  /* IO */
108 
109  private void saveToFile(Folder folder, Attachment attachment) {
110  attachment = AttachmentUtil.withContent(attachment);
111  try {
112  Files.write(folder.path().resolve(attachment.getFileName()),
113  attachment.getAttachContent().getFileContent());
114  } catch (IOException ex) {
115  Logger.getLogger(Attachments.class.getName()).log(Level.SEVERE, null, ex);
116  }
117  }
118 
119  /* Utils */
120 
121  public static Set<String> getAllPaths(String root) {
122  try(Stream<String> stream = SqlClause.select("distinct a.path").from("Attachment a")
123  .where().startsWith("a.path", "/" + root + "/")
124  .dao(new AttachPU())
125  .stream(String.class)) {
126  return new HashSet<>(
127  stream.map(path -> Path.pathFrom(path).getTill(2))
128  .toList());
129  }
130  }
131 
132  /* Attibutes */
133 
134  private boolean onlyPublic, banRestricted;
135 
137  onlyPublic = true;
138  return this;
139  }
140 
142  banRestricted = true;
143  return this;
144  }
145 
146  /* Factory */
147 
148  public static Attachments from(String path) {
149  return new Attachments(path);
150  }
151 
152  private final String path;
153 
154  private Attachments(String path) {
155  this.path = path;
156  }
157 
158 }
static Attachments from(String path)
static Set< String > getAllPaths(String root)
static Attachment withContent(Attachment attachment)