Skip to content

Commit

Permalink
fix: Unable to delete a folder with only file .DS_Store on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mindolph committed Jan 18, 2024
1 parent d2e0b13 commit 542b2e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,55 @@
import com.mindolph.core.constant.SupportFileTypes;
import com.mindolph.mfx.dialog.DialogFactory;
import com.mindolph.mfx.util.DesktopUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
* @author mindolph.com@gmail.com
*/
public class MindolphFileUtils {

/**
* Ignore the .DS_Store file from macOS.
*
* @param folder
* @return
* @throws IOException
*/
public static boolean isFolderEmpty(File folder) throws IOException {
if (folder == null || !folder.exists()) {
return true;
}
if (FileUtils.isEmptyDirectory(folder)) {
return true;
}
else {
String[] list = folder.list((dir, name) -> ".DS_Store".equals(name));
return list == null || list.length == 1;
}
}

/**
* Delete the .DS_Store file from macOS in folder.
*
* @param folder
*/
public static void deleteMacFile(File folder) {
// do not check current OS because the mac .DS_Store file might be copied to other OS.
File[] files = folder.listFiles((dir, name) -> ".DS_Store".equals(name));
if (files != null) {
for (File file : files) {
if (file.isHidden()) {
file.delete();
}
}
}
}

public static File getTempDir() {
return new File(SystemUtils.getUserHome(), "/Temp/mindolph");
}
Expand All @@ -32,6 +71,7 @@ public static File getTempFile(String fileName) {

/**
* TODO to be tested
*
* @param filePath
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import java.util.*;
import java.util.function.Consumer;

import static com.mindolph.base.util.MindolphFileUtils.deleteMacFile;
import static com.mindolph.base.util.MindolphFileUtils.isFolderEmpty;
import static com.mindolph.core.constant.SceneStatePrefs.*;
import static com.mindolph.core.constant.SupportFileTypes.*;

Expand Down Expand Up @@ -997,7 +999,7 @@ else if (source == miClone) {
else if (source == miDelete) {
if (selectedData != null) {
try {
if (selectedData.getFile().isDirectory() && !FileUtils.isEmptyDirectory(selectedData.getFile())) {
if (selectedData.getFile().isDirectory() && !isFolderEmpty(selectedData.getFile())) {
DialogFactory.errDialog("You can not delete a folder with files.");
return;
}
Expand All @@ -1013,6 +1015,9 @@ else if (source == miDelete) {
return;
}
log.info("Delete file: %s".formatted(selectedData.getFile()));
if (selectedData.getFile().isDirectory()){
deleteMacFile(selectedData.getFile());
}
try {
FileUtils.delete(selectedData.getFile());
} catch (IOException e) {
Expand Down

0 comments on commit 542b2e7

Please sign in to comment.