Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first working commands
  • Loading branch information
Thorsten Marx committed Jun 7, 2024
1 parent e1237fc commit 0e4ce61
Show file tree
Hide file tree
Showing 22 changed files with 882 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author t.marx
*/
@Data
public class Extension {
public class ExtensionInfo {

private String id;
private String version;
Expand Down
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;
}
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();
}
}
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

/**
*
Expand Down Expand Up @@ -73,7 +70,7 @@ public Optional<String> getContent (String extension) {
return Optional.empty();
}

public Optional<Extension> getInfo (String extension) {
public Optional<ExtensionInfo> getInfo (String extension) {
try {
var moduleInfoUrl = "https://raw.githubusercontent.com/thmarx/extension-registry/main/%s/%s.yaml"
.formatted(extension, extension);
Expand All @@ -83,7 +80,7 @@ public Optional<Extension> getInfo (String extension) {
String content = client.send(request, BodyHandlers.ofString()).body();


return Optional.of(new Yaml().loadAs(content, Extension.class));
return Optional.of(new Yaml().loadAs(content, ExtensionInfo.class));
} catch (IOException | InterruptedException ex) {
log.error("", ex);
}
Expand Down
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;
}
Loading

0 comments on commit 0e4ce61

Please sign in to comment.