-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first working commands
- Loading branch information
Thorsten Marx
committed
Jun 7, 2024
1 parent
e1237fc
commit 0e4ce61
Showing
22 changed files
with
882 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/repository/ModuleInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.thmarx.cms.extensions.repository; | ||
|
||
/*- | ||
* #%L | ||
* cms-extensions | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
@Data | ||
public class ModuleInfo { | ||
|
||
private String id; | ||
private String version; | ||
private String name; | ||
private String description; | ||
private String author; | ||
private String url; | ||
private String compatibility; | ||
private String file; | ||
} |
121 changes: 121 additions & 0 deletions
121
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/repository/ModulePacker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.github.thmarx.cms.extensions.repository; | ||
|
||
/*- | ||
* #%L | ||
* modules-manager | ||
* %% | ||
* Copyright (C) 2023 Thorsten Marx | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Enumeration; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
/** | ||
* | ||
* @author marx | ||
*/ | ||
public class ModulePacker { | ||
|
||
/** | ||
* Delets a dir recursively deleting anything inside it. | ||
* | ||
* @param dir The dir to delete | ||
* @return true if the dir was successfully deleted | ||
*/ | ||
protected static boolean deleteDirectory(File dir) { | ||
if (!dir.exists() || !dir.isDirectory()) { | ||
return false; | ||
} | ||
|
||
String[] files = dir.list(); | ||
for (int i = 0, len = files.length; i < len; i++) { | ||
File f = new File(dir, files[i]); | ||
if (f.isDirectory()) { | ||
deleteDirectory(f); | ||
} else { | ||
f.delete(); | ||
} | ||
} | ||
return dir.delete(); | ||
} | ||
|
||
protected static boolean moveDirectoy (final File src, final File dest) { | ||
return src.renameTo(dest); | ||
} | ||
|
||
/** | ||
* Unpack a zip file | ||
* | ||
* @param theFile | ||
* @param targetDir | ||
* @return the file | ||
* @throws IOException | ||
*/ | ||
protected static File unpackArchive(File theFile, File targetDir) throws IOException { | ||
if (!theFile.exists()) { | ||
throw new IOException(theFile.getAbsolutePath() + " does not exist"); | ||
} | ||
if (!buildDirectory(targetDir)) { | ||
throw new IOException("Could not create directory: " + targetDir); | ||
} | ||
boolean found = false; | ||
String moduleid = null; | ||
try (ZipFile zipFile = new ZipFile(theFile)) { | ||
for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { | ||
ZipEntry entry = (ZipEntry) entries.nextElement(); | ||
File file = new File(targetDir, File.separator + entry.getName()); | ||
if (entry.isDirectory() && !found) { | ||
moduleid = file.getName(); | ||
found = true; | ||
} | ||
if (!buildDirectory(file.getParentFile())) { | ||
throw new IOException("Could not create directory: " + file.getParentFile()); | ||
} | ||
if (!entry.isDirectory()) { | ||
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file))); | ||
} else if (!buildDirectory(file)) { | ||
throw new IOException("Could not create directory: " + file); | ||
} | ||
} | ||
} | ||
return new File(targetDir, moduleid); | ||
} | ||
|
||
private static void copyInputStream(InputStream in, OutputStream out) throws IOException { | ||
byte[] buffer = new byte[1024]; | ||
int len = in.read(buffer); | ||
while (len >= 0) { | ||
out.write(buffer, 0, len); | ||
len = in.read(buffer); | ||
} | ||
in.close(); | ||
out.close(); | ||
} | ||
|
||
private static boolean buildDirectory(File file) { | ||
return file.exists() || file.mkdirs(); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...ons/src/main/java/com/github/thmarx/cms/extensions/repository/RemoteModuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.github.thmarx.cms.extensions.repository; | ||
|
||
/*- | ||
* #%L | ||
* cms-extensions | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
* @param <T> | ||
*/ | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class RemoteModuleRepository<T> { | ||
|
||
static HttpClient client = HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_2) | ||
.followRedirects(HttpClient.Redirect.ALWAYS) | ||
.build(); | ||
|
||
private final Class<T> type; | ||
private final String baseUrl; | ||
|
||
public boolean exists(String id) { | ||
try { | ||
var moduleInfoUrl = baseUrl + "/main/%s/%s.yaml" | ||
.formatted(id, id); | ||
|
||
URI uri = URI.create(moduleInfoUrl); | ||
HttpRequest request = HttpRequest.newBuilder(uri).build(); | ||
return client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode() == 200; | ||
} catch (IOException | InterruptedException ex) { | ||
log.error("", ex); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public Optional<T> getInfo(String extension) { | ||
try { | ||
var moduleInfoUrl = baseUrl + "/main/%s/%s.yaml" | ||
.formatted(extension, extension); | ||
|
||
URI uri = URI.create(moduleInfoUrl); | ||
HttpRequest request = HttpRequest.newBuilder(uri) | ||
.build(); | ||
final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() != 200) { | ||
return Optional.empty(); | ||
} | ||
String content = response.body(); | ||
|
||
return Optional.of(new Yaml().loadAs(content, type)); | ||
} catch (IOException | InterruptedException ex) { | ||
log.error("", ex); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public void download(String url, Path target) { | ||
|
||
try { | ||
Path tempDirectory = Files.createTempDirectory("modules"); | ||
|
||
var request = HttpRequest.newBuilder(URI.create(url)).GET().build(); | ||
HttpResponse<Path> response = client.send( | ||
request, | ||
HttpResponse.BodyHandlers.ofFile(tempDirectory.resolve(System.currentTimeMillis() + ".zip"))); | ||
|
||
var downloaded = response.body(); | ||
|
||
File moduleTempDir = ModulePacker.unpackArchive(downloaded.toFile(), tempDirectory.toFile()); | ||
|
||
ModulePacker.moveDirectoy(moduleTempDir, target.resolve(moduleTempDir.getName()).toFile()); | ||
} catch (Exception ex) { | ||
log.error("", ex); | ||
throw new RuntimeException("error downloading module"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
cms-extensions/src/main/java/com/github/thmarx/cms/extensions/repository/ThemeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.thmarx.cms.extensions.repository; | ||
|
||
/*- | ||
* #%L | ||
* cms-extensions | ||
* %% | ||
* Copyright (C) 2023 - 2024 Marx-Software | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* | ||
* @author t.marx | ||
*/ | ||
@Data | ||
public class ThemeInfo { | ||
|
||
private String id; | ||
private String version; | ||
private String name; | ||
private String description; | ||
private String author; | ||
private String url; | ||
private String compatibility; | ||
private String file; | ||
} |
Oops, something went wrong.