Close more resources

This commit is contained in:
2022-10-29 23:47:21 +02:00
parent f755884497
commit 39254c3c7d
3 changed files with 26 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.core.PathSegment; import javax.ws.rs.core.PathSegment;
@@ -79,8 +80,8 @@ public class FileInfoService {
Path path = null; Path path = null;
try { try {
path = rootFolderPath.resolve("file").resolve(requestedPath.replaceAll("^/*", "")); path = rootFolderPath.resolve("file").resolve(requestedPath.replaceAll("^/*", ""));
} catch (Exception ignored) { } catch (Exception e) {
ignored.printStackTrace(); e.printStackTrace();
return null; return null;
} }
if (Files.exists(path)) { if (Files.exists(path)) {
@@ -92,7 +93,8 @@ public class FileInfoService {
requestedThumbUrl = Paths.get("/").resolve( requestedThumbUrl = Paths.get("/").resolve(
rootFolderPath.relativize(requestedThumbPath)).toUri().getRawPath(); rootFolderPath.relativize(requestedThumbPath)).toUri().getRawPath();
} }
} catch (Exception ignored) { } catch (Exception e) {
e.printStackTrace();
} }
if (Files.isDirectory(path)) { if (Files.isDirectory(path)) {
if (requestedFilename.isEmpty()) { if (requestedFilename.isEmpty()) {
@@ -107,7 +109,8 @@ public class FileInfoService {
} }
StringBuilder playlistContent = new StringBuilder(); StringBuilder playlistContent = new StringBuilder();
try { try {
Files.list(path).forEach(p -> { Stream<Path> stream = Files.list(path);
stream.forEach(p -> {
String fileName = p.getFileName().toString(); String fileName = p.getFileName().toString();
String fileUri = URLEncoder.encode(fileName, StandardCharsets.UTF_8) String fileUri = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
.replace("+", "%20"); .replace("+", "%20");
@@ -124,7 +127,8 @@ public class FileInfoService {
thumbPath = Paths.get("/lalafin/file" + requestedPath thumbPath = Paths.get("/lalafin/file" + requestedPath
+ "/.thumbnails/" + fileName + ".png"); + "/.thumbnails/" + fileName + ".png");
} }
} catch (Exception ignored) { } catch (Exception e) {
e.printStackTrace();
} }
FileInfoBase contentInfo; FileInfoBase contentInfo;
@@ -177,12 +181,13 @@ public class FileInfoService {
contentInfo.thumbnailUrl += ".png"; contentInfo.thumbnailUrl += ".png";
} }
} }
} catch (Exception ignored) { } catch (Exception e) {
e.printStackTrace();
} }
} }
folderInfo.content.add(contentInfo); folderInfo.content.add(contentInfo);
}); });
stream.close();
if (playlistPath != null) { if (playlistPath != null) {
playlistPath.getParent().toFile().mkdirs(); playlistPath.getParent().toFile().mkdirs();
Files.writeString(playlistPath, playlistContent.toString()); Files.writeString(playlistPath, playlistContent.toString());
@@ -196,7 +201,8 @@ public class FileInfoService {
+ playlistFileUri) + "/" + playlistFileUri; + playlistFileUri) + "/" + playlistFileUri;
folderInfo.playlist = playlistInfo; folderInfo.playlist = playlistInfo;
} }
} catch (IOException ignored) { } catch (IOException e) {
e.printStackTrace();
} }
return folderInfo; return folderInfo;
} else { } else {

View File

@@ -8,6 +8,7 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
@@ -87,11 +88,13 @@ public class ThumbnailService {
+ URLDecoder.decode(folderInfo.directUrl, StandardCharsets.UTF_8)); + URLDecoder.decode(folderInfo.directUrl, StandardCharsets.UTF_8));
try { try {
Optional<Path> imgPath = Files.list(folderPath).sorted() Stream<Path> stream = Files.list(folderPath);
Optional<Path> imgPath = stream.sorted()
.filter(p -> !Files.isDirectory(p) .filter(p -> !Files.isDirectory(p)
&& (p.toString().toLowerCase().endsWith(".png") && (p.toString().toLowerCase().endsWith(".png")
|| p.toString().toLowerCase().endsWith(".jpg"))) || p.toString().toLowerCase().endsWith(".jpg")))
.findFirst(); .findFirst();
stream.close();
if (imgPath.isPresent()) { if (imgPath.isPresent()) {
thumbPath.getParent().toFile().mkdirs(); thumbPath.getParent().toFile().mkdirs();

View File

@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream; import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
@@ -92,7 +93,8 @@ public class UploadService {
List<UploadedFile> uploadedFiles = new ArrayList<>(); List<UploadedFile> uploadedFiles = new ArrayList<>();
try { try {
Files.list(folderPath).forEach(p -> { Stream<Path> stream = Files.list(folderPath);
stream.forEach(p -> {
final UploadedFile uploadedFile = new UploadedFile(); final UploadedFile uploadedFile = new UploadedFile();
uploadedFile.iv = p.getFileName().toString(); uploadedFile.iv = p.getFileName().toString();
uploadedFile.downloadUrl = "/api/public/upload/" + user + "/" + uploadedFile.iv; uploadedFile.downloadUrl = "/api/public/upload/" + user + "/" + uploadedFile.iv;
@@ -108,6 +110,7 @@ public class UploadService {
e.printStackTrace(); e.printStackTrace();
} }
}); });
stream.close();
return Response.ok(uploadedFiles).build(); return Response.ok(uploadedFiles).build();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -185,8 +188,10 @@ public class UploadService {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();
} }
try { try {
Files.walk(folderPath).sorted(Comparator.reverseOrder()).map(Path::toFile) Stream<File> stream = Files.walk(folderPath).sorted(
.forEach(File::delete); Comparator.reverseOrder()).map(Path::toFile);
stream.forEach(File::delete);
stream.close();
return Response.ok().build(); return Response.ok().build();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();