use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.
the class MoveFilesCleanup method cleanup.
@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<Path> firstExistingFileDir = databaseContext.getFirstExistingFileDir(fileDirectoryPreferences);
if (!firstExistingFileDir.isPresent()) {
return Collections.emptyList();
}
List<Path> paths = databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences);
String defaultFileDirectory = firstExistingFileDir.get().toString();
Optional<Path> targetDirectory = FileHelper.expandFilenameAsPath(defaultFileDirectory, paths);
if (!targetDirectory.isPresent()) {
return Collections.emptyList();
}
List<LinkedFile> fileList;
List<LinkedFile> newFileList;
if (singleFileFieldCleanup != null) {
fileList = Arrays.asList(singleFileFieldCleanup);
//Add all other except the current selected file
newFileList = entry.getFiles().stream().filter(name -> !name.equals(singleFileFieldCleanup)).collect(Collectors.toList());
} else {
newFileList = new ArrayList<>();
fileList = entry.getFiles();
}
boolean changed = false;
for (LinkedFile fileEntry : fileList) {
String oldFileName = fileEntry.getLink();
Optional<Path> oldFile = fileEntry.findIn(paths);
if (!oldFile.isPresent() || !Files.exists(oldFile.get())) {
newFileList.add(fileEntry);
continue;
}
String targetDirName = "";
if (!fileDirPattern.isEmpty()) {
targetDirName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, fileDirPattern, layoutPrefs);
}
Path newTargetFile = targetDirectory.get().resolve(targetDirName).resolve(oldFile.get().getFileName());
if (Files.exists(newTargetFile)) {
// We do not overwrite already existing files
newFileList.add(fileEntry);
continue;
}
try {
if (!Files.exists(newTargetFile)) {
Files.createDirectories(newTargetFile);
}
} catch (IOException e) {
LOGGER.error("Could no create necessary target directoires for renaming", e);
}
if (FileUtil.renameFile(oldFile.get(), newTargetFile, true)) {
changed = true;
String newEntryFilePath = Paths.get(defaultFileDirectory).relativize(newTargetFile).toString();
LinkedFile newFileEntry = fileEntry;
if (!oldFileName.equals(newTargetFile.toString())) {
newFileEntry = new LinkedFile(fileEntry.getDescription(), newEntryFilePath, fileEntry.getFileType());
changed = true;
}
newFileList.add(newFileEntry);
}
}
if (changed) {
Optional<FieldChange> change = entry.setFiles(newFileList);
if (change.isPresent()) {
return Collections.singletonList(change.get());
} else {
return Collections.emptyList();
}
}
return Collections.emptyList();
}
use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.
the class RelativePathsCleanup method cleanup.
@Override
public List<FieldChange> cleanup(BibEntry entry) {
List<LinkedFile> fileList = entry.getFiles();
List<LinkedFile> newFileList = new ArrayList<>();
boolean changed = false;
for (LinkedFile fileEntry : fileList) {
String oldFileName = fileEntry.getLink();
String newFileName = FileUtil.shortenFileName(Paths.get(oldFileName), databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences)).toString();
LinkedFile newFileEntry = fileEntry;
if (!oldFileName.equals(newFileName)) {
newFileEntry = new LinkedFile(fileEntry.getDescription(), newFileName, fileEntry.getFileType());
changed = true;
}
newFileList.add(newFileEntry);
}
if (changed) {
Optional<FieldChange> change = entry.setFiles(newFileList);
if (change.isPresent()) {
return Collections.singletonList(change.get());
} else {
return Collections.emptyList();
}
}
return Collections.emptyList();
}
use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.
the class CleanupWorkerTest method cleanupDoesNothingByDefault.
@Test
public void cleanupDoesNothingByDefault() throws IOException {
BibEntry entry = new BibEntry();
entry.setCiteKey("Toot");
entry.setField("pdf", "aPdfFile");
entry.setField("some", "1st");
entry.setField("doi", "http://dx.doi.org/10.1016/0001-8708(80)90035-3");
entry.setField("month", "01");
entry.setField("pages", "1-2");
entry.setField("date", "01/1999");
entry.setField("pdf", "aPdfFile");
entry.setField("ps", "aPsFile");
entry.setField("file", "link::");
entry.setField("journal", "test");
entry.setField("title", "<b>hallo</b> units 1 A case AlGaAs and latex $\\alpha$$\\beta$");
entry.setField("abstract", "Réflexions");
File tempFile = bibFolder.newFile();
LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
List<FieldChange> changes = worker.cleanup(emptyPreset, entry);
Assert.assertEquals(Collections.emptyList(), changes);
}
use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.
the class CleanupWorkerTest method cleanupRelativePathsConvertAbsoluteToRelativePath.
@Test
public void cleanupRelativePathsConvertAbsoluteToRelativePath() throws IOException {
CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.MAKE_PATHS_RELATIVE);
File tempFile = bibFolder.newFile();
BibEntry entry = new BibEntry();
LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
worker.cleanup(preset, entry);
LinkedFile newFileField = new LinkedFile("", tempFile.getName(), "");
Assert.assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField("file"));
}
use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.
the class RenamePdfCleanupTest method cleanupRenamePdfRenamesFileStartingWithBibtexKey.
@Test
public void cleanupRenamePdfRenamesFileStartingWithBibtexKey() throws IOException {
String fileNamePattern = "\\bibtexkey - \\title";
File tempFile = testFolder.newFile("Toot.tmp");
LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
entry.setField("title", "test title");
RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, mock(LayoutFormatterPreferences.class), fileDirPrefs);
cleanup.cleanup(entry);
LinkedFile newFileField = new LinkedFile("", "Toot - test title.tmp", "");
assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField("file"));
}
Aggregations