diff --git a/source/net/sourceforge/filebot/cli/CmdlineOperations.java b/source/net/sourceforge/filebot/cli/CmdlineOperations.java index bd438d9f..1d32edc4 100644 --- a/source/net/sourceforge/filebot/cli/CmdlineOperations.java +++ b/source/net/sourceforge/filebot/cli/CmdlineOperations.java @@ -219,19 +219,6 @@ public class CmdlineOperations implements CmdlineInterface { // add matches from other files that are linked via filenames matches.addAll(derivateMatches); - // first write all the metadata if xattr is enabled - if (useExtendedFileAttributes()) { - try { - for (Match match : matches) { - if (match.getCandidate() instanceof Episode) { - MediaDetection.storeMetaInfo(match.getValue(), match.getCandidate()); - } - } - } catch (Throwable e) { - CLILogger.warning("Failed to write xattr: " + e.getMessage()); - } - } - // map old files to new paths by applying formatting and validating filenames Map renameMap = new LinkedHashMap(); @@ -245,7 +232,7 @@ public class CmdlineOperations implements CmdlineInterface { // rename episodes Analytics.trackEvent("CLI", "Rename", "Episode", renameMap.size()); - return renameAll(renameMap, renameAction, conflictAction); + return renameAll(renameMap, renameAction, conflictAction, matches); } private List> matchEpisodes(Collection files, Collection episodes, boolean strict) throws Exception { @@ -475,19 +462,6 @@ public class CmdlineOperations implements CmdlineInterface { } } - // first write all the metadata if xattr is enabled - if (useExtendedFileAttributes()) { - try { - for (Match match : matches) { - if (match.getCandidate() instanceof Movie) { - MediaDetection.storeMetaInfo(match.getValue(), match.getCandidate()); - } - } - } catch (Throwable e) { - CLILogger.warning("Failed to write xattr: " + e.getMessage()); - } - } - // map old files to new paths by applying formatting and validating filenames Map renameMap = new LinkedHashMap(); @@ -501,7 +475,7 @@ public class CmdlineOperations implements CmdlineInterface { // rename movies Analytics.trackEvent("CLI", "Rename", "Movie", renameMap.size()); - return renameAll(renameMap, renameAction, conflictAction); + return renameAll(renameMap, renameAction, conflictAction, matches); } public List renameMusic(Collection files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, MusicIdentificationService service) throws Exception { @@ -538,7 +512,7 @@ public class CmdlineOperations implements CmdlineInterface { // rename movies Analytics.trackEvent("CLI", "Rename", "AudioTrack", renameMap.size()); - return renameAll(renameMap, renameAction, conflictAction); + return renameAll(renameMap, renameAction, conflictAction, null); } private Map getContext(final Collection> matches) { @@ -574,7 +548,7 @@ public class CmdlineOperations implements CmdlineInterface { return newFile; } - public List renameAll(Map renameMap, RenameAction renameAction, ConflictAction conflictAction) throws Exception { + public List renameAll(Map renameMap, RenameAction renameAction, ConflictAction conflictAction, List> matches) throws Exception { if (renameMap.isEmpty()) { throw new Exception(format("[%s] Unable to process any files", renameAction)); } @@ -631,6 +605,24 @@ public class CmdlineOperations implements CmdlineInterface { } } + // write metadata into xattr if xattr is enabled + if (matches != null && useExtendedFileAttributes()) { + try { + for (Match match : matches) { + File file = match.getValue(); + Object meta = match.getCandidate(); + if (renameMap.containsKey(file) && meta != null) { + File destination = resolveDestination(file, renameMap.get(file), false); + if (destination.isFile()) { + MediaDetection.storeMetaInfo(destination, meta); + } + } + } + } catch (Throwable e) { + CLILogger.warning("Failed to write xattr: " + e.getMessage()); + } + } + // new file names List destinationList = new ArrayList(); for (Entry it : renameLog) { diff --git a/source/net/sourceforge/filebot/ui/rename/RenameAction.java b/source/net/sourceforge/filebot/ui/rename/RenameAction.java index ab06c9f8..86223011 100644 --- a/source/net/sourceforge/filebot/ui/rename/RenameAction.java +++ b/source/net/sourceforge/filebot/ui/rename/RenameAction.java @@ -77,26 +77,15 @@ class RenameAction extends AbstractAction { } Map renameMap = checkRenamePlan(validate(model.getRenameMap(), window), window); - StandardRenameAction action = (StandardRenameAction) getValue(RENAME_ACTION); if (renameMap.isEmpty()) { return; } // start processing - window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + List> matches = new ArrayList>(model.matches()); + StandardRenameAction action = (StandardRenameAction) getValue(RENAME_ACTION); - // first write all the metadata if xattr is enabled - if (useExtendedFileAttributes()) { - try { - for (Match match : model.matches()) { - if (renameMap.containsKey(match.getCandidate()) && match.getValue() != null) { - MediaDetection.storeMetaInfo(match.getCandidate(), match.getValue()); - } - } - } catch (Throwable e) { - Logger.getLogger(RenameAction.class.getName()).warning("Failed to write xattr: " + e.getMessage()); - } - } + window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); if (useNativeShell() && isNativeActionSupported(action)) { RenameJob renameJob = new NativeRenameJob(renameMap, NativeRenameAction.valueOf(action.name())); @@ -124,6 +113,24 @@ class RenameAction extends AbstractAction { dialog.setVisible(true); } } + + // write metadata into xattr if xattr is enabled + if (useExtendedFileAttributes()) { + try { + for (Match match : matches) { + File file = match.getCandidate(); + Object meta = match.getValue(); + if (renameMap.containsKey(file) && meta != null) { + File destination = resolveDestination(file, renameMap.get(file), false); + if (destination.isFile()) { + MediaDetection.storeMetaInfo(destination, meta); + } + } + } + } catch (Throwable e) { + Logger.getLogger(RenameAction.class.getName()).warning("Failed to write xattr: " + e.getMessage()); + } + } } catch (ExecutionException e) { // ignore, handled in rename worker } catch (Throwable e) { diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index bde3c45c..15cf54c5 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -71,7 +71,7 @@ public final class FileUtilities { } else { // copy file try { - java.nio.file.Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); } catch (LinkageError e) { org.apache.commons.io.FileUtils.copyFile(source, destination); }