BrightSide Workbench Full Report + Source Code
AttachFiles.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.file.util;
20 
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.stream.Collectors;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.file.Document;
30 import org.turro.file.Folder;
31 
36 public class AttachFiles {
37 
38  public void toFolder(Folder folder) {
39  folder.ensure();
40  publishableDocs().forEach(doc -> doc.copyTo(folder));
41  }
42 
43  public void remove() {
45  removePrivate();
46  }
47 
48  public void removePublishable() {
49  getPublishableFolder().remove();
50  }
51 
52  public void removePrivate() {
53  getPrivateFolder().remove();
54  }
55 
56  public List<Document> publishableDocs() {
57  try {
58  return getPublishableFolder().documents(1000);
59  } catch (IOException ex) {
60  Logger.getLogger(AttachFiles.class.getName()).log(Level.SEVERE, null, ex);
61  }
62  return Collections.EMPTY_LIST;
63  }
64 
65  public List<Document> privateDocs() {
66  try {
67  return getPrivateFolder().documents(1000);
68  } catch (IOException ex) {
69  Logger.getLogger(AttachFiles.class.getName()).log(Level.SEVERE, null, ex);
70  }
71  return Collections.EMPTY_LIST;
72  }
73 
74  /* Folders */
75 
76  private Folder getPublishableFolder() {
77  return Folder.from(ElephantContext.getRealPath("/_internal/files" + path));
78  }
79 
80  private Folder getPrivateFolder() {
81  return Folder.from(ElephantContext.getRealPath("/WEB-INF/files" + path));
82  }
83 
84  /* Utils */
85 
86  public static Set<String> getAllPublishablePaths(String root) {
87  try {
88  Folder folder = AttachFiles.from("/" + root).getPublishableFolder();
89  if(folder.exists()) {
90  return folder.folders().stream()
91  .map(f -> "/" + root + "/" + f.name()).collect(Collectors.toSet());
92  }
93  } catch (IOException ex) {
94  Logger.getLogger(AttachFiles.class.getName()).log(Level.SEVERE, null, ex);
95  }
96  return Collections.EMPTY_SET;
97  }
98 
99  public static Set<String> getAllPrivatePaths(String root) {
100  try {
101  Folder folder = AttachFiles.from("/" + root).getPrivateFolder();
102  if(folder.exists()) {
103  return folder.folders().stream()
104  .map(f -> "/" + root + "/" + f.name()).collect(Collectors.toSet());
105  }
106  } catch (IOException ex) {
107  Logger.getLogger(AttachFiles.class.getName()).log(Level.SEVERE, null, ex);
108  }
109  return Collections.EMPTY_SET;
110  }
111 
112  /* Factory */
113 
114  public static AttachFiles from(String path) {
115  return new AttachFiles(path);
116  }
117 
118  private final String path;
119 
120  private AttachFiles(String path) {
121  this.path = path;
122  }
123 
124 }
static Set< String > getAllPublishablePaths(String root)
static AttachFiles from(String path)
List< Document > privateDocs()
void toFolder(Folder folder)
static Set< String > getAllPrivatePaths(String root)
List< Document > publishableDocs()