Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-Archive type support #189

Merged
merged 2 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions owlplug-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.22</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
129 changes: 129 additions & 0 deletions owlplug-client/src/main/java/com/owlplug/core/utils/ArchiveUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* OwlPlug
* Copyright (C) 2021 Arthur <[email protected]>
*
* This file is part of OwlPlug.
*
* OwlPlug is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3
* as published by the Free Software Foundation.
*
* OwlPlug 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 OwlPlug. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owlplug.core.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArchiveUtils {

private static final Logger log = LoggerFactory.getLogger(ArchiveUtils.class);

public static void extract(String source, String dest) {
File sourceFile = new File(source);
File destDirectory = new File(dest);

extract(sourceFile, destDirectory);
}

public static void extract(File source, File dest) {
try {
uncompress(source, dest);
} catch (Exception e) {
log.error("Error extracting archive {} at {}", source.getAbsolutePath(),
dest.getAbsolutePath(), e);
throw new RuntimeException(e);
}

}

private static boolean isCompressed(File file) throws IOException {
log.debug("Verify file compression: {}", file.getAbsolutePath());
try (InputStream inputStream = new FileInputStream(file);
InputStream bufferedIn = new BufferedInputStream(inputStream)) {
String comp = CompressorStreamFactory.detect(bufferedIn);
log.debug("Compression signature found: {}", comp);
return true;
} catch (CompressorException e) {
log.debug("Compression signature not found");
return false;
}

}

private static void uncompress(File sourceFile, File destinationDirectory) throws IOException {

if (isCompressed(sourceFile)) {
try (InputStream fi = new FileInputStream(sourceFile);
InputStream bi = new BufferedInputStream(fi);
CompressorInputStream gzi = new CompressorStreamFactory().createCompressorInputStream(bi);
InputStream bgzi = new BufferedInputStream(gzi);
ArchiveInputStream o = new ArchiveStreamFactory().createArchiveInputStream(bgzi)) {

uncompress(o, destinationDirectory);
} catch (CompressorException e) {
throw new IOException("Error while uncompressing the archive stream: " + sourceFile.getAbsolutePath(), e);
} catch (ArchiveException e) {
throw new IOException("Error while extracting the archive stream: " + sourceFile.getAbsolutePath(), e);
}

} else {
try (InputStream fi = new FileInputStream(sourceFile);
InputStream bi = new BufferedInputStream(fi);
ArchiveInputStream o = new ArchiveStreamFactory().createArchiveInputStream(bi)) {

uncompress(o, destinationDirectory);
} catch (ArchiveException e) {
throw new IOException("Error while extracting the archive stream: " + sourceFile.getAbsolutePath(), e);
}
}
}

private static void uncompress(ArchiveInputStream o, File destinationDirectory) throws IOException {

ArchiveEntry entry = null;
while ((entry = o.getNextEntry()) != null) {
if (!o.canReadEntryData(entry)) {
log.debug("Stream entry cannot be read: {}", entry.getName());
continue;
}

File f = new File(destinationDirectory, entry.getName());
if (entry.isDirectory()) {
if (!f.isDirectory() && !f.mkdirs()) {
throw new IOException("failed to create directory " + f);
}
} else {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException("failed to create directory " + parent);
}
try (OutputStream output = Files.newOutputStream(f.toPath())) {
IOUtils.copy(o, output);
}
}
}
}

}
48 changes: 0 additions & 48 deletions owlplug-client/src/main/java/com/owlplug/core/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,54 +119,6 @@ private static void innerListFiles(List<File> files, File directory, boolean inc
}
}

public static void unzip(String source, String dest) throws IOException {

// Open the file
try (ZipFile file = new ZipFile(source)) {
FileSystem fileSystem = FileSystems.getDefault();
// Get file entries
Enumeration<? extends ZipEntry> entries = file.entries();

// We will unzip files in this folder
String uncompressedDirectory = dest;
Files.createDirectory(fileSystem.getPath(uncompressedDirectory));

// Iterate over entries
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// If directory then create a new directory in uncompressed folder
if (entry.isDirectory()) {
log.debug("Creating Directory:" + uncompressedDirectory + File.separator + entry.getName());
Files.createDirectories(fileSystem.getPath(uncompressedDirectory + File.separator + entry.getName()));

// Else create the file
} else {
InputStream is = file.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
String uncompressedFileName = uncompressedDirectory + File.separator + entry.getName();

new File(uncompressedFileName).getParentFile().mkdirs();

Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
Files.createFile(uncompressedFilePath);

FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);

byte[] buffer = new byte[2048];
int read = 0;
while ((read = bis.read(buffer)) > 0) {
fileOutput.write(buffer, 0, read);
}

fileOutput.close();
log.debug("Written :" + entry.getName());
}
}
} catch (IOException e) {
throw e;
}
}

