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

View File

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

View File

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