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

do not preload hidden files and files in hidden folders #683

Merged
merged 1 commit into from
Feb 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public GreenMailOperations loadEmails(Path sourceDirectory) throws IOException,

try (final Stream<Path> pathStream = Files.walk(sourceDirectory)) {
for (Path emailPath : pathStream
.filter(path -> !path.equals(sourceDirectory)) // Skip base dir
.filter(path -> !path.equals(sourceDirectory) && !isHiddenOrInHiddenDir(path)) // Skip base dir and files which are hidden or in hidden dirs
.map(Path::toAbsolutePath)
.collect(Collectors.toList())) {
loadEmail(sourceDirectory, emailPath, sourceNameCount, userManager, store, imapHostManager, session);
Expand Down Expand Up @@ -379,15 +379,16 @@ private void loadEmail(Path sourceDirectory, Path emailPath, int sourceNameCount
}

// Extract and optionally create intermediate folders
MailFolder folder = null;
folder = store.getMailbox(getUserBaseMailboxName(imapHostManager, user));
for (int i = sourceNameCount + 1; i < emailPathNameCount - 1; i++) {
String namePart = emailPath.getName(i).toString();
MailFolder child = store.getMailbox(folder, namePart);
if (null == child) {
child = store.createMailbox(folder, namePart, true);
MailFolder folder = store.getMailbox(getUserBaseMailboxName(imapHostManager, user));;
for (int i = sourceNameCount + 1; i < emailPathNameCount; i++) {
if (i < emailPathNameCount - 1 || Files.isDirectory(emailPath)) {
String namePart = emailPath.getName(i).toString();
MailFolder child = store.getMailbox(folder, namePart);
if (null == child) {
child = store.createMailbox(folder, namePart, true);
}
folder = child;
}
folder = child;
}

if (Files.isRegularFile(emailPath) && emailPath.toString().endsWith(".eml")) {
Expand All @@ -410,4 +411,12 @@ private String getUserBaseMailboxName(ImapHostManager imapHostManager, GreenMail
}
return inbox.substring(0, inbox.length() - ImapConstants.INBOX_NAME.length());
}

private boolean isHiddenOrInHiddenDir(Path path) {
try {
return Files.isHidden(path) || (path.getParent() != null && isHiddenOrInHiddenDir(path.getParent()));
} catch (IOException e) {
throw new IllegalStateException("Failed during preloading '" + path + "'");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Date: Sat, 16 Sep 2023 17:10:44 +0200 (CEST)
From: bar@localhost
To: foo@localhost
Message-ID: <[email protected]>
Subject: test-5
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Test message saved as eml (electronic mail format, aka internet message format)
File is in hidden folder, so it should not be preloaded
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Date: Sat, 16 Sep 2023 17:10:44 +0200 (CEST)
From: bar@localhost
To: foo@localhost
Message-ID: <[email protected]>
Subject: test-5
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Test message saved as eml (electronic mail format, aka internet message format).
File is hidden, so it should not be preloaded
Loading