public static void copyDirectory(File source, File target) throws IOException {
org.apache.commons.io.FileUtils.copyDirectory(source, target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,25 +335,26 @@ public boolean installBundle(PackageBundle bundle) {
bundle.getRemotePackage().getName(),
bundle.getName());


String baseDirectoryPath = exploreService.getBundleInstallFolder(bundle);
String relativeDirectoryPath = this.getPreferences().get(ApplicationDefaults.STORE_DIRECTORY_KEY, "");

Boolean shouldGroupByCreator = this.getPreferences().getBoolean(ApplicationDefaults.STORE_BY_CREATOR_ENABLED_KEY, false);

//if the enduser wishes to group plugins by their creator,
//then we need to include the subdirectory as well.
if (shouldGroupByCreator) {
String creator = FileUtils.sanitizeFileName(bundle.getRemotePackage().getCreator());
relativeDirectoryPath = relativeDirectoryPath + File.separator + creator;
}

File selectedDirectory = null;
String baseDirectoryPath = exploreService.getBundleInstallFolder(bundle);

// A custom root directory to store plugin is defined
if (this.getPreferences().getBoolean(ApplicationDefaults.STORE_DIRECTORY_ENABLED_KEY, false)) {
// A custom root directory to store plugin is defined and the base directory for
// the bundle type is defined or not blank.
if (this.getPreferences().getBoolean(ApplicationDefaults.STORE_DIRECTORY_ENABLED_KEY, false) &&
baseDirectoryPath != null && !baseDirectoryPath.isBlank()) {
// Store install target is already defined

String relativeDirectoryPath = this.getPreferences().get(ApplicationDefaults.STORE_DIRECTORY_KEY, "");
Boolean shouldGroupByCreator = this.getPreferences().getBoolean(ApplicationDefaults.STORE_BY_CREATOR_ENABLED_KEY, false);

//if the enduser wishes to group plugins by their creator,
//then we need to include the subdirectory as well.
if (shouldGroupByCreator) {
String creator = FileUtils.sanitizeFileName(bundle.getRemotePackage().getCreator());
relativeDirectoryPath = relativeDirectoryPath + File.separator + creator;
}

selectedDirectory = new File(baseDirectoryPath, relativeDirectoryPath);

// A plugin root directory is not defined
Expand All @@ -369,7 +370,6 @@ public boolean installBundle(PackageBundle bundle) {
Window mainWindow = masonryPane.getScene().getWindow();
selectedDirectory = directoryChooser.showDialog(mainWindow);
}



// If any install target directory can be found, abort install
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.owlplug.core.tasks.AbstractTask;
import com.owlplug.core.tasks.TaskException;
import com.owlplug.core.tasks.TaskResult;
import com.owlplug.core.utils.ArchiveUtils;
import com.owlplug.core.utils.CryptoUtils;
import com.owlplug.core.utils.FileUtils;
import com.owlplug.core.utils.nio.CallbackByteChannel;
Expand Down Expand Up @@ -100,7 +101,7 @@ protected TaskResult call() throws Exception {
this.updateMessage("Installing plugin " + bundle.getRemotePackage().getName() + " - Extracting files...");
File extractedArchiveFolder = new File(ApplicationDefaults.getTempDownloadDirectory() + "/" + "temp-"
+ archiveFile.getName().replace(".owlpack", ""));
FileUtils.unzip(archiveFile.getAbsolutePath(), extractedArchiveFolder.getAbsolutePath());
ArchiveUtils.extract(archiveFile.getAbsolutePath(), extractedArchiveFolder.getAbsolutePath());

this.commitProgress(30);

Expand Down