Browse Source

Handle shutdowns when zipping a large number of files.

qdn
CalDescent 3 years ago
parent
commit
59ef66f46d
  1. 2
      src/main/java/org/qortal/api/resource/ArbitraryResource.java
  2. 4
      src/main/java/org/qortal/api/resource/WebsiteResource.java
  3. 8
      src/main/java/org/qortal/arbitrary/ArbitraryDataWriter.java
  4. 9
      src/main/java/org/qortal/utils/ZipUtils.java

2
src/main/java/org/qortal/api/resource/ArbitraryResource.java

@ -275,7 +275,7 @@ public class ArbitraryResource {
ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression);
try { try {
arbitraryDataWriter.save(); arbitraryDataWriter.save();
} catch (IOException | DataException e) { } catch (IOException | DataException | InterruptedException e) {
LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage());
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE);
} catch (RuntimeException e) { } catch (RuntimeException e) {

4
src/main/java/org/qortal/api/resource/WebsiteResource.java

@ -109,7 +109,7 @@ public class WebsiteResource {
ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression);
try { try {
arbitraryDataWriter.save(); arbitraryDataWriter.save();
} catch (IOException | DataException e) { } catch (IOException | DataException | InterruptedException e) {
LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage());
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE);
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -211,7 +211,7 @@ public class WebsiteResource {
ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(directoryPath), name, service, method, compression); ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(directoryPath), name, service, method, compression);
try { try {
arbitraryDataWriter.save(); arbitraryDataWriter.save();
} catch (IOException | DataException e) { } catch (IOException | DataException | InterruptedException e) {
LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage());
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE);
} catch (RuntimeException e) { } catch (RuntimeException e) {

8
src/main/java/org/qortal/arbitrary/ArbitraryDataWriter.java

@ -52,7 +52,7 @@ public class ArbitraryDataWriter {
this.compression = compression; this.compression = compression;
} }
public void save() throws IllegalStateException, IOException, DataException { public void save() throws IllegalStateException, IOException, DataException, InterruptedException {
try { try {
this.preExecute(); this.preExecute();
this.process(); this.process();
@ -136,7 +136,7 @@ public class ArbitraryDataWriter {
this.validatePatch(); this.validatePatch();
} }
private void validatePatch() throws IOException { private void validatePatch() {
if (this.filePath == null) { if (this.filePath == null) {
throw new IllegalStateException("Null path after creating patch"); throw new IllegalStateException("Null path after creating patch");
} }
@ -158,13 +158,14 @@ public class ArbitraryDataWriter {
} }
} }
private void compress() { private void compress() throws InterruptedException {
// Compress the data if requested // Compress the data if requested
if (this.compression != Compression.NONE) { if (this.compression != Compression.NONE) {
this.compressedPath = Paths.get(this.workingPath.toString() + File.separator + "data.zip"); this.compressedPath = Paths.get(this.workingPath.toString() + File.separator + "data.zip");
try { try {
if (this.compression == Compression.ZIP) { if (this.compression == Compression.ZIP) {
LOGGER.info("Compressing...");
ZipUtils.zip(this.filePath.toString(), this.compressedPath.toString(), "data"); ZipUtils.zip(this.filePath.toString(), this.compressedPath.toString(), "data");
} }
else { else {
@ -190,6 +191,7 @@ public class ArbitraryDataWriter {
this.encryptedPath = Paths.get(this.workingPath.toString() + File.separator + "data.zip.encrypted"); this.encryptedPath = Paths.get(this.workingPath.toString() + File.separator + "data.zip.encrypted");
try { try {
// Encrypt the file with AES // Encrypt the file with AES
LOGGER.info("Encrypting...");
this.aesKey = AES.generateKey(256); this.aesKey = AES.generateKey(256);
AES.encryptFile("AES", this.aesKey, this.filePath.toString(), this.encryptedPath.toString()); AES.encryptFile("AES", this.aesKey, this.filePath.toString(), this.encryptedPath.toString());

9
src/main/java/org/qortal/utils/ZipUtils.java

@ -25,6 +25,8 @@
package org.qortal.utils; package org.qortal.utils;
import org.qortal.controller.Controller;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -35,7 +37,7 @@ import java.util.zip.ZipOutputStream;
public class ZipUtils { public class ZipUtils {
public static void zip(String sourcePath, String destFilePath, String fileName) throws IOException { public static void zip(String sourcePath, String destFilePath, String fileName) throws IOException, InterruptedException {
File sourceFile = new File(sourcePath); File sourceFile = new File(sourcePath);
if (fileName == null) { if (fileName == null) {
fileName = sourceFile.getName(); fileName = sourceFile.getName();
@ -47,7 +49,10 @@ public class ZipUtils {
fileOutputStream.close(); fileOutputStream.close();
} }
public static void zip(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException { public static void zip(final File fileToZip, final String fileName, final ZipOutputStream zipOut) throws IOException, InterruptedException {
if (Controller.isStopping()) {
throw new InterruptedException("Controller is stopping");
}
if (fileToZip.isDirectory()) { if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) { if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName)); zipOut.putNextEntry(new ZipEntry(fileName));

Loading…
Cancel
Save