Skip to content

Commit

Permalink
feat: do not preload hidden files and files in hidden folders
Browse files Browse the repository at this point in the history
Refs: #682
  • Loading branch information
An1s9n committed Feb 12, 2024
1 parent d218bc7 commit 992c1fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
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: <1882145060.266.1694877044545@127.0.0.1>
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: <1882145060.266.1694877044545@127.0.0.1>
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

0 comments on commit 992c1fe

Please sign in to comment.