-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMoverUtil.java
More file actions
224 lines (185 loc) · 9.19 KB
/
FileMoverUtil.java
File metadata and controls
224 lines (185 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.dotcms.userproxy.osgi;
import com.dotcms.userproxy.util.AppKey;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAssetAPI;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.util.ConfigUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import io.vavr.control.Try;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.osgi.framework.BundleContext;
public class FileMoverUtil {
private final File installedAppYaml = new File(
ConfigUtils.getAssetPath() + File.separator + "server" + File.separator + "apps" + File.separator
+ AppKey.USER_PROXY_APP_VALUE.appValue + ".yml");
String[] fieldUUIDs = new String[] {
"87ae3b42-4822-4ba8-b87c-258155f7012a",
"97a69459-e0aa-4278-bfd8-5797ab59e9db",
"f92e8cb0-1f95-4935-8782-92b62fc84001",
"db87ccb0-1f95-4935-8782-92b62fc84034",
};
public static List<JarEntry> listFilesInPackage(String packagePathStr) {
String packagePath = stripPackagePath(packagePathStr);
List<JarEntry> jarEntries = new ArrayList<>();
try {
String jarPath = FileMoverUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
try (java.util.jar.JarFile jarFile = new java.util.jar.JarFile(jarPath)) {
java.util.Enumeration<java.util.jar.JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (!entry.getName().startsWith(packagePath + "/") || entry.getName()
.equalsIgnoreCase(packagePath + "/")) {
continue;
}
Logger.debug(FileMoverUtil.class, "jar path:" + entry.getName());
jarEntries.add(entry);
}
}
} catch (java.io.IOException e) {
throw new RuntimeException("Error reading JAR file", e);
}
return jarEntries;
}
public static Map<String, String> loadGQLQueryMap(String packagePathStr) {
return Map.of();
}
/**
* Moves files from the plugin jar to the dotCMS virtual file system as
* fileAssets. If you do not specify a host,
* they will be placed on the default host
*
* @param packagePathInJar - the directory path in the jar to copy
*/
void copyFromJar(String packagePathInJar) {
this.copyFromJar(packagePathInJar,
Try.of(() -> APILocator.getHostAPI().findDefaultHost(APILocator.systemUser(), false))
.getOrElseThrow(DotRuntimeException::new));
}
static String stripPackagePath(String packagePathInJar) {
return packagePathInJar.startsWith("/") && packagePathInJar.endsWith("/")
? packagePathInJar.substring(1, packagePathInJar.length() - 1)
: packagePathInJar.startsWith("/")
? packagePathInJar.substring(1)
: packagePathInJar.endsWith("/")
? packagePathInJar.substring(0, packagePathInJar.length() - 1)
: packagePathInJar;
}
/**
* Moves files from the plugin jar to the dotCMS virtual file system as
* fileAssets. If you do not specify a host,
* they will be placed on the default host
*
* @param packagePathInJar - the directory path in the jar to copy
* @param site - the site to copy to
*/
void copyFromJar(String packagePathInJar, Host site) {
String strippedPackagePath = stripPackagePath(packagePathInJar);
List<JarEntry> directoryList = listFilesInPackage(strippedPackagePath).stream().filter(e -> e.isDirectory())
.collect(Collectors.toList());
List<JarEntry> fileList = listFilesInPackage(strippedPackagePath).stream().filter(e -> !e.isDirectory())
.collect(Collectors.toList());
try {
// create folders first
for (JarEntry e : directoryList) {
String folderPath = "/" + e.getName();
Logger.info(this.getClass(), "Creating folder: " + folderPath);
Folder subFolder = APILocator.getFolderAPI()
.findFolderByPath(folderPath, site, APILocator.systemUser(), false);
if (!UtilMethods.isSet(() -> subFolder.getIdentifier())) {
APILocator.getFolderAPI().createFolders(folderPath, site, APILocator.systemUser(), false);
}
}
for (JarEntry e : fileList) {
String fileName = e.getName().substring(e.getName().lastIndexOf("/") + 1);
String folderPath = "/" + e.getName().substring(0, e.getName().lastIndexOf("/"));
String fullPath = folderPath + "/" + fileName;
Folder destFolder = APILocator.getFolderAPI()
.findFolderByPath(folderPath, site, APILocator.systemUser(), false);
Identifier id = APILocator.getIdentifierAPI().find(site, fullPath);
if (UtilMethods.isSet(() -> id.getId())) {
Logger.warn(this.getClass(), "File already exists: " + folderPath + fileName);
continue;
}
System.out.println("Writing File:" + fullPath);
Logger.info(this.getClass(), "Creating file: " + fullPath);
// Create tmp file
File tmpDir = Files.createTempFile("osgi-app", "").toFile();
tmpDir.delete();
tmpDir.mkdirs();
File tmpFile = new File(tmpDir, fileName);
// write content to tmp file
try (final InputStream in = this.getClass().getResourceAsStream("/" + e.getName())) {
IOUtils.copy(in, Files.newOutputStream(tmpFile.toPath()));
} catch (IOException ioe) {
Logger.error(this.getClass(), "Error moving file: " + e.getName(), ioe);
continue;
}
//
Contentlet fileAsset = new Contentlet();
fileAsset.setFolder(destFolder.getIdentifier());
fileAsset.setHost(site.getIdentifier());
fileAsset.setStringProperty(FileAssetAPI.FILE_NAME_FIELD, fileName);
fileAsset.setBinary(FileAssetAPI.BINARY_FIELD, tmpFile);
fileAsset.setStringProperty(FileAssetAPI.TITLE_FIELD, fileName);
fileAsset.setContentTypeId(APILocator.getContentTypeAPI(APILocator.systemUser())
.find(FileAssetAPI.DEFAULT_FILE_ASSET_STRUCTURE_VELOCITY_VAR_NAME).id());
fileAsset.setProperty(Contentlet.DISABLE_WORKFLOW, true);
fileAsset.setLanguageId(APILocator.getLanguageAPI().getDefaultLanguage().getId());
Contentlet dotfile = APILocator.getContentletAPI().checkin(fileAsset, APILocator.systemUser(), false);
dotfile.setProperty(Contentlet.DISABLE_WORKFLOW, true);
APILocator.getContentletAPI().publish(dotfile, APILocator.systemUser(), false);
if (dotfile == null || dotfile.getIdentifier() == null) {
throw new DotRuntimeException("Unable to create file asset: " + fileName);
}
tmpFile.delete();
tmpDir.delete();
}
;
} catch (Exception e) {
throw new DotRuntimeException("Error moving files from jar to file assets:" + e.getMessage(), e);
}
}
/**
* copies the App yaml to the apps directory and refreshes the apps
*
* @throws IOException
*/
public void copyAppYml(BundleContext context) throws IOException {
Logger.info(this.getClass().getName(), "copying YAML File:" + installedAppYaml);
final String entryName = AppKey.USER_PROXY_APP_VALUE.appValue + ".yml";
final URL entry = context.getBundle().getEntry("/" + entryName);
if (entry == null) {
throw new IOException("Bundle entry not found: /" + entryName);
}
try (final InputStream in = entry.openStream()) {
IOUtils.copy(in, Files.newOutputStream(installedAppYaml.toPath()));
}
CacheLocator.getAppsCache().clearCache();
}
/**
* Deletes the App yaml to the apps directory and refreshes the apps
*
* @throws IOException
*/
public void deleteYml() throws IOException {
Logger.info(this.getClass().getName(), "deleting the YAML File:" + installedAppYaml);
installedAppYaml.delete();
CacheLocator.getAppsCache().clearCache();
}
}