Improved logging for client-aborted downloads

This commit is contained in:
grimsi
2022-08-10 21:15:58 +02:00
parent c196fd2cbc
commit 38bbdc7c57
2 changed files with 24 additions and 6 deletions
@@ -0,0 +1,7 @@
package de.grimsi.gameyfin.exceptions;
public class DownloadAbortedException extends RuntimeException {
public DownloadAbortedException() {
super();
}
}
@@ -2,18 +2,19 @@ package de.grimsi.gameyfin.service;
import de.grimsi.gameyfin.entities.Company; import de.grimsi.gameyfin.entities.Company;
import de.grimsi.gameyfin.entities.DetectedGame; import de.grimsi.gameyfin.entities.DetectedGame;
import de.grimsi.gameyfin.exceptions.DownloadAbortedException;
import de.grimsi.gameyfin.igdb.IgdbApiProperties; import de.grimsi.gameyfin.igdb.IgdbApiProperties;
import de.grimsi.gameyfin.repositories.DetectedGameRepository; import de.grimsi.gameyfin.repositories.DetectedGameRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.data.repository.core.RepositoryCreationException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
@@ -63,7 +64,7 @@ public class DownloadService {
public String getDownloadFileName(DetectedGame g) { public String getDownloadFileName(DetectedGame g) {
Path path = Path.of(g.getPath()); Path path = Path.of(g.getPath());
if(!path.toFile().isDirectory()) return getFilenameWithExtension(path); if (!path.toFile().isDirectory()) return getFilenameWithExtension(path);
return getFilenameWithExtension(path) + ".zip"; return getFilenameWithExtension(path) + ".zip";
} }
@@ -87,8 +88,16 @@ public class DownloadService {
Path path = Path.of(game.getPath()); Path path = Path.of(game.getPath());
if(path.toFile().isDirectory()) { if (path.toFile().isDirectory()) {
try {
downloadFilesAsZip(path, outputStream); downloadFilesAsZip(path, outputStream);
} catch(DownloadAbortedException e) {
stopWatch.stop();
log.info("Download of game {} was aborted by client after {} seconds", game.getTitle(), (int) stopWatch.getTotalTimeSeconds());
return;
}
} else { } else {
downloadFile(path, outputStream); downloadFile(path, outputStream);
} }
@@ -99,7 +108,6 @@ public class DownloadService {
} }
public void downloadGameCoversFromIgdb() { public void downloadGameCoversFromIgdb() {
StopWatch stopWatch = new StopWatch(); StopWatch stopWatch = new StopWatch();
@@ -163,7 +171,7 @@ public class DownloadService {
} }
private void downloadFilesAsZip(Path path, OutputStream outputStream) { private void downloadFilesAsZip(Path path, OutputStream outputStream) {
ZipOutputStream zos = new ZipOutputStream(outputStream){{ ZipOutputStream zos = new ZipOutputStream(outputStream) {{
def.setLevel(Deflater.NO_COMPRESSION); def.setLevel(Deflater.NO_COMPRESSION);
}}; }};
@@ -180,6 +188,9 @@ public class DownloadService {
}); });
zos.close(); zos.close();
} catch (ClientAbortException e) {
// Aborted downloads will be handled gracefully
throw new DownloadAbortedException();
} catch (IOException e) { } catch (IOException e) {
log.error("Error while zipping files:", e); log.error("Error while zipping files:", e);
} }